using the classical phpunit style I generate:test with the symfony2 helper.
I can get the service and I can assert it ok.
// tests
public function testGetServiceUrl() {
$ecservice = $this->getModule('Symfony2')->grabServiceFromContainer("ecservice");
$this->assertEquals("https://ecoconnect2.niwa.co.nz/services", $ecservice->getServiceUrl());
$this->assertEquals("xxx", $ecservice->getServiceUrl());
}
However in the second case where the assertion fails I get an exception:
Trying to test get service url (demoTest::testGetServiceUrl) - Failed
PHP Fatal error: Call to a member function getResponse() on a non-object in /Users/watkinsav/workspace/cd/ecoconnect_web/vendor/codeception/codeception/src/Codeception/Util/Framework.php on line 30
PHP Stack trace:
PHP 1. {main}() /Users/watkinsav/workspace/cd/ecoconnect_web/vendor/codeception/codeception/codecept:0
PHP 2. Symfony\Component\Console\Application->run() /Users/watkinsav/workspace/cd/ecoconnect_web/vendor/codeception/codeception/codecept:38
PHP 3. Symfony\Component\Console\Application->doRun() /Users/watkinsav/workspace/cd/ecoconnect_web/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:106
PHP 4. Symfony\Component\Console\Command\Command->run() /Users/watkinsav/workspace/cd/ecoconnect_web/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:193
PHP 5. Codeception\Command\Run->execute() /Users/watkinsav/workspace/cd/ecoconnect_web/vendor/symfony/symfony/src/Symfony/Component/Console/Command/Command.php:240
PHP 6. Codeception\Codecept->runSuite() /Users/watkinsav/workspace/cd/ecoconnect_web/vendor/codeception/codeception/src/Codeception/Command/Run.php:74
PHP 7. Codeception\SuiteManager->run() /Users/watkinsav/workspace/cd/ecoconnect_web/vendor/codeception/codeception/src/Codeception/Codecept.php:110
PHP 8. Codeception\PHPUnit\Runner->doEnhancedRun() /Users/watkinsav/workspace/cd/ecoconnect_web/vendor/codeception/codeception/src/Codeception/SuiteManager.php:132
PHP 9. PHPUnit_Framework_TestSuite->run() /Users/watkinsav/workspace/cd/ecoconnect_web/vendor/codeception/codeception/src/Codeception/PHPUnit/Runner.php:107
PHP 10. PHPUnit_Framework_TestSuite->runTest() /Users/watkinsav/workspace/cd/ecoconnect_web/vendor/phpunit/phpunit/PHPUnit/Framework/TestSuite.php:745
PHP 11. PHPUnit_Framework_TestCase->run() /Users/watkinsav/workspace/cd/ecoconnect_web/vendor/phpunit/phpunit/PHPUnit/Framework/TestSuite.php:775
PHP 12. PHPUnit_Framework_TestResult->run() /Users/watkinsav/workspace/cd/ecoconnect_web/vendor/phpunit/phpunit/PHPUnit/Framework/TestCase.php:776
PHP 13. PHPUnit_Framework_TestResult->addFailure() /Users/watkinsav/workspace/cd/ecoconnect_web/vendor/phpunit/phpunit/PHPUnit/Framework/TestResult.php:698
PHP 14. Codeception\PHPUnit\Listener->addFailure() /Users/watkinsav/workspace/cd/ecoconnect_web/vendor/phpunit/phpunit/PHPUnit/Framework/TestResult.php:307
PHP 15. Codeception\PHPUnit\Listener->fire() /Users/watkinsav/workspace/cd/ecoconnect_web/vendor/codeception/codeception/src/Codeception/PHPUnit/Listener.php:24
PHP 16. Symfony\Component\EventDispatcher\EventDispatcher->dispatch() /Users/watkinsav/workspace/cd/ecoconnect_web/vendor/codeception/codeception/src/Codeception/PHPUnit/Listener.php:66
PHP 17. Symfony\Component\EventDispatcher\EventDispatcher->doDispatch() /Users/watkinsav/workspace/cd/ecoconnect_web/vendor/symfony/symfony/src/Symfony/Component/EventDispatcher/EventDispatcher.php:53
PHP 18. call_user_func() /Users/watkinsav/workspace/cd/ecoconnect_web/vendor/symfony/symfony/src/Symfony/Component/EventDispatcher/EventDispatcher.php:164
PHP 19. Codeception\Subscriber\Module->failed() /Users/watkinsav/workspace/cd/ecoconnect_web/vendor/symfony/symfony/src/Symfony/Component/EventDispatcher/EventDispatcher.php:164
PHP 20. Codeception\Util\Framework->_failed() /Users/watkinsav/workspace/cd/ecoconnect_web/vendor/codeception/codeception/src/Codeception/Subscriber/Module.php:47
This is because the _failed() function in the Framework.php tries to call client-getResponse(). And we don't have a response class instantiated.
public function _failed(\Codeception\TestCase $test, $fail)
{
if (!$this->client->getResponse()) return;
file_put_contents(\Codeception\Configuration::logDir() . basename($test->getFileName()) . '.page.debug.html', $this->client->getResponse()->getContent());
}
In the example blog post here: http://codeception.com/02-12-2013/testing-symfony2.html There are some extra lines - but they do not fix the problem.
Thanks again Andrew
Well, looks like this function expected to get null from getResponse.
if (!$this->client->getResponse()) return;
This line should have blocked the screenshot call if no response is available.
However it didn't. Looks like an issue. Which Symfony version do you use?
Related
Am getting below issue after switching phpunit version from 7.1 to 9.5.28
Failed asserting that exception of type "Error" matches expected exception "Exception".
expectExceptionMessageRegExp() is deprecated.
expectExceptionMessageRegExp() is deprecated in PHPUnit 8 and will be removed in PHPUnit 9. Use expectExceptionMessageMatches() instead.
Example :
function testResponse(){
$this->expectExceptionMessageRegExp("/Unable to emit response/");
}
Replace with :
function testResponse(){
$this->expectExceptionMessageMatches("/Unable to emit response/");
}
ubuntu#admin[enter image description here][1]:~/orocommerce-application$ symfony console oro:install -vvv --sample-data=y --application-url=https://127.0.0.1:8000 --user-name=admin --user-email=admin#example.com --user-firstname=John --user-lastname=Doe --user-password=admin --organization-name=Oro --timeout=0 --symlink --env=prod -n
In ArrayNode.php line 322:
[Symfony\Component\Config\Definition\Exception\InvalidConfigurationException]
Unrecognized option "amqp" under "oro_message_queue.transport". Available option is "dbal".
Exception trace:
at /home/ubuntu/orocommerce-application/vendor/symfony/config/Definition/ArrayNode.php:322
Symfony\Component\Config\Definition\ArrayNode->normalizeValue() at /home/ubuntu/orocommerce-application/vendor/symfony/config/Definition/BaseNode.php:410
Symfony\Component\Config\Definition\BaseNode->normalize() at /home/ubuntu/orocommerce-application/vendor/symfony/config/Definition/ArrayNode.php:287
Symfony\Component\Config\Definition\ArrayNode->normalizeValue() at /home/ubuntu/orocommerce-application/vendor/symfony/config/Definition/BaseNode.php:410
Symfony\Component\Config\Definition\BaseNode->normalize() at /home/ubuntu/orocommerce-application/vendor/symfony/config/Definition/Processor.php:32
Symfony\Component\Config\Definition\Processor->process() at /home/ubuntu/orocommerce-application/vendor/symfony/config/Definition/Processor.php:46
Symfony\Component\Config\Definition\Processor->processConfiguration() at /home/ubuntu/orocommerce-application/vendor/symfony/dependency-injection/Extension/Extension.php:111
Symfony\Component\DependencyInjection\Extension\Extension->processConfiguration() at /home/ubuntu/orocommerce-application/vendor/oro/platform/src/Oro/Bundle/MessageQueueBundle/DependencyInjection/OroMessageQueueExtension.php:38
Oro\Bundle\MessageQueueBundle\DependencyInjection\OroMessageQueueExtension->load() at /home/ubuntu/orocommerce-application/vendor/symfony/dependency-injection/Compiler/MergeExtensionConfigurationPass.php:76
Symfony\Component\DependencyInjection\Compiler\MergeExtensionConfigurationPass->process() at /home/ubuntu/orocommerce-application/vendor/symfony/http-kernel/DependencyInjection/MergeExtensionConfigurationPass.php:42
Symfony\Component\HttpKernel\DependencyInjection\MergeExtensionConfigurationPass->process() at /home/ubuntu/orocommerce-application/vendor/symfony/dependency-injection/Compiler/Compiler.php:82
Symfony\Component\DependencyInjection\Compiler\Compiler->compile() at /home/ubuntu/orocommerce-application/vendor/symfony/dependency-injection/ContainerBuilder.php:757
Symfony\Component\DependencyInjection\ContainerBuilder->compile() at /home/ubuntu/orocommerce-application/vendor/oro/platform/src/Oro/Bundle/DistributionBundle/OroKernel.php:407
Oro\Bundle\DistributionBundle\OroKernel->initializeContainer() at /home/ubuntu/orocommerce-application/vendor/symfony/http-kernel/Kernel.php:789
Symfony\Component\HttpKernel\Kernel->preBoot() at /home/ubuntu/orocommerce-application/vendor/symfony/http-kernel/Kernel.php:128
Symfony\Component\HttpKernel\Kernel->boot() at /home/ubuntu/orocommerce-application/vendor/oro/platform/src/Oro/Bundle/DistributionBundle/OroKernel.php:231
Oro\Bundle\DistributionBundle\OroKernel->boot() at /home/ubuntu/orocommerce-application/vendor/symfony/framework-bundle/Console/Application.php:168
Symfony\Bundle\FrameworkBundle\Console\Application->registerCommands() at /home/ubuntu/orocommerce-application/vendor/symfony/framework-bundle/Console/Application.php:74
Symfony\Bundle\FrameworkBundle\Console\Application->doRun() at /home/ubuntu/orocommerce-application/vendor/symfony/console/Application.php:171
Symfony\Component\Console\Application->run() at /home/ubuntu/orocommerce-application/bin/console:33
It seems you are trying to set up the amqp transport in config/parameters.yml, which is not available in the community edition of OroCommerce.
You have reset the message queue configuration to the default value that you can find in the config/parameters.yml.dist and clear the cache to apply changes
I'm trying to write very simple phpunit test
php artisan make:test CarTest
class CarTest extends TestCase
{
/**
* A basic feature test example.
*
* #return void
*/
public function testExample()
{
$response = $this->get('/car');
$response->assertStatus(200);
}
}
Now I run it
phpunit ./tests/Feature/CarTest.php
I get
Fatal error: Uncaught TypeError: Argument 2 passed to PHPUnit\Runner\BaseTestRunner::getTest() must be of the type string, array given, called in C:\Users\yasse\AppData\Roaming\Composer\vendor\phpunit\phpunit\src\TextUI\Command.php on line 125 and defined in C:\xampp\htdocs\lara6\vendor\phpunit\phpunit\src\Runner\BaseTestRunner.php:60
Stack trace:
#0 C:\Users\{username}\AppData\Roaming\Composer\vendor\phpunit\phpunit\src\TextUI\Command.php(125): PHPUnit\Runner\BaseTestRunner->getTest('C:\\xampp\\htdocs...', Array)
#1 C:\Users\{username}\AppData\Roaming\Composer\vendor\phpunit\phpunit\src\TextUI\Command.php(101): PHPUnit\TextUI\Command->run(Array, true)
#2 C:\Users\{username}\AppData\Roaming\Composer\vendor\phpunit\phpunit\phpunit(61): PHPUnit\TextUI\Command::main()
#3 {main}
I installed phpunit globally and installed it locally (v8) using composer and run composer dump-autoload but issue is not resolved. How to fix this and be able to run this test?
I tried to use PHPUnit v8. However I was not succeeded with PhpStorm. When I run simple test (class method) in PhpStorm I got the following message:
PHP Fatal error: Uncaught PHPUnit\Runner\Exception: Class 'Mrself\\TreeType\\Tests\\Functional\\BuildingTest' could not be found in '/vagrant/symfony-tree-type/tests/Functional/BuildingTest.php'. in /vagrant/symfony-tree-type/vendor/phpunit/phpunit/src/Runner/StandardTestSuiteLoader.php:65
Yes, I have that class and yes I have psr configured properly:
"autoload": {
"psr-4": {
"Mrself\\TreeType\\": "./src/"
}
},
"autoload-dev": {
"psr-4": {
"Mrself\\TreeType\\Tests\\": "./tests/"
}
}
The proof the I have everything correctly setup is that when I run vendor/bin/phpunit it gives me correct result.
When I run method in PhpStorm I got the following call:
/usr/bin/php /vagrant/symfony-tree-type/vendor/phpunit/phpunit/phpunit --configuration /vagrant/symfony-tree-type/phpunit.xml --filter "/(::testFormCanBeBuild)( .*)?$/" Mrself\\TreeType\\Tests\\Functional\\BuildingTest /vagrant/symfony-tree-type/tests/Functional/BuildingTest.php --teamcity
However if I prepend class namespace with \\ everything works correctly as well. I can not get a clue what's going on. PHPUnit version 7 works as well.
Same thing happened to me. All of the sudden I started getting the following error:
PHP Fatal error: Uncaught PHPUnit\Runner\Exception: Class 'Tests\\Feature\\ExampleTest' could not be found
And after I have read #frank-vue's comment I noticed the same thing and he did: If I run tests on the entire folder it runs normally, but if I run test on a specific class/method I get that error.
I tried earlier version of PHPStorm, downgraded PHP plugin etc... and nothing worked.
In my case, when I checked the stacktrace looks like:
#0 /var/www/vendor/phpunit/phpunit/src/Runner/BaseTestRunner.php(145): PHPUnit\Runner\StandardTestSuiteLoader->load('Tests\\\\Unit\\\\Ex...', '/var/www/tests/...')
#1 /var/www/vendor/phpunit/phpunit/src/Runner/BaseTestRunner.php(105): PHPUnit\Runner\BaseTestRunner->loadSuiteClass('Tests\\\\Unit\\\\Ex...', '/var/www/tests/...')
#2 /var/www/vendor/phpunit/phpunit/src/TextUI/Command.php(177): PHPUnit\Runner\BaseTestRunner->getTest('Tests\\\\Unit\\\\Ex...', '/var/www/tests/...', Array)
#3 /var/www/vendor/phpunit/phpunit/src/TextUI/Command.php(159): PHPUnit\TextUI\Command->run(Array, true)
#4 /var/www/vendor/phpunit/phpunit/phpunit(61): PHPUnit\TextUI\Command::main()
#5 {main}
thrown in /var/www/vendor/phpunit/phpunit/src/Runner/StandardTestSuiteLoader.php on line 69
Notice Tests\\\\Unit\\\\Ex... instead of Tests\\Unit\\Ex....
So in the end I broke the rule and I've modified vendor file, which should be avoided at any cost, but as a temporary solution it solves my problem.
So I added 1 line to the vendor/phpunit/phpunit/src/Runner/BaseTestRunner.php on line 98 (PHPUnit version 8.4.1), which replaces unnecessary '\'s.
if (empty($suiteClassFile) && \is_dir($suiteClassName) && !\is_file($suiteClassName . '.php')) {
/** #var string[] $files */
$files = (new FileIteratorFacade)->getFilesAsArray(
$suiteClassName,
$suffixes
);
$suite = new TestSuite($suiteClassName);
$suite->addTestFiles($files);
return $suite;
}
$suiteClassName = str_replace('\\\\', '\\', $suiteClassName); // THIS IS THE LINE I ADDED
try {
$testClass = $this->loadSuiteClass(
$suiteClassName,
$suiteClassFile
);
} catch (Exception $e) {
$this->runFailed($e->getMessage());
return null;
}
Currently, when writing a unit test for my Symfony2 bundle, I explicitly set ini_set('display_errors', 1) to make sure I see any errors I made in writing my unit test. This is particularly helpful for fatal errors, such as those resulting from typos in method names.
Example:
# src/Foo/BarBundle/Tests/Security/RoutePermissionCheckerTest.php
namespace Foo\BarBundle\Tests\Security;
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
class RoutePermissionCheckerTest extends TestCase
{
public function testIndex()
{
ini_set('display_errors', 1);
$this->nonExistentMethod();
}
}
Output:
Configuration read from /Users/maurits_dekkers/Sites/hobby/symfony/saas-seed/app/phpunit.xml.dist
...
Fatal error: Call to undefined method Bb\UserBundle\Tests\Security\RoutePermissionCheckerTest::nonExistentMethod() in /Users/maurits_dekkers/Sites/hobby/symfony/saas-seed/src/Bb/UserBundle/Tests/Security/RoutePermissionCheckerTest.php on line 18
Call Stack:
0.0314 635432 1. {main}() /Users/maurits_dekkers/pear/bin/phpunit:0
0.5501 1191328 2. PHPUnit_TextUI_Command::main() /Users/maurits_dekkers/pear/bin/phpunit:46
0.5502 1192056 3. PHPUnit_TextUI_Command->run() /Users/maurits_dekkers/pear/share/pear/PHPUnit/TextUI/Command.php:129
1.2241 7133440 4. PHPUnit_TextUI_TestRunner->doRun() /Users/maurits_dekkers/pear/share/pear/PHPUnit/TextUI/Command.php:176
1.2619 7653720 5. PHPUnit_Framework_TestSuite->run() /Users/maurits_dekkers/pear/share/pear/PHPUnit/TextUI/TestRunner.php:349
14.0128 37804208 6. PHPUnit_Framework_TestSuite->run() /Users/maurits_dekkers/pear/share/pear/PHPUnit/Framework/TestSuite.php:705
14.0130 37804608 7. PHPUnit_Framework_TestSuite->runTest() /Users/maurits_dekkers/pear/share/pear/PHPUnit/Framework/TestSuite.php:745
14.0130 37804608 8. PHPUnit_Framework_TestCase->run() /Users/maurits_dekkers/pear/share/pear/PHPUnit/Framework/TestSuite.php:775
14.0131 37804608 9. PHPUnit_Framework_TestResult->run() /Users/maurits_dekkers/pear/share/pear/PHPUnit/Framework/TestCase.php:783
14.0131 37805600 10. PHPUnit_Framework_TestCase->runBare() /Users/maurits_dekkers/pear/share/pear/PHPUnit/Framework/TestResult.php:648
14.0134 37846840 11. PHPUnit_Framework_TestCase->runTest() /Users/maurits_dekkers/pear/share/pear/PHPUnit/Framework/TestCase.php:838
14.0134 37848304 12. ReflectionMethod->invokeArgs() /Users/maurits_dekkers/pear/share/pear/PHPUnit/Framework/TestCase.php:983
14.0134 37848336 13. Bb\UserBundle\Tests\Security\RoutePermissionCheckerTest->testIndex() /Users/maurits_dekkers/pear/share/pear/PHPUnit/Framework/TestCase.php:983
However, there must be a better way by means of a configuration setting. I've tried adding an extra Monolog setting to my config_test.yml:
# app/config/config_test.yml
monolog:
handlers:
console:
type: console
bubble: false
level: info
but that does not result in PHP errors being reported during my unit test. The unit test simply stops running.
Is there an easy way to configure my Symfony2 project to always report errors during a unit test?
You might want to set the following parameters to true in your PHPUnit configuration file (app/config/phpunit.xml):
convertErrorsToException
convertNoticesToExceptions
convertNoticesToExceptions