PHPUnit找不到我的Silex路线

时间:2016-02-01 15:28:57

标签: php phpunit silex

我编写了一个很好用的Silex应用程序代码,现在我想用PHPUnit编写一些函数测试。 我写了一个PHP类:

 <?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无法获得路由...... 从我的broswer tho可以到达那条路线。

任何提示?

修改

我的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>  

1 个答案:

答案 0 :(得分:4)

您的方法createApplication会返回没有路由等的裸应用$app = new Application(); return $app; 尝试初始化app,因为它在app/bootstrap.php中初始化或从全局变量中返回app

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