@@ -10,42 +10,27 @@ Inspired by MiniTest of Ruby now you combine BDD and classical TDD style in one
10
10
11
11
### Basic Example
12
12
13
- Traditionally Specify used ` $this->specify ` function for all descriptions.
14
- That works too!
13
+ Specify ` $this->specify ` method to add isolated test blocks for your PHPUnit tests!
15
14
16
15
``` php
17
- <?php
18
- class UserTest extends PHPUnit\Framework\TestCase {
19
-
20
- use Codeception\Specify;
21
-
22
- /** @specify */
23
- protected $user;
24
-
25
- public function setUp()
26
- {
27
- $this->user = new User;
28
- }
29
-
30
- public function testValidation()
31
- {
32
- $this->assertInstanceOf('Model', $this->user);
33
-
34
- $this->specify("username is required", function() {
35
- $this->user->username = null;
36
- $this->assertFalse($this->user->validate(['username']));
37
- });
38
-
39
- $this->specify("username is too long", function() {
40
- $this->user->username = 'toolooooongnaaaaaaameeee',
41
- $this->assertFalse($this->user->validate(['username']));
42
- });
43
-
44
- $this->specify("username is ok", function() {
45
- $this->user->username = 'davert',
46
- $this->assertTrue($this->user->validate(['username']));
47
- });
48
- }
16
+ public function testValidation()
17
+ {
18
+ $this->assertInstanceOf('Model', $this->user);
19
+
20
+ $this->specify("username is required", function() {
21
+ $this->user->username = null;
22
+ $this->assertFalse($this->user->validate(['username']));
23
+ });
24
+
25
+ $this->specify("username is too long", function() {
26
+ $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']));
33
+ });
49
34
}
50
35
```
51
36
@@ -54,43 +39,28 @@ class UserTest extends PHPUnit\Framework\TestCase {
54
39
Specify supports ` describe-it ` BDD syntax inside PHPUnit
55
40
56
41
``` php
57
- <?php
58
- class UserTest extends PHPUnit\Framework\TestCase {
59
-
60
- use Codeception\Specify;
61
-
62
- /** @specify */
63
- protected $user;
64
-
65
- public function setUp()
66
- {
67
- $this->user = new User;
68
- }
69
-
70
- public function testValidation()
71
- {
72
- $this->describe("user", function() {
73
- $this->it("should have a name", function() {
74
- $this->user->username = null;
75
- $this->assertFalse($this->user->validate(['username']));
76
- });
77
-
78
- $this->it("should not have long name", function() {
79
- $this->user->username = 'toolooooongnaaaaaaameeee';
80
- $this->assertFalse($this->user->validate(['username']));
81
- });
82
-
83
- // use `$this->>should` as shortcut
84
- $this->should("be ok with valid name", function() {
85
- $this->user->username = 'davert';
86
- $this->assertTrue($this->user->validate(['username']));
87
- });
88
-
89
- // empty codeblocks are marked as Incomplete tests
90
- $this->it("should be ok with valid name");
42
+ public function testValidation()
43
+ {
44
+ $this->describe("user", function() {
45
+ $this->it("should have a name", function() {
46
+ $this->user->username = null;
47
+ $this->assertFalse($this->user->validate(['username']));
91
48
});
92
49
93
- }
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() {
57
+ $this->user->username = 'davert';
58
+ $this->assertTrue($this->user->validate(['username']));
59
+ });
60
+
61
+ // empty codeblocks are marked as Incomplete tests
62
+ $this->it("should be ok with valid name");
63
+ });
94
64
}
95
65
```
96
66
@@ -100,40 +70,58 @@ class UserTest extends PHPUnit\Framework\TestCase {
100
70
Use [ Codeception/Verify] ( https://github.com/Codeception/Verify ) for simpler assertions:
101
71
102
72
``` php
103
- <?php
104
- $this->specify("username is required", function() {
105
- $this->user->username = null;
106
- expect_not($this->user->validate(['username']));
107
- });
108
-
109
- $this->specify("username is too long", function() {
110
- $this->user->username = 'toolooooongnaaaaaaameeee';
111
- expect_not($this->user->validate(['username']));
112
- });
113
-
114
- $this->specify("username is ok", function() {
115
- $this->user->username = 'davert';
116
- expect_that($this->user->validate(['username']));
117
- });
73
+ public function testValidation()
74
+ {
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() {
81
+ $this->user->username = 'toolooooongnaaaaaaameeee';
82
+ expect_not($this->user->validate(['username']));
83
+ });
84
+
85
+ $this->specify("username is ok", function() {
86
+ $this->user->username = 'davert';
87
+ expect_that($this->user->validate(['username']));
88
+ });
89
+ }
118
90
```
119
91
120
92
## Purpose
121
93
122
94
This tiny library makes your tests a bit readable, by organizing test in well described code blocks.
123
95
Each code block is isolated.
124
96
125
- This means call to ` $this->specify ` does not change values of configured properties of a test class.
97
+ This means call to ` $this->specify ` does not change values of properties of a test class.
98
+ Isolated properties should be marked with ` @specify ` annotation.
126
99
127
100
``` php
128
101
<?php
129
- $this->user->name = 'davert';
130
- $this->specify("i can change my name", function() {
131
- $this->user->name = 'jon';
132
- $this->assertEquals('jon', $this->user->name);
133
- });
134
-
135
- $this->assertEquals('davert', $this->user->name);
136
- ?>
102
+ class UserTest extends PHPUnit\Framework\TestCase {
103
+
104
+ use Codeception\Specify;
105
+
106
+ /** @specify */
107
+ protected $user; // is cloned inside specify blocks
108
+
109
+ public function setUp()
110
+ {
111
+ $this->user = new User;
112
+ }
113
+
114
+ public function testValidation()
115
+ {
116
+ $this->user->name = 'davert';
117
+ $this->specify("i can change my name", function() {
118
+ $this->user->name = 'jon';
119
+ $this->assertEquals('jon', $this->user->name);
120
+ });
121
+ // user name is davert again
122
+ $this->assertEquals('davert', $this->user->name);
123
+ }
124
+ }
137
125
```
138
126
139
127
Failure in ` specify ` block won't get your test stopped.
@@ -154,7 +142,7 @@ If a test fails you will see specification text in the result.
154
142
## Isolation
155
143
156
144
Isolation is achieved by ** cloning object properties** for each specify block.
157
- Only properties makred with ` @specify ` annotation are cloned.
145
+ Only properties marked with ` @specify ` annotation are cloned.
158
146
159
147
``` php
160
148
/** @specify */
@@ -169,6 +157,7 @@ protected $repository; // not cloning
169
157
```
170
158
171
159
Objects are cloned using deep cloning method.
160
+
172
161
** If object cloning affects performance, consider turning the clonning off** .
173
162
174
163
** Mocks are isolated** by default.
@@ -189,7 +178,6 @@ $this->specify('this should not fail', function () {
189
178
$config->expects($this->never())->method('init')->willReturn(null);
190
179
// success: $config->init() is never executed
191
180
});
192
-
193
181
```
194
182
195
183
## Examples
@@ -265,19 +253,16 @@ Available methods:
265
253
266
254
* Requires PHP >= 7.*
267
255
268
- Install with Composer:
269
-
256
+ * Install with Composer:
270
257
271
- ``` json
272
- "require-dev" : {
273
- "codeception/specify" : " *" ,
274
- "codeception/verify" : " *"
275
- }
276
258
```
277
- Include ` Codeception\Specify ` trait into ` PHPUnit\Framework\TestCase ` .
259
+ composer require codeception/specify --dev
260
+ ```
278
261
279
- For PHPUnit add ` Codeception\Specify\ResultPrinter ` printer into ` phpunit.xml `
262
+ * Include ` Codeception\Specify ` trait into ` PHPUnit\Framework\TestCase ` .
263
+ * Add ` /** @specify **/ ` docblock for all properties you want to make isolated inside tests.
280
264
265
+ * For PHPUnit add ` Codeception\Specify\ResultPrinter ` printer into ` phpunit.xml `
281
266
282
267
``` xml
283
268
<phpunit colors =" true" printerClass =" Codeception\Specify\ResultPrinter" >
0 commit comments