2016-02-01 59 views
2

我編寫了一個Silex應用程序,效果很好,現在我想用PHPUnit編寫一些函數測試。 我寫一個PHP類,如:PHPUnit無法找到我的Silex路由

<?php 
namespace foo\Tests; 

require __DIR__ . '/../vendor/autoload.php'; 

use Silex\WebTestCase; 
use Silex\Application; 

class WebAppTreeTest extends WebTestCase 
{ 
public function createApplication() 
{ 
    $app = new Application(); 
    return $app; 
} 

public function test404() 
{ 
    $client = $this->createClient(); 
    $client->request('GET', '/give-me-a-404'); 
    $this->assertEquals(404, $client->getResponse()->getStatusCode()); 
} 

public function testGet() 
{ 
    $client = $this->createClient(); 
    $crawler = $client->request('GET', '/id/123545'); 

    $this->assertTrue($client->getResponse()->isOk()); 
    $this->assertCount(1, $crawler->filter('Created')); 
} 
} 

我試圖與phpunit運行測試,但它返回一個失敗,第二次測試失敗。在調試了一下後,我發現PHPUnit無法獲得路線... 從我的大話中可以找到這條路線。

任何提示?

編輯:

我phpunit.xml文件:

<?xml version="1.0" encoding="UTF-8"?> 
<phpunit backupGlobals="false" 
    backupStaticAttributes="false" 
    colors="true" 
    convertErrorsToExceptions="true" 
    convertNoticesToExceptions="true" 
    convertWarningsToExceptions="true" 
    processIsolation="false" 
    stopOnFailure="false" 
    stopOnError="false" 
    stopOnIncomplete="false" 
    stopOnSkipped="false" 
    syntaxCheck="false" 
    bootstrap="app/bootstrap.php"> 
    <testsuites> 
     <testsuite name="FormOptimizer Test Suite"> 
      <directory>./tests/</directory> 
     </testsuite> 
    </testsuites> 
</phpunit> 
+0

您有任何重寫規則嗎?嘗試啓用調試以獲取更多信息。 –

+0

提供您的phpunit配置文件等 - [示例](http://silex.sensiolabs.org/doc/testing.html#configuration) –

+0

@Neok請參閱編輯。亞歷克斯:我怎麼能看到PHPunit使用的整個網址?我嘗試着使用和不使用重寫規則,相同的 – Xavier

回答

4

你的方法createApplication返回裸應用$app = new Application(); return $app;沒有路線等
嘗試初始化app,因爲它是在app/bootstrap.php或初始化從全局庫返回app

public function createApplication() 
{ 
    return $_GLOBALS['app']; 
} 
+0

'return $ GLOBALS ['app']; ' 爲我工作 – Vladtn