1
1
Specify
2
2
=======
3
3
4
- BDD style code blocks for PHPUnit / Codeception
4
+ BDD style code blocks for [ PHPUnit] [ 1 ] or [ Codeception] [ 2 ]
5
5
6
- [ ![ Latest Stable Version] ( https://poser.pugx.org/codeception/specify/v/stable )] ( https://packagist.org/packages/codeception/specify ) [ ![ Total Downloads] ( https://poser.pugx.org/codeception/specify/downloads )] ( https://packagist.org/packages/codeception/specify ) [ ![ Latest Unstable Version] ( https://poser.pugx.org/codeception/specify/v/unstable )] ( https://packagist.org/packages/codeception/specify ) [ ![ License] ( https://poser.pugx.org/codeception/specify/license )] ( https://packagist.org/packages/codeception/specify )
6
+ [ ![ Latest Stable Version] ( https://poser.pugx.org/codeception/specify/v/stable )] ( https://packagist.org/packages/codeception/specify )
7
+ [ ![ Total Downloads] ( https://poser.pugx.org/codeception/specify/downloads )] ( https://packagist.org/packages/codeception/specify )
8
+ [ ![ Latest Unstable Version] ( https://poser.pugx.org/codeception/specify/v/unstable )] ( https://packagist.org/packages/codeception/specify )
9
+ [ ![ License] ( https://poser.pugx.org/codeception/specify/license )] ( https://packagist.org/packages/codeception/specify )
7
10
8
- Specify allows you to write your tests in more readable BDD style, the same way you might have experienced with [ Jasmine] ( https://jasmine.github.io/ ) .
11
+ Specify allows you to write your tests in more readable BDD style, the same way you might have experienced with [ Jasmine] [ 3 ] .
9
12
Inspired by MiniTest of Ruby now you combine BDD and classical TDD style in one test.
10
13
11
- ### Basic Example
14
+ ## Installation
15
+
16
+ * Requires PHP >= 7.1*
17
+
18
+ * Install with Composer:
19
+
20
+ ```
21
+ composer require codeception/specify --dev
22
+ ```
23
+
24
+ * Include ` Codeception\Specify ` trait in your tests.
25
+
26
+
27
+ ## Usage
12
28
13
29
Specify ` $this->specify ` method to add isolated test blocks for your PHPUnit tests!
14
30
@@ -17,75 +33,65 @@ public function testValidation()
17
33
{
18
34
$this->assertInstanceOf('Model', $this->user);
19
35
20
- $this->specify(" username is required" , function() {
36
+ $this->specify(' username is required' , function() {
21
37
$this->user->username = null;
22
- $this->assertFalse($this->user->validate(['username']));
38
+ $this->assertFalse($this->user->validate(['username']));
23
39
});
24
40
25
- $this->specify(" username is too long" , function() {
41
+ $this->specify(' username is too long' , function() {
26
42
$this->user->username = 'toolooooongnaaaaaaameeee';
27
- $this->assertFalse($this->user->validate(['username']));
28
- });
29
-
30
- $this->specify("username is ok", function() {
31
- $this->user->username = 'davert';
32
- $this->assertTrue($this->user->validate(['username']));
43
+ $this->assertFalse($this->user->validate(['username']));
33
44
});
34
45
}
35
46
```
36
47
37
48
### BDD Example
38
49
39
- Specify supports ` describe-it ` BDD syntax inside PHPUnit
50
+ Specify supports ` describe-it ` and ` describe-should ` BDD syntax inside PHPUnit
40
51
41
52
``` php
42
53
public function testValidation()
43
54
{
44
- $this->describe(" user" , function() {
45
- $this->it(" should have a name" , function() {
55
+ $this->describe(' user' , function () {
56
+ $this->it(' should have a name' , function() {
46
57
$this->user->username = null;
47
- $this->assertFalse($this->user->validate(['username']));
58
+ $this->assertFalse($this->user->validate(['username']));
48
59
});
60
+ });
49
61
50
- $this->it("should not have long name", function() {
51
- $this->user->username = 'toolooooongnaaaaaaameeee';
52
- $this->assertFalse($this->user->validate(['username']));
53
- });
54
-
55
- // use `$this->>should` as shortcut
56
- $this->should("be ok with valid name", function() {
62
+ // you can use chained methods for better readability:
63
+ $this->describe('user')
64
+ ->should('be ok with valid name', function() {
57
65
$this->user->username = 'davert';
58
- $this->assertTrue($this->user->validate(['username']));
59
- });
60
-
66
+ $this->assertTrue($this->user->validate(['username']));
67
+ })
68
+ ->shouldNot('have long name', function() {
69
+ $this->user->username = 'toolooooongnaaaaaaameeee';
70
+ $this->assertFalse($this->user->validate(['username']));
71
+ })
61
72
// empty codeblocks are marked as Incomplete tests
62
- $this ->it(" should be ok with valid name");
63
- }) ;
73
+ ->it(' should be ok with valid name')
74
+ ;
64
75
}
65
76
```
66
77
67
78
68
79
### Specify + Verify Example
69
80
70
- Use [ Codeception/Verify] ( https://github.com/Codeception/Verify ) for simpler assertions:
81
+ Use [ Codeception/Verify] [ 4 ] for simpler assertions:
71
82
72
83
``` php
73
84
public function testValidation()
74
85
{
75
- $this->specify("username is required", function() {
76
- $this->user->username = null;
77
- expect_not($this->user->validate(['username']));
78
- });
79
-
80
- $this->specify("username is too long", function() {
86
+ $this->specify('username is too long', function() {
81
87
$this->user->username = 'toolooooongnaaaaaaameeee';
82
- expect_not($this->user->validate(['username']));
88
+ expect_not($this->user->validate(['username']));
89
+ });
90
+
91
+ $this->specify('username is ok', function() {
92
+ $this->user->username = 'davert';
93
+ expect_that($this->user->validate(['username']));
83
94
});
84
-
85
- $this->specify("username is ok", function() {
86
- $this->user->username = 'davert';
87
- expect_that($this->user->validate(['username']));
88
- });
89
95
}
90
96
```
91
97
@@ -98,26 +104,27 @@ This is very similar to BDD syntax as in RSpec or Mocha but works inside PHPUnit
98
104
99
105
``` php
100
106
<?php
107
+
101
108
class UserTest extends PHPUnit\Framework\TestCase
102
109
{
103
110
use Codeception\Specify;
104
-
111
+
105
112
/** @specify */
106
113
protected $user; // is cloned inside specify blocks
107
-
108
- public function setUp()
114
+
115
+ public function setUp(): void
109
116
{
110
117
$this->user = new User;
111
118
}
112
119
113
120
public function testValidation()
114
121
{
115
122
$this->user->name = 'davert';
116
- $this->specify(" i can change my name" , function() {
123
+ $this->specify(' i can change my name' , function() {
117
124
$this->user->name = 'jon';
118
125
$this->assertEquals('jon', $this->user->name);
119
- });
120
- // user name is " davert" again
126
+ });
127
+ // user name is ' davert' again
121
128
$this->assertEquals('davert', $this->user->name);
122
129
}
123
130
}
@@ -130,7 +137,7 @@ Failure in `specify` block won't get your test stopped.
130
137
131
138
``` php
132
139
<?php
133
- $this->specify(" failing but test goes on" , function() {
140
+ $this->specify(' failing but test goes on' , function() {
134
141
$this->fail('bye');
135
142
});
136
143
$this->assertTrue(true);
@@ -186,7 +193,7 @@ $this->specify('this should not fail', function () {
186
193
187
194
``` php
188
195
<?php
189
- $this->specify(" should calculate square numbers" , function($number, $square) {
196
+ $this->specify(' should calculate square numbers' , function($number, $square) {
190
197
$this->assertEquals($square, $number*$number);
191
198
}, ['examples' => [
192
199
[2,4],
@@ -198,7 +205,7 @@ You can also use DataProvider functions in `examples` param.
198
205
199
206
``` php
200
207
<?php
201
- $this->specify(" should calculate square numbers" , function($number, $square) {
208
+ $this->specify(' should calculate square numbers' , function($number, $square) {
202
209
$this->assertEquals($square, $number*$number);
203
210
}, ['examples' => $this->provider()]);
204
211
```
@@ -243,26 +250,25 @@ $this->cleanSpecify(); // removes before/after callbacks
243
250
244
251
Available methods:
245
252
246
- * ` $this->specify(name, callable fn = null, params = []) ` - starts a specify code block. If ` fn ` is null, marks test as incomplete.
247
- * ` $this->describe(name, callable fn = null) ` - starts a describe code block. Same as ` specify ` but expects to receive more nested into ` fn ` .
248
- * ` $this->it(name, callable fn = null) ` - starts a code block. Alias to ` specify ` .
249
- * ` $this->should(name, callable fn = null) ` - starts a code block. Same as ` specify ` but prepends word "should" into description.
253
+ ``` php
254
+ // Starts a specify code block:
255
+ $this->specify(string $thing, callable $code = null, $examples = [])
250
256
257
+ // Starts a describe code block. Same as 'specify' but expects chained 'it' or 'should' methods.
258
+ $this->describe(string $feature, callable $code = null)
251
259
252
- ## Installation
260
+ // Starts a code block. If 'code' is null, marks test as incomplete.
261
+ $this->it(string $spec, callable $code = null, $examples = [])
262
+ $this->its(string $spec, callable $code = null, $examples = [])
253
263
254
- * Requires PHP >= 7.*
255
-
256
- * Install with Composer:
257
-
258
- ```
259
- composer require codeception/specify --dev
264
+ // Starts a code block. Same as 'it' but prepends 'should' or 'should not' into description.
265
+ $this->should(string $behavior, callable $code = null, $examples = [])
266
+ $this->shouldNot(string $behavior, callable $code = null, $examples = [])
260
267
```
261
268
262
- * Include ` Codeception\Specify ` trait into ` PHPUnit\Framework\TestCase ` .
263
- * Add ` /** @specify **/ ` docblock for all properties you want to make isolated inside tests.
269
+ ## Printer Options
264
270
265
- * For PHPUnit add ` Codeception\Specify\ResultPrinter ` printer into ` phpunit.xml `
271
+ For PHPUnit, add ` Codeception\Specify\ResultPrinter ` printer into ` phpunit.xml `
266
272
267
273
``` xml
268
274
<phpunit colors =" true" printerClass =" Codeception\Specify\ResultPrinter" >
@@ -271,8 +277,16 @@ composer require codeception/specify --dev
271
277
272
278
## Recommended
273
279
274
- * Use [ Codeception/AssertThrows] ( https://github.com/Codeception/AssertThrows ) for exception assertions
275
- * Use [ Codeception/DomainAssert] ( https://github.com/Codeception/DomainAssert ) for verbose domain logic assertions
276
- * Сombine this with [ Codeception/Verify] ( https://github.com/Codeception/Verify ) library, to get BDD style assertions.
280
+ * Use [ Codeception/AssertThrows] [ 5 ] for exception assertions
281
+ * Use [ Codeception/DomainAssert] [ 6 ] for verbose domain logic assertions
282
+ * Combine this with [ Codeception/Verify] [ 4 ] library, to get BDD style assertions.
283
+
284
+ License: [ MIT.] [ 7 ]
277
285
278
- License: MIT
286
+ [ 1 ] : https://phpunit.de/
287
+ [ 2 ] : http://codeception.com/
288
+ [ 3 ] : https://jasmine.github.io/
289
+ [ 4 ] : https://github.com/Codeception/Verify
290
+ [ 5 ] : https://github.com/Codeception/AssertThrows
291
+ [ 6 ] : https://github.com/Codeception/DomainAssert
292
+ [ 7 ] : /LICENSE
0 commit comments