有没有办法在模拟对象中设置类级变量?
我有类似于这样的模拟对象:
$stub = $this->getMock('SokmeClass', array('method'));
$stub->expects($this->once())
->method('method')
->with($this->equalTo($arg1));
Run Code Online (Sandbox Code Playgroud)
赢得真正的类有一个变量需要设置才能正常工作.如何在模拟对象中设置该变量?
我正在使用phpunit框架,我有这样的代码:
public function A() {
try {
(...some code...)
die (json_encode ($data));
}
catch (Exception $e) {
die(false);
}
}
Run Code Online (Sandbox Code Playgroud)
这个函数是通过AJAX调用的,我不能用return替换die.问题是:如何使用这样的代码进行单元测试?
断言,我不能用它.
谢谢.
我刚开始使用PHPUnit.我写的一些简单测试正在进行中.所以一般来说PHPUnit已启动并正在运行.但MySQLi类有问题.
在我的代码中它工作正常.这是行:
$this->mysqli = new \mysqli($this->host, $user->getUser(), $user->getPwd(), $this->db);
Run Code Online (Sandbox Code Playgroud)
当运行phpunit解析此行时,我收到以下错误消息(指向该行):
PHP Fatal error: Class 'mysqli' not found in /opt/lampp/htdocs/...
Run Code Online (Sandbox Code Playgroud)
两种可能性(我认为):
1)我缺少一些功能/扩展/配置步骤/与使用MySQLi扩展的PHPUnit的正确设置相关的其他内容.
编辑
如果我测试扩展,extension_loaded('mysqli')它会true在我的正常代码中返回.如果我在测试中执行以下操作,它会跳过测试(即返回false):
if (!extension_loaded('mysqli')) {
$this->markTestSkipped(
'The MySQLi extension is not available.'
);
}
Run Code Online (Sandbox Code Playgroud)
/编辑
2)我的代码可能有问题.我正在尝试模拟User对象以进行测试连接.所以这里是:
<?php
class ConnectionTest extends \PHPUnit_Framework_TestCase
{
private $connection;
protected function setUp()
{
$user = $this->getMockBuilder('mysqli\User')
->setMethods(array('getUser', 'getPwd'))
->getMock();
$user->expects($this->once())
->method('getUser')
->will($this->returnValue('username'));
$user->expects($this->once())
->method('getPwd')
->will($this->returnValue('p@ssw0rd'));
$this->connection = new \mysqli\Connection($user);
}
public function testInternalTypeGetMysqli()
{
$actual = …Run Code Online (Sandbox Code Playgroud) 我是PHPUnit和单元测试的新手,所以我有一个任务:我可以在类之外测试一个函数:
function odd_or_even( $num ) {
return $num%2; // Returns 0 for odd and 1 for even
}
class test extends PHPUnit_Framework_TestCase {
public function odd_or_even_to_true() {
$this->assetTrue( odd_or_even( 4 ) == true );
}
}
Run Code Online (Sandbox Code Playgroud)
现在它只是返回:
No tests found in class "test".
Run Code Online (Sandbox Code Playgroud) 我正在尝试探索在类级别上使用@group或@author注释的可能性,以便我可以为特定的人分配某种所有权.另外,有了这个,我计划对事物进行宏观管理,例如:如果我想要运行一个或多个类(完整地),我可以将它们的组指定为ABC,然后使用--groups选项.
目前,我认为@groups或@author仅用于测试用例级别,而不是测试级别.我认为一个类可能有数百个测试用例,写作@author或者@group非常繁琐.并且在将来,如果所有权发生变化,我们需要在任何地方更改注释属性.因此,有没有办法@group在课堂上指定或类似的东西?
每次我运行一个类或整个文件夹的单元测试时,phpunit会为整个系统生成覆盖,因为它是在phpunit.xml中配置的.
这很糟糕,因为它需要更长的时间并耗尽PHP的内存.
我的phpunit.xml
<!-- http://www.phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit
backupGlobals = "false"
backupStaticAttributes = "false"
colors = "true"
convertErrorsToExceptions = "true"
convertNoticesToExceptions = "true"
convertWarningsToExceptions = "true"
processIsolation = "false"
stopOnFailure = "false"
syntaxCheck = "false"
bootstrap = "Bootstrap.php" >
<testsuites>
<testsuite name="Application Module Suite Test">
<directory>./Module1Test</directory>
<directory>./Module2Test</directory>
<directory>./Module3Test</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>../module/Module1</directory>
<directory>../module/Module2</directory>
<directory>../module/Module3</directory>
</whitelist>
</filter>
</phpunit>
Run Code Online (Sandbox Code Playgroud)
有没有一种方法来生成的唯一的东西我测试现在,覆盖动态?
示例
对于下面的命令,我想Controller/ExampleController.php仅为路径生成coverage .
phpunit Controller/ExampleController.php --coverage-html ~/Desktop/tests
Run Code Online (Sandbox Code Playgroud)
我正在使用PHPUnit 4.8和3.7,Sublime Text Editor,应用程序正在使用Zend Framework 2.
我最近在学习Symfony 3框架和依赖注入.
我希望您帮助我解决我对使用PHPUnit在Symfony 3 中测试服务的方法的疑虑.我有一些担心如何正确地做到这一点.
让我们举一个Service类的例子:
// src/AppBundle/Services/MathService.php
namespace AppBundle\Services;
class MathService
{
public function subtract($a, $b)
{
return $a - $b;
}
}
Run Code Online (Sandbox Code Playgroud)
我看到通常Symfony中的UnitTest类测试控制器.
但是,我可以测试像服务这样的独立类(例如包含业务逻辑)而不是控制器?
我知道至少有两种方法可以做到:
1.创建一个测试类,在此测试类中的某些方法或构造函数中扩展PHPUnit_Framework_TestCase和创建 Service对象(与Symfony关于测试的文档完全相同)
// tests/AppBundle/Services/MathTest.php
namespace Tests\AppBundle\Services;
use AppBundle\Services\MathService;
class MathTest extends \PHPUnit_Framework_TestCase
{
protected $math;
public function __construct() {
$this->math = new MathService();
}
public function testSubtract() …Run Code Online (Sandbox Code Playgroud) 我使用symfony 3.0与phpUnit框架3.7.18
单元测试文件. abcControllerTest.php
namespace AbcBundle\Tests\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpFoundation\Response;
class AbcControllerTest extends WebTestCase {
public function testWithParams() {
$Params = array("params" => array("page_no" => 5));
$expectedData = $this->listData($Params);
print_r($expectedData);
}
private function listData($Params) {
$client = static::createClient();
$server = array('HTTP_CONTENT_TYPE' => 'application/json', 'HTTP_ACCEPT' => 'application/json');
$crawler = $client->request('POST', $GLOBALS['host'] . '/abc/list', $Params,array(), $server);
$response = $client->getResponse();
$this->assertSame('text/html; charset=UTF-8', $response->headers->get('Content-Type'));
$expectedData = json_decode($response->getContent());
return $expectedData;
}
}
Run Code Online (Sandbox Code Playgroud)
行动:abc/list
abcController.php
public function listAction(Request $request) {
$Params = json_decode($request->getContent(), true); …Run Code Online (Sandbox Code Playgroud) 我有一个Laravel 5.4应用程序,它的模型指向不同的数据库连接.
例如,我User指向一个MySQL数据库,然后Company指向一个PostgreSQL数据库(使用该$connection变量).
现在,当我运行PHPUnit时,我希望将$connection变量替换为phpunit.xml文件中指定的内容,这是内存类型数据库中的SQLite.
这怎么可以实现?
我有这样的情况.我有一些第三方特征(我不想测试)我有我的特性使用这个特性,在某些情况下运行第三方特征方法(在下面的例子我总是运行它).
当我有这样的代码:
use Mockery;
use PHPUnit\Framework\TestCase;
class SampleTest extends TestCase
{
/** @test */
public function it_runs_parent_method_alternative()
{
$class = Mockery::mock(B::class)->makePartial();
$class->shouldReceive('fooX')->once();
$this->assertSame('bar', $class->foo());
}
protected function tearDown()
{
Mockery::close();
}
}
trait X {
function foo() {
$this->something->complex3rdpartyStuff();
}
}
trait Y2 {
function foo() {
$this->fooX();
return 'bar';
}
}
class B {
use Y2, X {
Y2::foo insteadof X;
X::foo as fooX;
}
}
Run Code Online (Sandbox Code Playgroud)
它会工作正常,但我不希望代码组织这样.在上面的类I代码中使用两个traits,但在代码中我想测试其实trait使用开头提到的其他特性.
但是当我有这样的代码时:
<?php
use Mockery;
use PHPUnit\Framework\TestCase;
class SampleTest extends TestCase
{
/** …Run Code Online (Sandbox Code Playgroud)