2012-09-19 61 views
3

我想爲我的Silex應用程序編寫一些測試並遇到問題。Silex phpunit不匹配路線

我有以下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 
). 

有什麼明顯的毛病,我怎麼想編寫測試?

乾杯

+0

凡路由/包/搜索 – gunnx

+0

的代碼你有沒有使用貝哈特考慮? – umpirsky

+0

你有沒有想過這個?遇到同樣的問題。 – Lewis

回答

0

我遇到了同樣的問題。所有我必須做的(雖然我不喜歡這樣的解決方案)是使用requireinclude代替require_onceinclude_once