【问题标题】:symfony : can't run functional testssymfony:无法运行功能测试
【发布时间】:2020-01-08 08:00:38
【问题描述】:

版本: Symfony 3.4 phpunit 8.3

我的 phpunit.xml 文件:

<?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/4.8/phpunit.xsd"
         backupGlobals="false"
         colors="true"
         bootstrap="autoload.php">
    <php>
        <ini name="error_reporting" value="-1" />
        <env name="SYMFONY_DEPRECATIONS_HELPER" value="weak" />
        <!--
            <server name="KERNEL_DIR" value="/path/to/your/app/" />
        -->
    </php>

    <testsuites>
        <testsuite name="Project Test Suite">
            <directory>../src/*/*Bundle/Tests</directory>
            <directory>../src/*/Bundle/*Bundle/Tests</directory>
            <directory>../src/*Bundle/Tests</directory>
        </testsuite>
    </testsuites>

    <filter>
        <whitelist>
            <directory>../src</directory>
            <exclude>
                <directory>../src/*Bundle/Resources</directory>
                <directory>../src/*Bundle/Tests</directory>
                <directory>../src/*/*Bundle/Resources</directory>
                <directory>../src/*/*Bundle/Tests</directory>
                <directory>../src/*/Bundle/*Bundle/Resources</directory>
                <directory>../src/*/Bundle/*Bundle/Tests</directory>
            </exclude>
        </whitelist>
    </filter>
</phpunit>

我的测试文件:

class QuittancementControllerTest extends WebTestCase
{
    private $client;
    /** @var EntityManager */
    private $em;
    private $myApiUrl;
    /** @var BailRepository */
    private $bailRepo;
    /** @var EcritureCompteLocataireRepository */
    private $ecritureRepo;

    public function setUp(): void
    {
        $this->client = static::createClient([], [
            'PHP_AUTH_USER' => 'testUser',
            'PHP_AUTH_PW' => 'pa$$word',
        ]);
        $this->myApiUrl = $this->client->getContainer()->getParameter('myApi_url');
        $this->client->disableReboot();
        $this->em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
        $this->em->beginTransaction();
        $this->ecritureRepo = $this->em->getRepository('AppBundle:EcritureCompteLocataire');
        $this->bailRepo = $this->em->getRepository('AppBundle:Bail');
    }

    public function run(TestResult $result = null): TestResult
    {
        $this->setPreserveGlobalState(false);
        return parent::run($result);
    }

    public function tearDown(): void
    {
        $this->em->rollback();
    }

    /**
     * Vérifie que l'insertion d'une écriture à la date du jour met bien à jour le solde des écritures qui suivent
     * @runInSeparateProcess
     */
    public function testMajSoldeEcritureFuture(): void
    {
        /** @var Bail $bail */
        $bail = $this->bailRepo->findOneBy(['numeroCompteTiers' => '9000001']);
        $postData = array
        (
            'bail' => $bail->getId(),
            'test' => 'example',
        );
        $this->client->request(Request::METHOD_POST, $this->myApiUrl.'/api/test', $postData);
        $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
        $solde = $this->ecritureRepo->getSolde($bail->getId(), new DateTime());

        // voluntary false assertion
        $this->assertEquals(111, 10);

    }
}

如果我像这样运行测试,我有这个错误:

{"code":500,"message":"'SimpleXMLElement' 的序列化是不允许的"} C:\wamp64\www\chronos2017\src\tests\AppBundle\Controller\QuittancementControllerTest.php:44

statut 200 的断言是 write,第二个断言是 false,但 phpunit 无法正确显示结果

如果我删除“@runInSeparateProcess”行,客户端调用失败:

{"code":500,"message":"无法启动会话,因为标头已由 \"C:\wamp64\www\mySite\src\vendor\phpunit\phpunit\src\Util\ 发送Printer.php\" 在第 109 行。"}

用 symfony 进行功能测试的正确方法是什么?你能帮帮我吗?

【问题讨论】:

    标签: symfony phpunit


    【解决方案1】:

    我也使用 symfony 3.4 但 phpunit 7 所以可能是因为 phpunit 8,但我不需要对我的 phpunit.xml 文件进行任何修改。根据错误,您的错误似乎来自您的xml文件。

    我猜你已经使用过文档,但如果没有:https://symfony.com/doc/3.4/testing.html

    我的文件:

    <?xml version="1.0" encoding="UTF-8"?>
    <phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:noNamespaceSchemaLocation="phpunit.xsd"
             bootstrap="tests/bootstrap.php"
             cacheResult="true"
             verbose="true">
        <testsuites>
            <testsuite name="unit">
                <directory suffix="Test.php">tests/unit</directory>
            </testsuite>
    
            <testsuite name="end-to-end">
                <directory suffix=".phpt">tests/end-to-end</directory>
            </testsuite>
        </testsuites>
    
        <filter>
            <whitelist processUncoveredFilesFromWhitelist="true">
                <directory suffix=".php">src</directory>
                <exclude>
                    <file>src/Framework/Assert/Functions.php</file>
                    <file>src/Util/PHP/eval-stdin.php</file>
                </exclude>
            </whitelist>
        </filter>
    
        <php>
            <const name="PHPUNIT_TESTSUITE" value="true"/>
        </php>
    </phpunit>
    

    【讨论】:

    • phpunit7.5.9 和从您的示例构建的配置存在相同问题。我想我将不得不开始一个新项目并逐步添加组件以查看问题所在。
    • 您可以尝试一个非常简单的示例,例如: $client = static::createClient(); $crawler = $client->request('GET', '/'); $this->assertTrue($client->getResponse()->isSuccessful(), '响应状态为2xx');
    猜你喜欢
    • 2021-02-06
    • 1970-01-01
    • 2012-12-29
    • 1970-01-01
    • 1970-01-01
    • 2016-08-05
    • 2022-01-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多