Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

php - Which is an acceptable approach for testing commands in Laravel 5.2 with PHPUnit?

I'm trying to write test cases for Commands in PHPUnit, without much success.

At this point I've tried many things, being probably this post the closest approach I found for my purpose. Still, I'm struggling a lot to get this working.

Follows an example output for you:

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 AppConsoleKernel::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. IlluminateSupportFacadesArtisan::resolve() /home/alariva/timegrid.io/app/tests/unit/Console/Commands/SendBusinessReportTest.php:14
PHP  14. IlluminateSupportFacadesFacade::__callStatic() /home/alariva/timegrid.io/app/tests/unit/Console/Commands/SendBusinessReportTest.php:14

  [SymfonyComponentDebugExceptionFatalErrorException]  
  Call to undefined method AppConsoleKernel::resolve() 

You can find my current attempt here, (broken tests), but maybe you can hint me something I'm missing :)

Just for reference, how I'm doing right now but does not seem to cover the code: 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.

Thanks in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In above case, you should remove use Artisan; because you don't use any namespace for your test


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...