我想使用命令行向PHP单元发送一个参数.
例如
./phpunit --foo='bar' AllTests
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
我能够达到目标的最接近的是使用以下内容:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit>
<php>
<env name="foo" value="bar"/>
</php>
</phpunit>
Run Code Online (Sandbox Code Playgroud)
然后我可以使用访问变量$_ENV['foo'].
但是,我想使用命令行发送此变量.
我有一个Symfony控制台命令配置如下:
protected function configure()
{
$this
->setName('crmpiccobundle:list:sync')
->setDescription('Sync users to the list')
->addOption(
'filepath',
null,
InputOption::VALUE_OPTIONAL,
'The path to the file',
null
)
;
}
Run Code Online (Sandbox Code Playgroud)
...我有一个PHPUnit测试,看起来像这样:
public function testExecuteWhenFileIsEmpty()
{
self::bootKernel();
$application = new Application(self::$kernel);
$application->add(new Sync());
$command = $application->find('crmpiccobundle:list:sync');
$commandTester = new CommandTester($command);
$commandTester->execute([
'command' => $command->getName(),
'filepath' => '/tmp/crmpicco.co.uk.csv'
]);
}
Run Code Online (Sandbox Code Playgroud)
当我运行它时,我收到以下错误:
InvalidArgumentException:"filepath"参数不存在.
我的问题是-我怎么通过一个选项的CommandTester?传递参数很好,但是我找不到关于如何为Options做的文档.
由于我仍然对PHPUnit / PHP代码覆盖率和Xdebug感到烦恼,因此我决定尝试另一种方法-使用phpdbg。
我这样做是因为票数所示。在CMD和Git Bash中尝试过,但结果相同,但失败了:
$ composer info | grep "phpunit"
phpunit/php-code-coverage 4.0.0 Library that provides collectio...
phpunit/php-file-iterator 1.4.1 FilterIterator implementation t...
phpunit/php-text-template 1.2.1 Simple template engine.
phpunit/php-timer 1.0.8 Utility class for timing
phpunit/php-token-stream 1.4.8 Wrapper around PHP's tokenizer ...
phpunit/phpunit 5.4.6 The PHP Unit Testing framework.
phpunit/phpunit-mock-objects 3.2.3 Mock Object library for PHPUnit
$ phpdbg -qrr ./vendor/bin/phpunit -v
dir=$(d=${0%[/\\]*}; cd "$d"; cd "../phpunit/phpunit" && pwd)
# See if we are running …Run Code Online (Sandbox Code Playgroud) 使用phpunit和Symfony(扩展WebTestCase类)创建单元测试时,我需要执行指定内容类型的请求.
所以我有我的客户:
$client->request('POST', '/my/route');
但请求内容类型默认为application/x-www-form-urlencoded,我该如何更改?
例如将其设置为 application/json
每次我prepareStatusFilterQuery()在使用debug进行单元测试时进入方法()时,我都会收到警告并且调试器没有进入方法.警告:
无法在本地找到文件'/ 3'.要修复它,请通过环境变量PHP_IDE_CONFIG设置服务器名称并重新启动调试会话.
除了这种情况,调试器工作正常.
我有:
1. IntegrationTestCase extends TestCase
2. UnitTestCase extends TestCase
3. AcceptanceTestCase extends TestCase
Run Code Online (Sandbox Code Playgroud)
在这些中,我有很多非静态方法,它们在很多测试中使用.我的所有Test类都扩展了这3个类中的一个.
现在在很多测试类中,我有一个setUp方法,它准备所需的数据和服务,并将它们分配给类变量:
class SomeTestClass extends IntegrationTestCase
{
private $foo;
public function setUp()
{
parent::setUp();
$bar = $this->createBar(...);
$this->foo = new Foo($bar);
}
public function testA() { $this->foo...; }
public function testB() { $this->foo...; }
}
Run Code Online (Sandbox Code Playgroud)
setUp每个测试都会遇到问题,无法完成我想要做的事情,如果setUp需要花费很长时间,那么这个方法会乘以测试方法的数量.
使用public function __construct(...) { parent::__construct(..); ... }会产生问题,因为现在Laravel中的低级方法和类不可用.
我有以下PHPUnit测试用例:
$mailer = $this->getMockBuilder(MailerInterface::class)->getMock();
$simpleMailer = new SimpleMailer($mailer);
$message = (new Message())
->setTo($user)
->setFrom($from)
->setSubject($subject)
->setTemplate(SimpleMailer::TEMPLATE)
->setContext(['message' => $body]);
if ($bcc) { $message->addBcc($bcc); }
$mailer
->expects($this->once())
->method('send')
->with($this->equalTo($message));
$simpleMailer->sendMessage($user, $subject, $body, $from, $bcc);
Run Code Online (Sandbox Code Playgroud)
在更改Message类之前,此方法一直工作良好。Message类现在在构造上设置一个唯一的ID,这意味着它equalTo现在返回false,具有以下区别:
MailerBundle\Document\Message Object (
- 'id' => '5a372f3c-a8a9-4e1e-913f-d756244c8e52'
+ 'id' => '11176427-7d74-4a3c-8708-0026ae666f8b'
'type' => null
'user' => Tests\TestUser Object (...)
'toName' => ''
'toAddress' => null
'domain' => null
'fromName' => null
'fromAddress' => 'user@example.org'
'bccAddresses' => Array (...)
'subject' => 'subject'
'textBody' => null …Run Code Online (Sandbox Code Playgroud) 当前,我正在尝试使用命令行phpunit tests / Unit / ExampleTest在laravel项目上使用phpunit运行一些简单的单元测试。但是它仅运行第一种方法。有人有解决方案吗?谢谢 !
namespace Tests\Unit;
use PHPUnit\Framework\TestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ExampleTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function testBasicTest()
{
$this->assertTrue(true);
}
/**
* An other basic test
*
* @return void
*/
public function secondTestBasicTest()
{
$this->assertTrue(true);
}
public function basicTest(){
$response = $this->action('GET', 'DepartementsController@display', ['id' => 1]);
$view = $response->original;
$this->assertEquals(1, $view['$dpt->iddepartement']);
}
}
Run Code Online (Sandbox Code Playgroud) 我想使用PHPUnit测试我的PHP类。
是否可以将用于我的测试方法的数据提供者放在一个单独的文件中,该文件仅用于存储数据提供者?如果是这样,该怎么做?
另一个问题是,将测试方法和数据提供程序方法放在同一个测试类中是一种好习惯还是更好的选择。
让我们psr/log特别看一下中的代码:
如您所知,特征无法实现接口,因此这两部分需要一个类才能成功连接在一起。
假设我涵盖了特性测试(通过PHPUnit相对容易getMockForTrait)。接下来要测试的是,我想证明该特性满足该接口。
在代码方面,它看起来很简单:
public function testThatTraitSatisfiesInterface()
{
$className = 'test_class_' . uniqid();
$classCode = sprintf(
'class %s implements %s { use %s; }',
$className,
LoggerAwareInterface::class,
LoggerAwareTrait::class
);
eval($classCode); // ewww :see_no_evil:
new $className(); // no errors? good, test successful
}
Run Code Online (Sandbox Code Playgroud)
这里有一些问题:
eval()尽可能地避免(即使我知道反正是驱动PHPUnit的原因),但是..因此,最大的问题是,还有其他选择吗?
phpunit ×10
php ×9
unit-testing ×4
laravel ×2
symfony ×2
content-type ×1
dataprovider ×1
interface ×1
php-7 ×1
phpdbg ×1
phpstorm ×1
testing ×1
traits ×1
xdebug ×1