2016-02-01 50 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