我尝试使用 Symfony 4 和 phpunit 6 进行一些测试,但出现错误消息:



namespace App\Tests;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class RegistrationControllerTest extends WebTestCase
{
    public function testSomething()
    {
        $client = static::createClient();
        $crawler = $client->request('GET', '/');

        $this->assertSame(200, $client->getResponse()->getStatusCode());
        $this->assertSame(0, $crawler->filter('html:contains("Hello World")')->count());
    }

    public function testCheckPassword(){
        $client = static::createClient();

        $crawler = $client->request(
            'GET',
            '/register'
        );

        $form = $crawler->selectButton('S\'inscrire')->form();

        $form['user[email]'] = '[email protected]';
        $form['user[username]'] = 'usernametest';
        $form['user[fullName]'] = 'John Doe';
        $form['user[password][first]'] = 'pass1';
        $form['user[password][second]'] = 'pass2';

        $crawler = $client->submit($form);

        //echo $client->getResponse()->getContent();


        $this->assertEquals(1,
            $crawler->filter('li:contains("This value is not valid.")')->count()
        );


    }

}


我的环境文件:



此应用程序在开发环境中运行良好

谢谢 !

[编辑]
只需添加 phpunit.xml.dist

<env name="DATABASE_URL" value="mysql://root:@127.0.0.1/userDemo" />


谢谢@dbrumann

最佳答案

文档解释了如何在 Changing Database Settings for Functional Tests 中很好地解决您的问题:

关于Symfony 4 PHPunit 没有选择数据库,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48085404/

10-16 10:53