在 Laravel 5.2 中使用 PHPUnit 测试命令的可接受方法是什么?
Which is an acceptable approach for testing commands in Laravel 5.2 with PHPUnit?
我正在尝试在 PHPUnit 中为 Commands 编写测试用例,但没有成功。
在这一点上,我已经尝试了很多东西,可能是 this post 我找到的最接近我的目的的方法。尽管如此,我仍在努力使它正常工作。
下面是一个示例输出:
alariva@trinsic ~/timegrid.io/app $ phpunit --filter=SendBusinessReportTest
PHP Warning: The use statement with non-compound name 'Artisan' has no effect in /home/alariva/timegrid.io/app/tests/unit/Console/Commands/SendBusinessReportTest.php on line 4
PHP Stack trace:
PHP 1. {main}() /usr/bin/phpunit:0
PHP 2. PHPUnit_TextUI_Command::main() /usr/bin/phpunit:46
PHP 3. PHPUnit_TextUI_Command->run() /usr/share/php/PHPUnit/TextUI/Command.php:129
PHP 4. PHPUnit_TextUI_Command->handleArguments() /usr/share/php/PHPUnit/TextUI/Command.php:138
PHP 5. PHPUnit_Util_Configuration->getTestSuiteConfiguration() /usr/share/php/PHPUnit/TextUI/Command.php:657
PHP 6. PHPUnit_Util_Configuration->getTestSuite() /usr/share/php/PHPUnit/Util/Configuration.php:789
PHP 7. PHPUnit_Framework_TestSuite->addTestFiles() /usr/share/php/PHPUnit/Util/Configuration.php:873
PHP 8. PHPUnit_Framework_TestSuite->addTestFile() /home/alariva/timegrid.io/app/vendor/phpunit/phpunit/src/Framework/TestSuite.php:409
PHP 9. PHPUnit_Util_Fileloader::checkAndLoad() /home/alariva/timegrid.io/app/vendor/phpunit/phpunit/src/Framework/TestSuite.php:335
PHP 10. PHPUnit_Util_Fileloader::load() /usr/share/php/PHPUnit/Util/Fileloader.php:76
PHPUnit 4.8.26 by Sebastian Bergmann and contributors.
PHP Fatal error: Call to undefined method App\Console\Kernel::resolve() in /home/alariva/timegrid.io/app/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php on line 217
PHP Stack trace:
PHP 1. {main}() /usr/bin/phpunit:0
PHP 2. PHPUnit_TextUI_Command::main() /usr/bin/phpunit:46
PHP 3. PHPUnit_TextUI_Command->run() /usr/share/php/PHPUnit/TextUI/Command.php:129
PHP 4. PHPUnit_TextUI_TestRunner->doRun() /usr/share/php/PHPUnit/TextUI/Command.php:176
PHP 5. PHPUnit_Framework_TestSuite->run() /home/alariva/timegrid.io/app/vendor/phpunit/phpunit/src/TextUI/TestRunner.php:440
PHP 6. PHPUnit_Framework_TestSuite->run() /home/alariva/timegrid.io/app/vendor/phpunit/phpunit/src/Framework/TestSuite.php:747
PHP 7. PHPUnit_Framework_TestCase->run() /home/alariva/timegrid.io/app/vendor/phpunit/phpunit/src/Framework/TestSuite.php:747
PHP 8. PHPUnit_Framework_TestResult->run() /home/alariva/timegrid.io/app/vendor/phpunit/phpunit/src/Framework/TestCase.php:724
PHP 9. PHPUnit_Framework_TestCase->runBare() /home/alariva/timegrid.io/app/vendor/phpunit/phpunit/src/Framework/TestResult.php:612
PHP 10. PHPUnit_Framework_TestCase->runTest() /home/alariva/timegrid.io/app/vendor/phpunit/phpunit/src/Framework/TestCase.php:768
PHP 11. ReflectionMethod->invokeArgs() /home/alariva/timegrid.io/app/vendor/phpunit/phpunit/src/Framework/TestCase.php:909
PHP 12. SendBusinessReportTest->it_tests_command() /home/alariva/timegrid.io/app/vendor/phpunit/phpunit/src/Framework/TestCase.php:909
PHP 13. Illuminate\Support\Facades\Artisan::resolve() /home/alariva/timegrid.io/app/tests/unit/Console/Commands/SendBusinessReportTest.php:14
PHP 14. Illuminate\Support\Facades\Facade::__callStatic() /home/alariva/timegrid.io/app/tests/unit/Console/Commands/SendBusinessReportTest.php:14
[Symfony\Component\Debug\Exception\FatalErrorException]
Call to undefined method App\Console\Kernel::resolve()
你可以找到 my current attempt here,(失败的测试),但也许你可以提示我一些我遗漏的东西:)
仅供参考,我现在的表现如何,但似乎没有涵盖代码:Coverage Report and Test Case
Side Note: You may ask why PHPUnit and not another testing framework. So far I'm generating my test coverage with PHPUnit and I'd like to stick to it until I feel the actual need to switch. However, all suggestions are welcome.
提前致谢!
在上述情况下,您应该删除 use Artisan;
,因为您没有为您的测试使用任何名称空间
我得到了一个可接受的方法如下:
<?php
use App\Console\Commands\SendBusinessReport;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Symfony\Component\Console\Application as ConsoleApplication;
use Symfony\Component\Console\Tester\CommandTester;
use Timegridio\Concierge\Models\Appointment;
class SendBusinessReportTest extends TestCase
{
use DatabaseTransactions;
use CreateBusiness, CreateUser, CreateContact, CreateAppointment, CreateService, CreateVacancy;
/** @test */
public function it_reports_to_all_businesses()
{
$this->arrangeFixture();
// BEGIN Actual solution code
$application = new ConsoleApplication();
$testedCommand = $this->app->make(SendBusinessReport::class);
$testedCommand->setLaravel(app());
$application->add($testedCommand);
$command = $application->find('business:report');
$commandTester = new CommandTester($command);
$commandTester->execute([
'command' => $command->getName(),
]);
// END Actual solution code
$this->assertRegExp('/Scanning all businesses../', $commandTester->getDisplay());
}
/**
* Arrange Fixture.
*
* @return void
*/
protected function arrangeFixture()
{
$this->owner = $this->createUser();
$this->issuer = $this->createUser();
$this->business = $this->createBusiness();
$this->business->owners()->save($this->owner);
$this->contact = $this->createContact();
$this->contact->user()->associate($this->issuer);
$this->service = $this->makeService();
$this->business->services()->save($this->service);
$this->vacancy = $this->makeVacancy();
$this->vacancy->service()->associate($this->service);
$this->business->vacancies()->save($this->vacancy);
$appointment = $this->makeAppointment($this->business, $this->issuer, $this->contact, [
'status' => Appointment::STATUS_CONFIRMED,
]);
}
}
alariva@trinsic ~/timegrid.io/app $ phpunit --filter=it_reports_to_all_businesses
PHPUnit 5.4.6 by Sebastian Bergmann and contributors.
. 1 / 1 (100%)
Time: 813 ms, Memory: 30.00MB
OK (1 test, 1 assertion)
我正在尝试在 PHPUnit 中为 Commands 编写测试用例,但没有成功。
在这一点上,我已经尝试了很多东西,可能是 this post 我找到的最接近我的目的的方法。尽管如此,我仍在努力使它正常工作。
下面是一个示例输出:
alariva@trinsic ~/timegrid.io/app $ phpunit --filter=SendBusinessReportTest
PHP Warning: The use statement with non-compound name 'Artisan' has no effect in /home/alariva/timegrid.io/app/tests/unit/Console/Commands/SendBusinessReportTest.php on line 4
PHP Stack trace:
PHP 1. {main}() /usr/bin/phpunit:0
PHP 2. PHPUnit_TextUI_Command::main() /usr/bin/phpunit:46
PHP 3. PHPUnit_TextUI_Command->run() /usr/share/php/PHPUnit/TextUI/Command.php:129
PHP 4. PHPUnit_TextUI_Command->handleArguments() /usr/share/php/PHPUnit/TextUI/Command.php:138
PHP 5. PHPUnit_Util_Configuration->getTestSuiteConfiguration() /usr/share/php/PHPUnit/TextUI/Command.php:657
PHP 6. PHPUnit_Util_Configuration->getTestSuite() /usr/share/php/PHPUnit/Util/Configuration.php:789
PHP 7. PHPUnit_Framework_TestSuite->addTestFiles() /usr/share/php/PHPUnit/Util/Configuration.php:873
PHP 8. PHPUnit_Framework_TestSuite->addTestFile() /home/alariva/timegrid.io/app/vendor/phpunit/phpunit/src/Framework/TestSuite.php:409
PHP 9. PHPUnit_Util_Fileloader::checkAndLoad() /home/alariva/timegrid.io/app/vendor/phpunit/phpunit/src/Framework/TestSuite.php:335
PHP 10. PHPUnit_Util_Fileloader::load() /usr/share/php/PHPUnit/Util/Fileloader.php:76
PHPUnit 4.8.26 by Sebastian Bergmann and contributors.
PHP Fatal error: Call to undefined method App\Console\Kernel::resolve() in /home/alariva/timegrid.io/app/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php on line 217
PHP Stack trace:
PHP 1. {main}() /usr/bin/phpunit:0
PHP 2. PHPUnit_TextUI_Command::main() /usr/bin/phpunit:46
PHP 3. PHPUnit_TextUI_Command->run() /usr/share/php/PHPUnit/TextUI/Command.php:129
PHP 4. PHPUnit_TextUI_TestRunner->doRun() /usr/share/php/PHPUnit/TextUI/Command.php:176
PHP 5. PHPUnit_Framework_TestSuite->run() /home/alariva/timegrid.io/app/vendor/phpunit/phpunit/src/TextUI/TestRunner.php:440
PHP 6. PHPUnit_Framework_TestSuite->run() /home/alariva/timegrid.io/app/vendor/phpunit/phpunit/src/Framework/TestSuite.php:747
PHP 7. PHPUnit_Framework_TestCase->run() /home/alariva/timegrid.io/app/vendor/phpunit/phpunit/src/Framework/TestSuite.php:747
PHP 8. PHPUnit_Framework_TestResult->run() /home/alariva/timegrid.io/app/vendor/phpunit/phpunit/src/Framework/TestCase.php:724
PHP 9. PHPUnit_Framework_TestCase->runBare() /home/alariva/timegrid.io/app/vendor/phpunit/phpunit/src/Framework/TestResult.php:612
PHP 10. PHPUnit_Framework_TestCase->runTest() /home/alariva/timegrid.io/app/vendor/phpunit/phpunit/src/Framework/TestCase.php:768
PHP 11. ReflectionMethod->invokeArgs() /home/alariva/timegrid.io/app/vendor/phpunit/phpunit/src/Framework/TestCase.php:909
PHP 12. SendBusinessReportTest->it_tests_command() /home/alariva/timegrid.io/app/vendor/phpunit/phpunit/src/Framework/TestCase.php:909
PHP 13. Illuminate\Support\Facades\Artisan::resolve() /home/alariva/timegrid.io/app/tests/unit/Console/Commands/SendBusinessReportTest.php:14
PHP 14. Illuminate\Support\Facades\Facade::__callStatic() /home/alariva/timegrid.io/app/tests/unit/Console/Commands/SendBusinessReportTest.php:14
[Symfony\Component\Debug\Exception\FatalErrorException]
Call to undefined method App\Console\Kernel::resolve()
你可以找到 my current attempt here,(失败的测试),但也许你可以提示我一些我遗漏的东西:)
仅供参考,我现在的表现如何,但似乎没有涵盖代码:Coverage Report and Test Case
Side Note: You may ask why PHPUnit and not another testing framework. So far I'm generating my test coverage with PHPUnit and I'd like to stick to it until I feel the actual need to switch. However, all suggestions are welcome.
提前致谢!
在上述情况下,您应该删除 use Artisan;
,因为您没有为您的测试使用任何名称空间
我得到了一个可接受的方法如下:
<?php
use App\Console\Commands\SendBusinessReport;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Symfony\Component\Console\Application as ConsoleApplication;
use Symfony\Component\Console\Tester\CommandTester;
use Timegridio\Concierge\Models\Appointment;
class SendBusinessReportTest extends TestCase
{
use DatabaseTransactions;
use CreateBusiness, CreateUser, CreateContact, CreateAppointment, CreateService, CreateVacancy;
/** @test */
public function it_reports_to_all_businesses()
{
$this->arrangeFixture();
// BEGIN Actual solution code
$application = new ConsoleApplication();
$testedCommand = $this->app->make(SendBusinessReport::class);
$testedCommand->setLaravel(app());
$application->add($testedCommand);
$command = $application->find('business:report');
$commandTester = new CommandTester($command);
$commandTester->execute([
'command' => $command->getName(),
]);
// END Actual solution code
$this->assertRegExp('/Scanning all businesses../', $commandTester->getDisplay());
}
/**
* Arrange Fixture.
*
* @return void
*/
protected function arrangeFixture()
{
$this->owner = $this->createUser();
$this->issuer = $this->createUser();
$this->business = $this->createBusiness();
$this->business->owners()->save($this->owner);
$this->contact = $this->createContact();
$this->contact->user()->associate($this->issuer);
$this->service = $this->makeService();
$this->business->services()->save($this->service);
$this->vacancy = $this->makeVacancy();
$this->vacancy->service()->associate($this->service);
$this->business->vacancies()->save($this->vacancy);
$appointment = $this->makeAppointment($this->business, $this->issuer, $this->contact, [
'status' => Appointment::STATUS_CONFIRMED,
]);
}
}
alariva@trinsic ~/timegrid.io/app $ phpunit --filter=it_reports_to_all_businesses
PHPUnit 5.4.6 by Sebastian Bergmann and contributors.
. 1 / 1 (100%)
Time: 813 ms, Memory: 30.00MB
OK (1 test, 1 assertion)