Skip to content

Commit 8e0bbe1

Browse files
committed
Improce docs
1 parent 4aa15a2 commit 8e0bbe1

13 files changed

+1060
-501
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ It provides **two set of helpers**:
1717

1818
- PHP 5.6+
1919
- [Composer](https://getcomposer.org/) to install
20-
20+
2121
Via Composer following packages are required:
2222

2323
- [mockery/mockery](https://packagist.org/packages/mockery/mockery) version 1 (BSD-3-Clause)
@@ -42,4 +42,5 @@ Brain Monkey is hosted on GitHub. Feel free to open issues there for suggestions
4242

4343
I'm Giuseppe, I deal with PHP since 2005. For questions, rants or chat ping me on Twitter ([@gmazzap](https://twitter.com/gmazzap))
4444
or on ["The Loop"](http://chat.stackexchange.com/rooms/6/the-loop) (Stack Exchange) chat.
45-
Well, it's possible I'll ignore rants.
45+
46+
Well, it's possible I'll ignore rants.

docs/functions-expect.md

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,73 +5,74 @@ title: "Testing functions with expect()"
55
-->
66
# Testing functions with expect()
77

8-
Often, in tests, what we need is not only to enforce a function returned value (what `when()` allows to do), but
9-
to test function behavior based on **expectations**.
8+
Often, in tests, what we need is not only to enforce a function returned value (what `Functions\when()` allows to do), but to test function behavior based on **expectations**.
109

11-
Mockery has a very powerful, and human readable Domain Specific Language (DSL) that allows to set expectations
12-
on how object methods should behave, e.g. validate arguments they should receive, how many times they are called, and so on.
10+
Mockery has a very powerful, and human readable Domain Specific Language (DSL) that allows to set expectations on how object methods should behave, e.g. validate arguments they should receive, how many times they are called, and so on.
1311

14-
Brain Monkey brings that power to function testing. The entry-point is the `Functions::expect()` method.
12+
Brain Monkey brings that power to function testing. The entry-point is the `Functions\expect()` function.
1513

1614
It receives a function name and returns a Mockery expectation object with all its power.
1715

1816
Below there are just several examples, for the full story about Mockery expectations see its [documentation](http://docs.mockery.io/en/latest/reference/index.html).
1917

20-
Only note that in functions testing the `shouldReceive` Mockery method makes **no sense**, don't use it (an exception will be thrown if you do that).
18+
Only note that in functions testing the `shouldReceive` Mockery method makes **no sense**, so don't use it (an exception will be thrown if you do that).
19+
20+
2121

2222

2323
## Expectations on times a function is called
2424

2525
```php
26-
Functions::expect('paganini')->once();
26+
Functions\expect('paganini')->once();
2727

28-
Functions::expect('tween')->twice();
28+
Functions\expect('tween')->twice();
2929

30-
Functions::expect('who_knows')->zeroOrMoreTimes();
30+
Functions\expect('who_knows')->zeroOrMoreTimes();
3131

32-
Functions::expect('i_should_run')->atLeast()->once();
32+
Functions\expect('i_should_run')->atLeast()->once();
3333

34-
Functions::expect('i_have_a_max')->atMost()->twice();
34+
Functions\expect('i_have_a_max')->atMost()->twice();
3535

36-
Functions::expect('poor_me')->never();
36+
Functions\expect('poor_me')->never();
3737

38-
Functions::expect('pretty_precise')->times(3);
38+
Functions\expect('pretty_precise')->times(3);
3939

40-
Functions::expect('i_have_max_and_min')->between(2, 4);
40+
Functions\expect('i_have_max_and_min')->between(2, 4);
4141
```
4242

4343
There is no need to explain how it works: Mockery DSL reads like plain English.
4444

4545
Of course, expectation on the times a function should run can be combined with arguments expectation.
4646

4747

48+
4849
## Expectations on received arguments
4950

5051
Below a few examples, for the full story see [Mockery docs](http://docs.mockery.io/en/latest/reference/argument_validation.html).
5152

5253
```php
5354
// allow anything
54-
Functions::expect('function_name')
55+
Functions\expect('function_name')
5556
->once()
5657
->withAnyArgs();
5758

5859
// allow nothing
59-
Functions::expect('function_name')
60+
Functions\expect('function_name')
6061
->once()
6162
->withNoArgs();
6263

6364
// validate specific arguments
64-
Functions::expect('function_name')
65+
Functions\expect('function_name')
6566
->once()
6667
->with('arg_1', 'arg2');
6768

6869
// validate specific argument types
69-
Functions::expect('function_name')
70+
Functions\expect('function_name')
7071
->times(3)
7172
->with(Mockery::type('resource'), Mockery::type('int'));
7273

7374
// validate anything in specific places
74-
Functions::expect('function_name')
75+
Functions\expect('function_name')
7576
->zeroOrMoreTimes()
7677
->with(Mockery::any());
7778

@@ -81,44 +82,46 @@ Functions::expect('function_name')
8182
->with(Mockery::anyOf('a', 'b', 'c'));
8283

8384
// regex validation
84-
Functions::expect('function_name')
85+
Functions\expect('function_name')
8586
->once()
8687
->with('/^foo/');
8788

8889
// excluding specific values
89-
Functions::expect('function_name')
90+
Functions\expect('function_name')
9091
->once()
9192
->with(Mockery::not(2, 3));
9293

9394
// dealing with array arguments
94-
Functions::expect('function_name')
95+
Functions\expect('function_name')
9596
->once()
9697
->with(Mockery::hasKey('foo'), Mockery::contains('bar', 'baz'));
9798
```
9899

100+
101+
99102
## Forcing behavior
100103

101104
Excluding `shouldReceive`, all the Mockery expectation methods can be used with Brain Monkey, including
102105
`andReturn` or `andReturnUsing` used to enforce a function to return specific values during tests.
103106

104-
In fact, `Functions::when()` do same thing for simple cases when no expectations are required.
107+
In fact, `Functions\when()` do same thing for simple cases when no expectations are required.
105108

106109
Again, just a few examples:
107110

108111
```php
109112
// return a specific value
110-
Functions::expect('function_name')
113+
Functions\expect('function_name')
111114
->once()
112115
->with('foo', 'bar')
113116
->andReturn('Baz!');
114117

115118
// return values in order
116-
Functions::expect('function_name')
119+
Functions\expect('function_name')
117120
->twice()
118121
->andReturn('First time I run', 'Second time I run');
119122

120123
// return values in order, alternative
121-
Functions::expect('function_name')
124+
Functions\expect('function_name')
122125
->twice()
123126
->andReturnValues(['First time I run', 'Second time I run']);
124127

@@ -128,15 +131,15 @@ Functions::expect('function_name')
128131
->andReturnNull();
129132

130133
// use a callback for returning a value
131-
Functions::expect('function_name')
134+
Functions\expect('function_name')
132135
->atLeast()
133136
->once()
134137
->andReturnUsing(function() {
135138
return 'I am an alias!';
136139
});
137140

138141
// makes function throws an Exception (e.g. to test try statements)
139-
Functions::expect('function_name')
142+
Functions\expect('function_name')
140143
->once()
141144
->andThrow('RuntimeException'); // Both exception names and object are supported
142145
```

docs/functions-setup.md

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,41 @@ title: "Setup Brain Monkey"
55
-->
66
# Testing PHP Functions: Setup Brain Monkey
77

8+
9+
810
## Testing framework agnostic
911

1012
Brain Monkey can be used with any testing framework.
13+
1114
Examples in this page will use PHPUnit, but the concepts are applicable at any testing framework.
1215

16+
17+
1318
## Warning
1419

15-
Brain Monkey uses [Patchwork](http://antecedent.github.io/patchwork/) to redefine functions and so inherits from it some gotchas:
20+
Brain Monkey uses [Patchwork](http://antecedent.github.io/patchwork/) to redefine functions.
21+
22+
Brain Monkey 2.* requires Patchwork 2 which allows to re-define both userland and core functions,
23+
with some [limitations](http://patchwork2.org/limitations/).
1624

17-
- only **userland** (custom) functions can be redefined, PHP core functions can't.
25+
The main limitations that affects Brain Monkey are (from Patchwork website):
1826

19-
- only functions defined **after** Patchwork as been loaded can be redefined. Brain Monkey loads Patchwork when you call `Monkey::setUp()` (see below).
27+
- _Patchwork will fail on every attempt to redefine an internal function that is missing from the redefinable-internals array of your `patchwork.json`._
28+
- _Make sure that Patchwork is imported as early as possible, since any files imported earlier, including the one from which the importing takes place, will be missed by Patchwork's code preprocessor._
2029

21-
That happen at the start of any test. If functions you want to test are defined earlier (e.g. via Composer "file" autoload directive) you need to "manually" load Patchwork earlier
22-
(you'll need to require `Patchwork.php` file, see Patchwork docs).
2330

24-
Note that this is something you **don't** have to worry about if functions you want to test are not defined at all during tests.
2531

2632

2733
## Setup tests
2834

2935
After Brain Monkey is part of the project (see *Getting Started / Installation*), to be able to use its features
30-
you need to **require vendor autoload file** before running tests (e.g. PHPUnit users will probably require it in their bootstrap file).
36+
two simple steps are needed before being able to use Brain Monkey in tests:
37+
38+
1. be sure to require Composer autoload file _before_ running tests (e.g. PHPUnit users will probably require it in their bootstrap file).
39+
2. call the function `Brain\Monkey\tearDown()` after any test
3140

32-
After that you need to call a method *before* any test, and another *after* any test.
3341

34-
These two methods are:
3542

36-
- `Brain\Monkey::setUp()` has to be run before any test
37-
- `Brain\Monkey::tearDown()` has to be run after any test
3843

3944
### PHPUnit example
4045

@@ -47,15 +52,9 @@ use Brain\Monkey;
4752
class MyTestCase extends PHPUnit_Framework_TestCase
4853
{
4954

50-
protected function setUp()
51-
{
52-
parent::setUp();
53-
Monkey::setUp();
54-
}
55-
5655
protected function tearDown()
5756
{
58-
Monkey::tearDown();
57+
Monkey\tearDown();
5958
parent::tearDown();
6059
}
6160
}
@@ -65,30 +64,33 @@ After that for all test classes can extend this class instead of directly extend
6564

6665
That's all. Again, I used PHPUnit for the example, but any testing framework can be used.
6766

68-
Now you are ready to start testing functions.
67+
For function mocking and testing there are two entry-point functions:
68+
69+
- **`Functions\when()`**
70+
- **`Functions\expect()`**
71+
72+
See dedicated documentation pages.
6973

70-
For the scope there are two entry-point methods of the `Functions` class: **`when()`** and **`expect()`**.
71-
See dedicated doc pages.
7274

7375

7476
## Namespaced functions
7577

7678
All the code examples in this documentation make use of functions in global namespace.
77-
However, note that namespaced functions are supported as well, just be sure to pass the fully qualified name of the function
78-
to `Functions` methods:
79+
80+
However, note that namespaced functions are supported as well, just be sure to pass the fully qualified name of the functions:
7981

8082
```php
81-
Functions::expect('a_global_function');
83+
Functions\expect('a_global_function');
8284

83-
Functions::expect('My\\App\\awesome_function()');
85+
Functions\expect('My\\App\\awesome_function');
8486
```
8587

8688

89+
8790
## Note for WordPressers
8891

8992
Anything said in this page is fine for WordPress functions too, they are PHP functions, after all.
9093

9194
However, Brain Monkey has specific features for WordPress, and there is a way to setup tests for **all** Brain Monkey features (WordPress-specific and not).
9295

93-
If you want to use Brain Monkey to test code wrote for WordPress, it is preferable to use the setup explained in the *"WordPress / Setup"* section
94-
that *includes* the setup needed to use Brain Monkey tools for functions.
96+
**If you want to use Brain Monkey to test code wrote for WordPress, it is preferable to use the setup explained in the *"WordPress / Setup"* section that *includes* the setup needed to use Brain Monkey tools for functions.**

0 commit comments

Comments
 (0)