petrknap/nette-bootstrap

此包已被放弃,不再维护。作者建议使用 netpromotion/symfony-up 包。

Nette Bootstrap 和 PHPUnit 测试用例

v1.2.0 2016-11-12 11:06 UTC

This package is not auto-updated.

Last update: 2022-02-01 13:02:32 UTC


README

容器

基础引导类名称自解释,自然使用即可。

Bootstrap::getContainer()->getByType("Nette\\Application\\Application")->run();

测试

单元测试的特殊配置

如果您需要为单元测试进行特殊配置(例如,不同的数据库连接),您可以直接修改 Bootstrap::getConfigFiles 方法。

<?php

class Bootstrap extends \PetrKnap\Nette\Bootstrap\Bootstrap
{
    protected function getConfigFiles()
    {
        return array(
            __DIR__ . "/cfg/config.neon",
            self::getOption(self::OPTION_IS_TEST_RUN) ? __DIR__ . "/cfg/test.neon" : __DIR__ . "/cfg/local.neon"
        );
    }
}

如果您需要不同的日志目录等,也可以以相同的方式修改任何其他方法。

测试用例

已准备好的 NetteTestCase,是您单元测试的基础类。此类需要定义常量 NETTE_BOOTSTRAP_CLASS,您可以在您的 phpunit.xml 中定义它

<phpunit>
    <php>
      <const name="NETTE_BOOTSTRAP_CLASS" value="Bootstrap"/>
    </php>
</phpunit>

或者在您自己的测试用例中定义

<?php

class NetteTestCase extends \PetrKnap\Nette\Bootstrap\PhpUnit\NetteTestCase
{
    const NETTE_BOOTSTRAP_CLASS = "Bootstrap";
}

容器访问

在测试中使用方法 NetteTestCase::getContainer 访问应用程序容器。

演示者测试

测试用例模拟 Application\Request 而不是 Http\Request

对于演示者测试,请使用方法 NetteTestCase::runPresenter

<?php

class NetteTestCase extends \PetrKnap\Nette\Bootstrap\PhpUnit\NetteTestCase
{
    public function testHelloWorld()
    {
        /** @var \Nette\Application\Responses\TextResponse $response */
        $response = $this->runPresenter("World", "hello"); // calls WorldPresenter::actionHello
        $html = (string) $response->getSource(); // renders output
        $this->assertContains("Hello, world!", $html);
    }
}

如何安装

运行 composer require petrknap/nette-bootstrap 或手动将此 JSON 代码合并到您的项目 composer.json 文件中,然后运行 composer install。您可以使用 已发布的版本之一 而不是 dev-master

{
    "require": {
        "petrknap/nette-bootstrap": "dev-master"
    }
}

或者通过 git clone https://github.com/petrknap/nette-bootstrap.git 手动克隆此存储库,或者下载 此存储库作为 ZIP 并将文件提取到您的项目中。