phpunit - Silex phpunit 不匹配路由

标签 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
).

我编写测试的方式是否有明显的错误?

干杯

最佳答案

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

关于phpunit - Silex phpunit 不匹配路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12493164/

相关文章:

php - 当最初 stub 的方法不存在时,PHPUnit失败

php - 在 Twig View 中访问 "dot"命名空间服务

error-handling - Silex - 如何将请求 $request 传递给 $app->error(function (\Exception $e, $code) use ($app) {

composer-php - silex/silex 1.0.*@dev -> 未找到匹配的包

php - Symfony2 验证器,NotBlank 但允许 null

unit-testing - CakePHP2 测试登录()

php - 如何使 PHPUnit mock 在调用未配置的方法时失败?

Php Storm "Test framework quit unexpectedly"

sql - 如何使用 PHPUnit 使用 SQL 文件设置和拆除数据库?

symfony - Silex + FOQElasticaBundle