You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/functions-expect.md
+31-28Lines changed: 31 additions & 28 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,73 +5,74 @@ title: "Testing functions with expect()"
5
5
-->
6
6
# Testing functions with expect()
7
7
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**.
10
9
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.
13
11
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.
15
13
16
14
It receives a function name and returns a Mockery expectation object with all its power.
17
15
18
16
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).
19
17
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).
Copy file name to clipboardExpand all lines: docs/functions-setup.md
+29-27Lines changed: 29 additions & 27 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,36 +5,41 @@ title: "Setup Brain Monkey"
5
5
-->
6
6
# Testing PHP Functions: Setup Brain Monkey
7
7
8
+
9
+
8
10
## Testing framework agnostic
9
11
10
12
Brain Monkey can be used with any testing framework.
13
+
11
14
Examples in this page will use PHPUnit, but the concepts are applicable at any testing framework.
12
15
16
+
17
+
13
18
## Warning
14
19
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/).
16
24
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):
18
26
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._
20
29
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).
23
30
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.
25
31
26
32
27
33
## Setup tests
28
34
29
35
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
31
40
32
-
After that you need to call a method *before* any test, and another *after* any test.
33
41
34
-
These two methods are:
35
42
36
-
-`Brain\Monkey::setUp()` has to be run before any test
37
-
-`Brain\Monkey::tearDown()` has to be run after any test
38
43
39
44
### PHPUnit example
40
45
@@ -47,15 +52,9 @@ use Brain\Monkey;
47
52
class MyTestCase extends PHPUnit_Framework_TestCase
48
53
{
49
54
50
-
protected function setUp()
51
-
{
52
-
parent::setUp();
53
-
Monkey::setUp();
54
-
}
55
-
56
55
protected function tearDown()
57
56
{
58
-
Monkey::tearDown();
57
+
Monkey\tearDown();
59
58
parent::tearDown();
60
59
}
61
60
}
@@ -65,30 +64,33 @@ After that for all test classes can extend this class instead of directly extend
65
64
66
65
That's all. Again, I used PHPUnit for the example, but any testing framework can be used.
67
66
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.
69
73
70
-
For the scope there are two entry-point methods of the `Functions` class: **`when()`** and **`expect()`**.
71
-
See dedicated doc pages.
72
74
73
75
74
76
## Namespaced functions
75
77
76
78
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:
79
81
80
82
```php
81
-
Functions::expect('a_global_function');
83
+
Functions\expect('a_global_function');
82
84
83
-
Functions::expect('My\\App\\awesome_function()');
85
+
Functions\expect('My\\App\\awesome_function');
84
86
```
85
87
86
88
89
+
87
90
## Note for WordPressers
88
91
89
92
Anything said in this page is fine for WordPress functions too, they are PHP functions, after all.
90
93
91
94
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).
92
95
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