3

我正在尝试为我的 Silex 应用程序编写一些测试,但遇到了问题。

我有以下 phpunit.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<phpunit 
     bootstrap="./bootstrap.php"
     backupGlobals="false"
     backupStaticAttributes="false"
     colors="true"
     convertErrorsToExceptions="true"
     convertNoticesToExceptions="true"
     convertWarningsToExceptions="true"
     processIsolation="false"
     stopOnFailure="false"
     syntaxCheck="false"
>
    <testsuites>
        <testsuite name="Management Test Suite">
            <directory>./</directory>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist>
            <directory>../src/</directory>
        </whitelist>
    </filter>
</phpunit>

引导代码是

<?php

use Symfony\Component\HttpKernel\Client;

function getJSONResponse($app, Client $client, $url, $params = array())
{
    $params['test_key'] = $app['test_key'];
    $client->request('GET', $url, $params);
    $response = $client->getResponse();
    $data = json_decode($response->getContent(), true);
    return $data;
}

我的第一个测试文件如下

<?php

require_once $_SERVER['frog_docroot'] . '/www/vendor/autoload.php';

class DefaultTest extends Silex\WebTestCase
{
    public function createApplication()
    {
        return require $_SERVER['frog_docroot'] . '/www/src/app.php';
    }

    public function testInvalidUrlThrowsException()
    {
        $client = $this->createClient();
        $data = getJSONResponse($this->app, $client, '/some/url/that/does/not/exist');
        $this->assertContains('No route found for "GET /some/url/that/does/not/exist"', $data['message']);
    }
}

我的第二个是

<?php

require_once $_SERVER['frog_docroot'] . '/www/vendor/autoload.php';

class AnotherTest extends Silex\WebTestCase
{
    public function createApplication()
    {
        return require $_SERVER['frog_docroot'] . '/www/src/app.php';
    }

    public function testSearchReturnsResults()
    {
        $client = $this->createClient();
        $data = getJSONResponse($this->app, $client, '/packages/search', array(
            'search' => 'something',
            'offset' => 0,
            'limit' => 10,
        ));

        $this->assertSame(array(
            'data' => array(
                '1' => 'Some Package',
            ),
            'offset' => 0,
            'limit' => 10,
        ), $data);
    }
}

问题是,如果我单独运行测试,它们都会通过。

如果我将它们作为测试套件的一部分运行,则会引发异常

There was 1 failure:

1) AnotherTest::testSearchReturnsResults
Failed asserting that Array (
    'message' => 'No route found for "GET /packages/search"'
    'code' => 0
) is identical to Array (
    'data' => Array (
        '1' => 'Some Package'
    )
    'offset' => 0
    'limit' => 10
    'more' => false
).

我尝试编写测试的方式有什么明显错误吗?

干杯

4

1 回答 1

0

我遇到了同样的问题。我所要做的(尽管我不喜欢那个解决方案)就是使用requireandinclude而不是require_onceandinclude_once

于 2015-06-26T09:18:03.300 回答