Symfony测试无法访问存储库

时间:2018-05-03 20:10:07

标签: symfony doctrine phpunit functional-testing symfony4

我正在使用Symfony 4,我有一个功能测试,我需要访问我的数据库。但是当执行此操作时:

$repository = $this->getDoctrine()->getRepository(User::class);

我有这个输出:

  

应用\测试\控制器\ AppControllerTest :: testPostSubmit   错误:调用未定义的方法App \ Tests \ Controller   \ AppControllerTest :: getDoctrine()

这是我的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:root@127.0.0.1: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>

这是我的考验(没有完成):

/**
 * 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('root@root.root');
            $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());
}

我真的不明白为什么它不起作用......

1 个答案:

答案 0 :(得分:0)

如果您的测试类继承自Show,您可以通过设置函数中的KernelTestCase类检索EntityManager对象:

Kernel

如果您的类继承自/** @var \Doctrine\ORM\EntityManager */ private $entityManager; public function setUp() { $kernel = self::bootKernel(); $this->entityManager = $kernel ->getContainer() ->get('doctrine') ->getManager(); } ,您还可以通过设置函数中的WebTestCase实例检索EntityManager对象:

Client

查看Symfony文档的this part,了解有关测试Doctrine存储库的更多信息。