php-solution/sf-functional-test
最新稳定版本:v6.0.5
Composer 安装命令:
composer require php-solution/sf-functional-test
包简介
Extra functionality for work with Symfony functional tests
README 文档
README
Install
$ composer require php-solution/sf-functional-test
Load environment variables from files
Add to your phpunit.xml extension and configure paths (comma separated relative file paths from your phpunit.xml configuration file):
<bootstrap class="PhpSolution\FunctionalTest\PhpUnit\Extension\PreRunEnvLoaderExtension"> <parameter name="paths" value="../.env,.env"/> </bootstrap>
Load Doctrine fixtures before test cases
Add to your phpunit.xml extension:
<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>
Run Doctrine migrations before test cases
Add to your phpunit.xml extension:
<bootstrap class="\PhpSolution\FunctionalTest\PhpUnit\Extension\DoctrineMigrationExtension" />
Or simply:
<bootstrap class="PhpSolution\FunctionalTest\PhpUnit\Extension\PreRunCommandLauncherExtension"> <parameter name="command" value="doctrine:migration:migrate --no-interaction"/> <parameter name="exitOnError" value="true" /> </bootstrap>
Run sf command with parameters
Add to your phpunit.xml extension:
<bootstrap class="PhpSolution\FunctionalTest\PhpUnit\Extension\PreRunCommandLauncherExtension"> <parameter name="command" value="doctrine:mongodb:schema:drop --collection"/> </bootstrap>
Run native command with parameters
Add to your phpunit.xml extension:
<bootstrap class="PhpSolution\FunctionalTest\PhpUnit\Extension\PreRunNativeCommandLauncherExtension"> <parameter name="command" value="bin/console doctrine:mongodb:schema:drop --collection"/> </bootstrap>
Using Test case additional functionallity PhpSolution\FunctionalTest\TestCase\AppTestCase
Using Authorization:
- Add to your config_test.yml:
security: firewalls: your_secured_category: http_basic: ~
- Use on TestCase
$client = $this->getAuthorizedClient('user_login', 'password');
Use profiler for testing
- Add to your framework.yml:
when@test: framework: profiler: collect: false
- Add ProfilerTrait to your TestCase
use PhpSolution\FunctionalTest\TestCase\ProfilerTrait;
- Use the following methods to run request with profiler:
[$response, $profiler] = self::withRequestProfiler(static function () { return self::createSystemAuthorizedTester() ->setExpectedStatusCode(Response::HTTP_OK) ->sendGet('/some/awesome/endpoint', ['foo' => 'bar']); });
By default, the following collectors are enabled: 'db', 'http_client', 'cache', 'memory'
but you can always disable or enable new collectors by passing them in the withRequestProfiler method:
[$response, $profiler] = self::withRequestProfiler(static function () { return self::createSystemAuthorizedTester() ->setExpectedStatusCode(Response::HTTP_OK) ->sendGet('/some/awesome/endpoint', ['foo' => 'bar']); }, ['db', 'http_client']);
- Use profiler to get collectors:
self::getCollector($profiler, 'http_client');
Work with Doctrine (ORM, ODM)
- Add EntityTrait or DocumentTrait to your TestCase
$this->getDoctrine()
- Find Entity helper method:
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 = [])
- Refresh Entity:
protected function refreshEntity($entity) protected function refreshDocument($document)
Assert doctrine queries using request profiler
- Make sure you have set up the profiler as described above.
- Add ProfilerTrait and EntityProfilerTrait to your TestCase:
use PhpSolution\FunctionalTest\TestCase\ProfilerTrait; use PhpSolution\FunctionalTest\TestCase\EntityProfilerTrait;
- Assert queries using profiler:
self::getDoctrineCollector($profiler)->getQueries(); // returns array of executed queries self::assertDoctrineQueriesCount(8, $profiler); self::assertDoctrineQueriesCountLessThanOrEqual(3, $profiler); self::assertDoctrineSelectQueriesCount(2, $profiler); self::assertDoctrineSelectQueriesCountLessThanOrEqual(2, $profiler); self::assertDoctrineUpdateQueriesCount(1, $profiler); self::assertDoctrineUpdateQueriesCountLessThanOrEqual(1, $profiler); self::assertDoctrineInsertQueriesCount(1, $profiler); self::assertDoctrineInsertQueriesCountLessThanOrEqual(1, $profiler); self::assertDoctrineDeleteQueriesCount(1, $profiler); self::assertDoctrineDeleteQueriesCountLessThanOrEqual(1, $profiler);
Test emails
- Add config
swiftmailer: disable_delivery: true spool: type: file path: '%kernel.project_dir%/var/spool' delivery_addresses: ~
- Add SpoolTrait and find methods
public function purgeSpool() public function getSpooledEmails() public function getEmailContent($file) protected function getSpoolDir()
Example of correct project structure:
See correct project structure and configs for functional tests on link
统计信息
- 总下载量: 73.31k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 1
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-04-05