Reputation: 11
I am using Symfony 4 and I have a functional test where I need to access to my database. But when this is executed:
$repository = $this->getDoctrine()->getRepository(User::class);
I have this output:
App\Tests\Controller\AppControllerTest::testPostSubmit Error: Call to undefined method App\Tests\Controller \AppControllerTest::getDoctrine()
This is my phpunit.xml.dist
:
<?xml version="1.0" encoding="UTF-8"?>
<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.1/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="vendor/autoload.php"
>
<php>
<ini name="error_reporting" value="-1" />
<env name="KERNEL_CLASS" value="App\Kernel" />
<env name="APP_ENV" value="test" />
<env name="APP_DEBUG" value="1" />
<env name="APP_SECRET" value="s$cretf0rt3st" />
<env name="SHELL_VERBOSITY" value="-1" />
<env name="DATABASE_URL" value="mysql://root:[email protected]:3306/haytest" />
<!-- define your env variables for the test env here -->
</php>
<testsuites>
<testsuite name="Project Test Suite">
<directory>tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>./src/</directory>
</whitelist>
</filter>
<listeners>
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener" />
</listeners>
</phpunit>
And this is my test (it's not done):
/**
* Test the submitting of posts
*/
public function testPostSubmit()
{
$client = static::createClient();
$repository = $this->getDoctrine()->getRepository(User::class);
if (!$this->getUser()) {
if (!$repository->find(1)) {
$user = new User;
$user->setFirstName('root');
$user->setLastName('root');
$user->setUsername('root');
$user->setEmail('[email protected]');
$user->setPassword(password_hash('root', PASSWORD_ARGON2I));
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
}
$loginpage = $client->request('GET', '/en/login');
$form = $loginpage->selectButton('submit')->form();
$form['username'] = 'root';
$form['password'] = 'root';
$client->submit($form);
}
// We request the app_index controller.
$crawler = $client->request('GET', '/en/');
// We verify that we have a 200 status code.
$this->assertEquals(200, $client->getResponse()->getStatusCode());
}
I really don't understand why it's not working...
Upvotes: 0
Views: 1906
Reputation: 356
If your test class inherits from KernelTestCase
, you can retrieve the EntityManager
object via the Kernel
class in the setup function:
/** @var \Doctrine\ORM\EntityManager */
private $entityManager;
public function setUp()
{
$kernel = self::bootKernel();
$this->entityManager = $kernel
->getContainer()
->get('doctrine')
->getManager();
}
If your class inherits from WebTestCase
, you can also retrieve the EntityManager
object via the Client
instance in the setup function:
/** @var \Doctrine\ORM\EntityManager */
private $entityManager;
public function setUp()
{
$client = static::createClient();
$this->entityManager = $client
->getContainer()
->get('doctrine')
->getManager();
}
Check out this part of the Symfony documentation for more information on testing Doctrine repositories.
Upvotes: 1