speicher210/functional-test-bundle
Composer 安装命令:
composer require speicher210/functional-test-bundle
包简介
Symfony bundle for functional testing
README 文档
README
Introduction
This Bundle provides base classes and functionality for writing and running functional tests with focus on testing REST endpoint. It provides help in setting up test database and loading fixtures and mocking services in DI (even private services).
Installation
Download the Bundle
$ composer require --dev speicher210/functional-test-bundle
Enable the Bundle
Symfony
<?php // config/bundles.php return [ // ... Speicher210\FunctionalTestBundle\Speicher210FunctionalTestBundle::class => ['dev' => true, 'test' => true], // ... ];
Basic usage
Bootstrap for PHPUnit:
<?php declare(strict_types=1); require dirname(__DIR__) . '/config/bootstrap.php'; require_once dirname(__DIR__) . '/vendor/speicher210/functional-test-bundle/src/Test/bootstrap.php'; // $kernel variable will contain the current Kernel instance
<?php declare(strict_types=1); use Speicher210\FunctionalTestBundle\Test\RestControllerWebTestCase; use Symfony\Component\HttpFoundation\Request; final class MyUserEndpointTest extends RestControllerWebTestCase { public function testReturn404IfUserIsNotFound() : void { $this->assertRestRequestReturns404('/api/user/1', Request::METHOD_GET); } public function testReturns200AndUserData() : void { $this->assertRestGetPath('/api/user/1'); } }
The assertions are done using snapshots for comparison.
By default the framework will look under Expected directory (from where the test class is located)
for a file with the same name as the test and suffixed with -1.json.
Example of expected output can be:
Expected/testReturns200AndUserData-1.json
{
"id": "1",
"first_name": "John",
"last_name": "Doe",
"email": "@string@",
"sign_up_date_time": "@string@.isDateTime()"
}
The -1.jsonsuffix will be incremented for every REST assertion made under one test.
In the expected any functionality from coduo/php-matcher can be used.
It is possible to automatically update content of expected files during test execution to the actual content by adding this extension to your phpunit config:
<extensions> <extension class="Speicher210\FunctionalTestBundle\Extension\RestRequestFailTestExpectedOutputFileUpdater" /> </extensions>
Fixtures are loaded using Doctrine fixtures.
By default the framework will look by default under Fixtures directory (from where the test class is located)
for a PHP file with the same name as the test. This file must return an array of class names that extend
Speicher210\FunctionalTestBundle\Test\Loader\AbstractLoader class.
Example of fixture file can be:
<?php // Fixtures/testReturns200AndUserData.php return [ \App\Tests\Fixtures\Loader\LoadOneUser::class ];
In order to rebuild and reset the database you need to create a bootstrap file for PHPUnit.
In your own bootstrap file you can include the file Test/bootstrap.php which will reset the test database.
The database will not be rebuild for every test, but only once when the tests start.
This means that data must be removed before running next test. This can be achieved by running tests in a transaction.
For this add the extension to your phpunit config:
<extensions> <extension class="DAMA\DoctrineTestBundle\PHPUnit\PHPUnitExtension" /> </extensions>
Accessing and mocking services
Mocking services can be achieved by using
<?php $this->mockContainerService('my_service_id', $myServiceMock);
Services can also be accessed by using
<?php $this->getContainerService('my_service_id');
speicher210/functional-test-bundle 适用场景与选型建议
speicher210/functional-test-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 65.72k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2016 年 07 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 speicher210/functional-test-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 speicher210/functional-test-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 65.72k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 10
- 依赖项目数: 2
- 推荐数: 2
其他信息
- 授权协议: MIT
- 更新时间: 2016-07-15