dbstudios/symfony-phpunit-helpers

1.0.1 2017-07-07 16:34 UTC

This package is auto-updated.

Last update: 2024-09-19 10:17:32 UTC


README

将以下内容添加到 app/AppKernel.php 文件中。

<?php
    // app/AppKernel.php
    
    // ...
    class AppKernel extends Kernel {
        public function registerBundles() {
            // ...
            if (in_array($this->getEnvironment(), array('dev', 'test'), true)) {
                // ...
                if ($this->getEnvironment() === 'test') {
                    $bundles[] = new Liip\FunctionalTestBundle\LiipFunctionalTestBundle();
                }
            }
    
            return $bundles;
        }
    
        // ...
    }

接下来,启用 Liip 扩展包。此外,您还应该配置 Doctrine 使用临时 SQLite 数据库,而不是您正常的数据库连接。

liip_functional_test:
    cache_sqlite_db: true

# Optional, but recommended; tells Doctrine to use a temporary SQLite database for testing
doctrine:
    dbal:
        driver: pdo_sqlite
        path: '%kernel.cache_dir%/test.db'

有关配置 Liip 扩展包的更多信息,请参阅 LiipFunctionalTestBundle 文档。

使用方法

简单地让您的测试用例继承自 DaybreakStudios\Utility\SymfonyPHPUnitHelpers\WebTestCase

<?php
    use DaybreakStudios\Utility\SymfonyPHPUntHelpers\WebTestCase;

    class MyTestCase extends WebTestCase {
    	public function testItRespondsSuccessfully() {
    	    $this->client->request('GET', '/home');
    	    
    	    $response = $this->client->getResponse();
    	    
    	    $this->isSuccessful($response);
    	}
    }