andreasindal / limerence
PHP 的 BDD 风格测试框架。
Requires
- php: >=5.3.0
This package is auto-updated.
Last update: 2024-08-28 17:56:23 UTC
README
Limerence 是一个受 BDD/TDD 启发的 PHP 测试库,它深受 mocha 和 chai 的启发。
安装
建议使用 Composer 进行安装。要将 Limerence 添加到您的项目中,请运行以下命令:
$ composer require --dev andreasindal/limerence
然后,要运行您的测试,请运行:
$ vendor/bin/limerence
或者,您可以全局安装 Limerence
$ composer global require andreasindal/limerence
全局安装 Limerence 后,您可以通过简单地输入 limerence
从项目的根目录轻松运行测试。
内容
用法
Limerence 预期您将所有测试放入项目根目录下名为 test 或 tests 的目录中,但您可以根据需要使用任意数量的子目录。
测试
Limerence 非常易于使用,您输入的每一行都会感觉非常自然。测试中主要使用以下四个函数:test()
、describe()
、it()
和 expect()
,并且它们都是相互配合使用的。
test()
test()
函数定义了测试套件的边界。
describe()
describe()
函数定义了测试用例的边界。
it()
it()
函数描述了测试的预期。
expect()
expect()
函数进行断言。
示例
Dog.php
namespace App\Animals; class Dog { public function makeSound() { return "Woof woof!"; } }
dogTest.php
require 'vendor/autoload.php'; use App\Animals\Dog; test('Dog model test', function () { describe('function makeSound()', function () { it('should make a barking sound', function () { $dog = new Dog(); expect($dog)->to->have->method('makeSound'); $bark = $dog->makeSound(); expect($bark)->to->be->a('string'); expect($bark)->to->equal('Woof woof!'); }); }); });
在这里,我们期望 Dog 对象具有名为 makeSound() 的方法,并且它返回一个等于 "Woof woof!" 的字符串。如果我们运行测试,我们会得到以下输出:
Limerence v0.0.1
Tests:
‾‾‾‾‾‾
Dog model test
function makeSound()
✔ should make a barking sound
1 tests (3 assertions) completed in 0.00 ms.
1 tests passing.
0 tests failing.
断言
Limerence 提供了各种断言。
equal(value)
expect($user->name)->to->equal('James');
be(value)
expect($user->name)->to->be->a('string');
Be 可以断言以下类型:string、number、int、integer、long、bool、boolean、float、double、real、object、array、callable、null、empty、file、writable。
还有一个 a
的别名,当预期以元音字母开头时,可以使预期看起来更美观。
expect($user->age)->to->be->an('integer');
如果 a/an 没有意义,您也可以直接调用 be($value)
。
expect($user->age)->to->be('null');
(请注意,expect($user->age)->to->be->a('integer')
和 expect($user->age)->to->be('integer')
都会正常工作。)
have(value)
可以使用 have 方法来断言对象的属性和方法。
expect($user)->to->have->property('username');
expect($user)->to->have->method('login');
contain(value|values)
Contain 可以用来检查数组是否包含某些值或键。
expect($user->hobbies)->to->contain('Fishing');
这相当于
expect($user->hobbies)->to->contain->value('Fishing');
您也可以通过调用来检查键
expect($user->skills)->to->contain->key('Programming');
Contain 也可以通过传递一个数组作为参数来检查多个键/值。
expect($user->hobbies)->to->contain(['Fishing', 'Knitting', 'Sports']);
否定
可以通过在断言链中添加 not
来否定所有断言,并且将按预期行为。例如
expect($user->name)->to->not->be->a('number');
或者
expect($user->name)->to->not->equal('John Doe');
HTTP 请求
沉迷工具还包含了测试HTTP请求的功能,这在测试REST API时尤其有用。协议默认为http,主机名默认为localhost,端口默认为80,请求方法默认为GET。示例
request(method[, endpoint])
test('/users', function () { describe('GET', function () { it('should return a list of users', function () { request('GET', '/users') ->expect(200) ->end(function ($err, $res) { if ($err) return; expect($res->body)->to->have->property('success'); expect($res->body->success)->to->be->a('boolean'); expect($res->body->success)->to->equal(true); expect($res->body)->to->have->property('users'); expect($res->body->users)->to->be->an('array'); }); }); }); });
expect(status)
expect函数用于对响应状态码进行断言。
get('/admin') ->expect(403) // Expects the server to respond with 403 Forbidden ->end(function ($err, $res) { // ... });
protocol(protocol)
设置请求的协议。可以是http或https。默认为http。
get('/users') ->protocol('https') // Sets the protocol to https ->expect(200) ->end(function ($err, $res) { // ... });
at(hostname)
设置请求的目标主机名。默认为localhost。
get('/users') ->at('myapp.dev') // Sets the hostname to myapp.dev ->expect(200) ->end(function ($err, $res) { // ... });
on(port)
设置请求的目标端口。默认为80。
get('/users') ->on(3000) // Sets the port to 3000 ->expect(200) ->end(function ($err, $res) { // ... });
简写助手
还有简写函数用于GET、POST、PUT、PATCH和DELETE请求,例如
get('/users') ->expect(200) ->end(function ($err, $res) { // ... });
send(data[, json])
要发送数据,请使用send函数。它接受一个包含数据键/值对的数组,以及一个可选的标志,告诉Limerence将数据作为JSON发送。如果您将json设置为true,它将自动为您包含一个Content-type: application/json
头部。示例
post('/users') ->expect(201) ->send([ 'username' => 'johnny', 'email' => 'john.smith@example.com', 'password' => 'password123', ]) // no json flag, data will be sent as normal form data ->end(function ($err, $res) { // ... });
put('/users/15') ->expect(200) ->send([ 'firstname' => 'James', 'lastname' => 'Smith', ], true) // Data will be sent as JSON ->end(function ($err, $res) { // ... });
with(header, value)
要给请求附加一个头部,只需使用with
函数。它需要一个键和一个值,例如
delete('/users/15') ->expect(200) ->with('Authorization', 'Bearer eyJhbGciOiJIUzI1N...') ->end(function ($err, $res) { // ... });
end(callback)
当调用end
方法时,请求会被发送,该方法接受一个回调函数。回调函数将传递两个变量,第一个包含发生的任何错误,第二个是响应对象。在回调函数内部,您应该放置所有的断言。
许可证
MIT。