Symfony functional test does not work, but the same request works in the browser

I followed Symfony 's documentation on functional tests to write my first, but I have some problems. The answer I get through the browser works well:

Browser response

But when I run phpunit -c app/ in the shell, I get a crash.

1) AppBundle \ Tests \ Controller \ MeterAPIControllerTest :: testGetAllVariables Failed to claim that 500 matches are expected 200.

This is the code:

 <?php namespace AppBundle\Tests\Controller; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; class MeterAPIControllerTest extends WebTestCase { public function testGetAllVariables() { $client = static::createClient(); $crawler = $client->request( 'GET', '/meters/121/120/variables' ); // Assert a specific 200 status code $this->assertEquals(200, $client->getResponse()->getStatusCode()); } } 

If I try another test statement, I also get a failure.

 // Assert that the "Content-Type" header is "application/json" $this->assertTrue( $client->getResponse()->headers->contains( 'Content-Type', 'application/json' ) ); 

EDIT

When I run phpunit in app/logs/test.log , I get a PHP exception:

[2016-03-31 15:25:21] request.CRITICAL: Unaccepted PHP exception Doctrine \ Common \ Persistence \ Mapping \ MappingException: "Invalid mapping file 'AppBundle.Entity.EM2Meter.orm.yml' for class' AppBundle \ Entity \ EM2Meter ". At / Applications / MAMP / htdocs / iso 50k1_tst_symfony / vendor / doctrine / common / lib / Doctrine / Common / Persistence / Mapping / MappingException.php line 86 {" exception ":" [object] (Doctrine \ Common \ Persistence \ Mapping \ MappingException (code: 0): Invalid mapping file "AppBundle.Entity.EM2Meter.orm.yml" for class' AppBundle \ Entity \ EM2Meter. In / Applications / MAMP / htdocs / iso 50k1_tst_symfony / vendor / doctrine / common /lib/Doctrine/Common/Persistence/Mapping/MappingException.php:86) "} []

How can i fix this?

+6
source share
2 answers

You did not tell the symfony client to contact the localhost server on port 8000, its default value is 80.

When you instantiate your client, specify the host in this way.

 $client = static::createClient([], [ 'HTTP_HOST' => 'localhost:8000', ] ); 
+1
source

There seems to be a cache issue here. I find it good practice to clear the cache for the current environment before running tests and before running your project for functional tests:

$ php bin/console cache:clear --env=dev

$ php bin/console cache:clear --env=tests

0
source

All Articles