allan-simon/functionnal-test-helpers
Composer 安装命令:
composer require --dev allan-simon/functionnal-test-helpers
包简介
A suite of helpers traits to help you writing functionnal tests with phpunit for symfony2
README 文档
README
This library offer you a set of Traits to ease the creation of functionnal tests for both REST api and normal HTML controllers of your symfony2 application. It also take care as much as possible to close the database connections after the tests are done (which by default phpunit seems not to do)
Installation
add in your composer.json :
"require-dev": { ... "allan-simon/functionnal-test-helpers": "*", "liip/functional-test-bundle": "~1.0", ... }
Then the classes are accessible from the namespace AllanSimon\TestHelpers
Documentation:
A more complete documentation is to come, waiting for that one can directly check the code, which has been made to be as clear as possible, if you got any specific question, you can open a ticket, we're quite fast to answer.
For ApiHelpersTrait
For the moment the Api go with the assumption that except for binary data (images etc.), you want to send and receive JSON encoded data
Perform requests
all these methods perform the request using $this->client and it's your responsability
(for the moment) to create it to fit your needs
Once the Request is performed, the raw response is assigned in $this->response if the content
is json, the decoded content will in $this->responseJson
performGET(string $uri)GET request to $uriperformDELETE($string $uri)DELETE request to $uriperformPOSt(string $uri, array $data)json_encode the $data and POST it to the URIperformPUT(string $uri, array $data)json_encode the $data and PUT it to the URIperformPATCH(string $uri, array $data)json_encode the $data and PATCH it to the URI
Methods to play with data fixtures
given(string $fixtureName), load the entity referenced by $fixtureName and set it in $this->entityrefreshEntiy(), resync/refresh the entity in$this->entitywith the database
Assert HTTP status code
`
-
assertBadRequestError()=> 400 -
assertPermissionError()=> 401 -
assertPermissionDenied()=> 403 -
assertNotFoundError()=> 404 -
assertResponseUnprocessableEntity()=> 422 -
assertOkSuccess()=> 200 -
assertCreatedSuccess()=> 201 -
assertNoContentResponse()=> 203
Assert JSON returned
all these assets use the property $this->responseJson, which is populated by
assertEmptyList, check the json returned by aperform*is a json Array with 0 elementassertNotEmptyList, check the json returned by aperform*is a json Array with 1+ elementassertResponseHasFields(array $fields)check that the json returned is a json object made of at least the fields given in parametersassertListElementsHaveFields(array $fields)check the json is an array for which every single element contains at least the given fields
Usage For REST Api
<?php namespace YourBundle\Tests\Controller; use AllanSimon\TestHelpers\ApiHelpersTrait; use Liip\FunctionalTestBundle\Test\WebTestCase; class AnalystControllerTest extends WebTestCase { use ApiHelpersTrait; private static $ANALYST_FIELDS = [ 'id', 'title', 'job_title', 'biography', 'registration_code', 'videos', 'subscribed', 'users_subscribed', ]; private $analyst = null; public function setUp() { $this->client = static::createClient(); } public function testGetAnalystsWithoutSortReturnBadParameter() { $this->performGET('/api/analysts'); $this->assertBadRequestError(); } public function testGetAnalystsWithdValidSortReturnListAnalysts() { //TODO replace by phpunit stuff to feed with data foreach (['hot', 'recommended', 'newest'] as $validSort) { $this->performGET('/api/analysts?sort='.$sort) $this->assertOkSuccess(); $this->assertArrayHasKeys( self::$ANALYST_FIELDS, $this->responseJson[0] ); } } }
Example
Usage For normal Controllers
<?php
namespace YourBundle\Tests\Controller;
use Liip\FunctionalTestBundle\Test\WebTestCase;
use AllanSimon\TestHelpers\IsControllerTestTrait;
class CommentsControllerTest extends WebTestCase
{
use IsControllerTestTrait;
const COMMENT_OF_VIDEO_PAGE = '/backend/comments/of-video/';
private $video;
// if you don't use any fixtures declare this array as empty
// in latter version it will not be needed to declare it if not used
protected $fixturelist = [
'YourBundle\DataFixtures\ORM\LoadCommentData',
'YourBundle\DataFixtures\ORM\LoadBackendCommentData',
];
public function testOpenCommentOfVideoPageShouldHaveAlistOfComment()
{
$this->givenVideo('commented-video');
$this->openCommentOfVideoPage();
$this->assertPageOpenedSuccessfully();
$this->assertListofCommentsPresents();
}
// conveniency methods
private function givenVideo($fixturesName)
{
$video = $this->fixtures->getReference($fixturesName);
$this->video = $video;
}
private function openCommentOfVideoPage()
{
$id = $this->video->getId();
$this->openPage(self::COMMENT_OF_VIDEO_PAGE."$id");
}
// assert
private function assertListofCommentsPresents()
{
$this->assertEquals(
1,
$this->getFirstElementByTestName('table-comments-list')->count(),
'The table containing the list of comments was not found.'
);
$this->assertEquals(
1,
$this->getFirstElementByTestName('comments-list-record')->count(),
'There is no comment listed.'
);
}
}
Note
The method getFirstElementByTestName('example') takes the first HTML tag with the attribute
data-for-test-name="example"
the goal being to put all identifier and selectors based on data-for-test-* attributes, the rationnal being
that this way you can modify without fear your id and class or tag types without breaking your tests
(and there's nothing more raging than to break tests because you refactorize the CSS)
License
MIT
Contributing
Contributions are warmly welcomed, be it
- Feature requests
- Bug reports
- PR correcting one little typo
- PR adding some functionnalities
if you would like to contribute but have no clue on how to do it feel free to open an issue explaining what you want to do, and we will try to guide you step by step.
allan-simon/functionnal-test-helpers 适用场景与选型建议
allan-simon/functionnal-test-helpers 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.52k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2015 年 05 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「Symfony2」 「testing」 「phpunit」 「test」 「traits」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 allan-simon/functionnal-test-helpers 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 allan-simon/functionnal-test-helpers 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 allan-simon/functionnal-test-helpers 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
HiDev plugin for PHPUnit
Testing Suite For Lumen like Laravel does.
A cli tool which generates unit tests.
The PHP SDK for Checkmango
PHPUnit DbUnit Native Array-based Fixtures
Symfony2 Barcode Generator Bundle with Twig function extension
统计信息
- 总下载量: 1.52k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 21
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-05-24