Silex phpunit不匹配路由

时间:2012-09-19 10:45:43

标签: phpunit silex

我正在尝试为我的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
).

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

干杯

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题。我所要做的就是(虽然我不喜欢这个解决方案)是使用requireinclude代替require_onceinclude_once