php-solution/sf-functional-test

为使用Symfony功能测试提供额外功能

v6.0.0 2024-02-06 09:48 UTC

README

安装

$ composer require php-solution/sf-functional-test

从文件中加载环境变量

将扩展添加到您的phpunit.xml中并配置路径(从您的phpunit.xml配置文件中,以逗号分隔的相对文件路径)

<bootstrap class="PhpSolution\FunctionalTest\PhpUnit\Extension\PreRunEnvLoaderExtension">
    <parameter name="paths" value="../.env,.env"/>
</bootstrap>

在测试用例之前加载Doctrine数据集

将扩展添加到您的phpunit.xml中

<bootstrap class="PhpSolution\FunctionalTest\PhpUnit\Extension\PreRunCommandLauncherExtension">
     <parameter name="command" value="functional-test:fixtures:load"/>
    <!--Default is false. If true, if command's exit code > 0 then tests will fail immediately-->
    <parameter name="exitOnError" value="true" />
</bootstrap>

在测试用例之前运行Doctrine迁移

将扩展添加到您的phpunit.xml中

<bootstrap class="\PhpSolution\FunctionalTest\PhpUnit\Extension\DoctrineMigrationExtension" />

或者简单地

<bootstrap class="PhpSolution\FunctionalTest\PhpUnit\Extension\PreRunCommandLauncherExtension">
    <parameter name="command" value="doctrine:migration:migrate --no-interaction"/>
    <parameter name="exitOnError" value="true" />
</bootstrap>

使用参数运行sf命令

将扩展添加到您的phpunit.xml中

<bootstrap class="PhpSolution\FunctionalTest\PhpUnit\Extension\PreRunCommandLauncherExtension">
    <parameter name="command" value="doctrine:mongodb:schema:drop --collection"/>
</bootstrap>

使用参数运行本地命令

将扩展添加到您的phpunit.xml中

<bootstrap class="PhpSolution\FunctionalTest\PhpUnit\Extension\PreRunNativeCommandLauncherExtension">
    <parameter name="command" value="bin/console doctrine:mongodb:schema:drop --collection"/>
</bootstrap>

使用测试用例附加功能 PhpSolution\FunctionalTest\TestCase\AppTestCase

使用授权

  1. 添加到您的config_test.yml
security:
    firewalls:
        your_secured_category:
            http_basic: ~
  1. 在测试用例中使用
$client = $this->getAuthorizedClient('user_login', 'password');

与Doctrine(ORM、ODM)一起工作

  1. 将EntityTrait或DocumentTrait添加到您的测试用例中
$this->getDoctrine()
  1. 查找实体辅助方法
protected function findEntity(string $entityClass, string $orderBy = 'id', array $findBy = [])
protected function findDocument(string $documentClass, array $criteria = [])
protected function findDocuments(string $documentClass, array $criteria = [], array $orderBy = [])
  1. 刷新实体
protected function refreshEntity($entity) 
protected function refreshDocument($document)

测试电子邮件

  1. 添加配置
swiftmailer:
    disable_delivery: true
    spool:
        type: file
        path: '%kernel.project_dir%/var/spool'
    delivery_addresses: ~
  1. 添加SpoolTrait及其方法
public function purgeSpool()
public function getSpooledEmails()
public function getEmailContent($file)
protected function getSpoolDir()

正确项目结构的示例

有关功能测试的正确项目结构和配置,请参阅链接