we-bridge / functionnal-test-helpers
一套辅助特性,帮助您使用phpunit为symfony2编写功能测试
Requires
- php: >=5.4.0
- phpunit/phpunit: ~4.4
This package is not auto-updated.
Last update: 2024-09-14 18:47:44 UTC
README
这个库为您提供一组特质,以便轻松创建symfony2应用程序的REST API和普通HTML控制器的功能测试。它还尽可能在测试完成后关闭数据库连接(默认情况下,phpunit似乎不这样做)
安装
在您的 composer.json 中添加
"require-dev": { ... "we-bridge/functionnal-test-helpers": "*", "liip/functional-test-bundle": "~1.0", ... }
然后,类可以从命名空间 WeBridge\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)对URI进行POST请求,并JSON编码数据performPUT(string $uri, array $data)对URI进行PUT请求,并JSON编码数据performPATCH(string $uri, array $data)对URI进行PATCH请求,并JSON编码数据
用于处理数据固定值的方法
given(string $fixtureName)加载由 $fixtureName 引用的实体,并将其设置为 $this->entityrefreshEntity(),在$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个元素的JSON数组assertNotEmptyList检查由perform*返回的JSON是具有1+个元素的JSON数组assertResponseHasFields(array $fields)检查返回的JSON是至少包含参数中给定字段的JSON对象assertListElementsHaveFields(array $fields)检查JSON是数组,其中每个元素至少包含给定的字段
用于REST API的用法
<?php namespace YourBundle\Tests\Controller; use WeBridge\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 WeBridge\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-* 属性的标识符和选择器放入其中,其合理性在于这样您可以修改而不用担心破坏测试(没有任何比破坏测试更令人沮丧的事情了,尤其是在重构CSS时)
许可证
MIT
贡献
我们热烈欢迎各种贡献,无论是
- 特性请求
- 错误报告
- PR纠正一个小错误
- PR添加一些功能
如果您想做出贡献但不知道如何下手,请随时创建一个问题,说明您想做什么,我们会尽力一步步引导您。