gpt4 book ai didi

phpunit - Silex phpunit 不匹配路由

转载 作者:行者123 更新时间:2023-12-03 03:58:32 35 4
gpt4 key购买 nike

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

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com