11Specify
22=======
33
4- BDD style code blocks for PHPUnit / Codeception
4+ BDD style code blocks for [ PHPUnit] [ 1 ] or [ Codeception] [ 2 ]
55
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 )
710
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 ] .
912Inspired by MiniTest of Ruby now you combine BDD and classical TDD style in one test.
1013
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
1228
1329Specify ` $this->specify ` method to add isolated test blocks for your PHPUnit tests!
1430
@@ -17,75 +33,65 @@ public function testValidation()
1733{
1834 $this->assertInstanceOf('Model', $this->user);
1935
20- $this->specify(" username is required" , function() {
36+ $this->specify(' username is required' , function() {
2137 $this->user->username = null;
22- $this->assertFalse($this->user->validate(['username']));
38+ $this->assertFalse($this->user->validate(['username']));
2339 });
2440
25- $this->specify(" username is too long" , function() {
41+ $this->specify(' username is too long' , function() {
2642 $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']));
3344 });
3445}
3546```
3647
3748### BDD Example
3849
39- Specify supports ` describe-it ` BDD syntax inside PHPUnit
50+ Specify supports ` describe-it ` and ` describe-should ` BDD syntax inside PHPUnit
4051
4152``` php
4253public function testValidation()
4354{
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() {
4657 $this->user->username = null;
47- $this->assertFalse($this->user->validate(['username']));
58+ $this->assertFalse($this->user->validate(['username']));
4859 });
60+ });
4961
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() {
5765 $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+ })
6172 // 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+ ;
6475}
6576```
6677
6778
6879### Specify + Verify Example
6980
70- Use [ Codeception/Verify] ( https://github.com/Codeception/Verify ) for simpler assertions:
81+ Use [ Codeception/Verify] [ 4 ] for simpler assertions:
7182
7283``` php
7384public function testValidation()
7485{
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() {
8187 $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']));
8394 });
84-
85- $this->specify("username is ok", function() {
86- $this->user->username = 'davert';
87- expect_that($this->user->validate(['username']));
88- });
8995}
9096```
9197
@@ -98,26 +104,27 @@ This is very similar to BDD syntax as in RSpec or Mocha but works inside PHPUnit
98104
99105``` php
100106<?php
107+
101108class UserTest extends PHPUnit\Framework\TestCase
102109{
103110 use Codeception\Specify;
104-
111+
105112 /** @specify */
106113 protected $user; // is cloned inside specify blocks
107-
108- public function setUp()
114+
115+ public function setUp(): void
109116 {
110117 $this->user = new User;
111118 }
112119
113120 public function testValidation()
114121 {
115122 $this->user->name = 'davert';
116- $this->specify(" i can change my name" , function() {
123+ $this->specify(' i can change my name' , function() {
117124 $this->user->name = 'jon';
118125 $this->assertEquals('jon', $this->user->name);
119- });
120- // user name is " davert" again
126+ });
127+ // user name is ' davert' again
121128 $this->assertEquals('davert', $this->user->name);
122129 }
123130}
@@ -130,7 +137,7 @@ Failure in `specify` block won't get your test stopped.
130137
131138``` php
132139<?php
133- $this->specify(" failing but test goes on" , function() {
140+ $this->specify(' failing but test goes on' , function() {
134141 $this->fail('bye');
135142});
136143$this->assertTrue(true);
@@ -186,7 +193,7 @@ $this->specify('this should not fail', function () {
186193
187194``` php
188195<?php
189- $this->specify(" should calculate square numbers" , function($number, $square) {
196+ $this->specify(' should calculate square numbers' , function($number, $square) {
190197 $this->assertEquals($square, $number*$number);
191198}, ['examples' => [
192199 [2,4],
@@ -198,7 +205,7 @@ You can also use DataProvider functions in `examples` param.
198205
199206``` php
200207<?php
201- $this->specify(" should calculate square numbers" , function($number, $square) {
208+ $this->specify(' should calculate square numbers' , function($number, $square) {
202209 $this->assertEquals($square, $number*$number);
203210}, ['examples' => $this->provider()]);
204211```
@@ -243,26 +250,25 @@ $this->cleanSpecify(); // removes before/after callbacks
243250
244251Available methods:
245252
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 = [])
250256
257+ // Starts a describe code block. Same as 'specify' but expects chained 'it' or 'should' methods.
258+ $this->describe(string $feature, callable $code = null)
251259
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 = [])
253263
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 = [])
260267```
261268
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
264270
265- * For PHPUnit add ` Codeception\Specify\ResultPrinter ` printer into ` phpunit.xml `
271+ For PHPUnit, add ` Codeception\Specify\ResultPrinter ` printer into ` phpunit.xml `
266272
267273``` xml
268274<phpunit colors =" true" printerClass =" Codeception\Specify\ResultPrinter" >
@@ -271,8 +277,16 @@ composer require codeception/specify --dev
271277
272278## Recommended
273279
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 ]
277285
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