allan-simon / functionnal-test-helpers
一套辅助特质,帮助您使用phpunit为symfony2编写功能测试
Requires
- php: >=5.4.0
- phpunit/phpunit: ~4.4|~5.4
This package is not auto-updated.
Last update: 2024-09-28 17:54:36 UTC
README
此库为您提供了特质集,用于简化symfony2应用程序中REST API和普通HTML控制器功能测试的创建。它还尽可能确保在测试完成后关闭数据库连接(默认情况下,phpunit似乎不这样做)
安装
在您的composer.json
中添加
"require-dev": { ... "allan-simon/functionnal-test-helpers": "*", "liip/functional-test-bundle": "~1.0", ... }
然后,类可通过命名空间AllanSimon\TestHelpers
访问
文档
更完整的文档即将推出,在那之前,可以直接查看代码,代码已尽可能编写得清晰易懂。如果您有任何具体问题,可以开票,我们会尽快回答。
对于ApiHelpersTrait
目前,API假设除了二进制数据(图像等)外,您想要发送和接收JSON编码的数据
执行请求
所有这些方法都使用$this->client
执行请求,您(目前)负责根据您的需求创建它
请求执行后,原始响应将分配给$this->response
。如果内容是JSON,则解码后的内容将存储在$this->responseJson
performGET(string $uri)
向$uri发送GET请求performDELETE(string $uri)
向$uri发送DELETE请求performPOST(string $uri, array $data)
对$data进行json_encode并POST到URIperformPUT(string $uri, array $data)
对$data进行json_encode并PUT到URIperformPATCH(string $uri, array $data)
对$data进行json_encode并PATCH到URI
与数据固定值交互的方法
given(string $fixtureName)
加载由$fixtureName引用的实体,并将其设置在$this->entity中refreshEntity()
,与数据库重新同步/刷新$this->entity
中的实体
断言HTTP状态码
`
-
assertBadRequestError()
=> 400 -
assertPermissionError()
=> 401 -
assertPermissionDenied()
=> 403 -
assertNotFoundError()
=> 404 -
assertResponseUnprocessableEntity()
=> 422 -
assertOkSuccess()
=> 200 -
assertCreatedSuccess()
=> 201 -
assertNoContentResponse()
=> 203
断言返回JSON
所有这些断言都使用属性$this->responseJson
,它由以下内容填充
assertEmptyList
,检查由perform*
返回的JSON是一个包含0个元素的数组assertNotEmptyList
,检查由perform*
返回的JSON是一个包含1+个元素的数组assertResponseHasFields(array $fields)
检查返回的JSON是一个由参数中给出的至少包含这些字段的JSON对象assertListElementsHaveFields(array $fields)
检查JSON是一个数组,其中每个元素至少包含给定的字段
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] ); } } }
示例
正常控制器的使用
<?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.'
);
}
}
注意
方法getFirstElementByTestName('example')
获取具有属性
data-for-test-name="example"
目标是将所有基于data-for-test-*
属性的标识符和选择器放在一起,理由是这样您可以放心地修改id
、class
或tag
类型,而不用担心会破坏测试(没有什么比在重构CSS时破坏测试更令人愤怒了)
许可证
MIT
贡献
我们热烈欢迎贡献,无论是
- 功能请求
- 错误报告
- PR更正一个小错误
- PR添加一些功能
如果您想贡献但不知道如何下手,请随时创建一个issue,说明您想做什么,我们将尽力一步一步地引导您。