shortcodes/tests
Composer 安装命令:
composer require shortcodes/tests
包简介
This is test package for Laravel Application
README 文档
README
This is package of test tools to help test laravel app properly
Tools
Api CRUD testing
To test CRUD in your API you need to create test that extends Shortcodes\Tests\Blueprints\ApiCrudTest and provide model class in protected property
class ModelCrudTest extends ApiCrudTest
{
protected $model = Model::class;
}
By default test will perform assertions to test
- i_can_make_index_request_and_get_200_status
- i_can_make_show_request_and_get_200_status
- i_can_make_store_request_and_get_201_status
- i_can_make_update_request_and_get_200_status
- i_can_make_delete_request_and_get_204_status
It is crucial to remember that provided model must have factory with valid generated data.
If index method requires some query string parameters you can define it in class method
class ModelCrudTest extends ApiCrudTest
{
protected $model = Model::class;
public function getQueryStringParams(){
return [
'length' => 10,
'page' => 1
];
}
}
Form Request testing
To test Form Request in your API you need to create test that extends Shortcodes\Tests\Blueprints\FormRequestTest and provide request class in protected property
class IndexModelRequestTest extends FormRequestTest
{
protected $model = IndexModelRequest::class;
}
FormRequestTest class allow to use method to validate request like in the example
class IndexModelRequestTest extends FormRequestTest
{
protected $model = IndexModelRequest::class;
public function testValidRequest(){
$this->prepareRequest(['some_data'=>'value'])->assertValidRequest()
}
public function testInvalidRequest(){
$this->prepareRequest(['some_data'=>'value'])->assertInvalidRequest()
}
public function testInvalidParameterInRequest(){
$this->prepareRequest(['some_data'=>'value','invalid_parameter'=>'value'])
->assertInvalidRequest()
->assertInvalidParameter(['invalid_parameter'])
->assertInvalidParameter('invalid_parameter') //or without array
->assertInvalidParameter('missing_request_required_parameter')
}
public function testValidParameterInInvalidRequestRequest(){
$this->prepareRequest(['some_data'=>'value','invalid_parameter'=>'value'])
->assertInvalidRequest()
->assertInvalidParameter(['invalid_parameter'])
->assertValidParameter(['some_data'])
}
}
There is also possibility to test request whit required injected model like with query parameters.
class IndexModelRequestTest extends FormRequestTest
{
protected $model = UpdateModelRequest::class;
public function testTitleDuplicationRequest(){
$object = Model::first();
$this->prepareRequest(['title'=>$object->title'], $object)->assertValidRequest()
}
}
Api request testing
To test Api Request in you need to create test that extends Shortcodes\Tests\Blueprints\TestCase. This is a simple wrapper on standard method that allows to write less code.
$response = $this->apiRequest('POST', url('named.route.name'), $data, $headers);
Method create request that act as user defined in class user property or create one from factory. And attach header for authorization
X-App-Token which is taken from env('AUTH_KEY'),
shortcodes/tests 适用场景与选型建议
shortcodes/tests 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.67k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 11 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 shortcodes/tests 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 shortcodes/tests 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 2.67k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: mit
- 更新时间: 2019-11-15