5
5
6
6
` AspectMock\Test ` class is a builder of test doubles.
7
7
Any object can be enhanced and turned to a test double with the call to ` double ` method.
8
+ Mocking abstract classes and interfaces is not supported at this time.
8
9
This allows to redefine any method of object with your own, and adds mock verification methods.
9
10
10
11
** Recommended Usage** :
@@ -14,15 +15,16 @@ This allows to redefine any method of object with your own, and adds mock verifi
14
15
use AspectMock\Test as test;
15
16
?>
16
17
```
17
- #### * public static* double($classOrObject, $params = null)
18
- test::double registers class or object to track its calls.
18
+
19
+ #### * public static* double($classOrObject, array $params = Array ( ) )
20
+ ` test::double ` registers class or object to track its calls.
19
21
In second argument you may pass values that mocked mathods should return.
20
22
21
- Returns either of [ ** ClassProxy** ] ( https://github.com/Codeception/AspectMock/blob/master/docs/ClassProxy.md )
22
- or [ ** InstanceProxy** ] ( https://github.com/Codeception/AspectMock/blob/master/docs/InstanceProxy.md ) .
23
- Proxies are used to verify method invocations, and some other useful things.
23
+ Returns either of [ ** ClassProxy** ] ( https://github.com/Codeception/AspectMock/blob/master/docs/ClassProxy.md ) (when a string was passed)
24
+ or [ ** InstanceProxy** ] ( https://github.com/Codeception/AspectMock/blob/master/docs/InstanceProxy.md ) (when an object was passed) .
25
+ Proxies are used to verify method invocations, and some other useful things (check out the links above for more) .
24
26
25
- Example :
27
+ Examples :
26
28
27
29
``` php
28
30
<?php
@@ -64,11 +66,10 @@ test::double('User')->make(); // without calling constructor
64
66
65
67
# stub for magic method
66
68
test::double('User', ['findByUsernameAndPasswordAndEmail' => false]);
67
- User::findByUsernameAndPasswordAndEmail; // null
69
+ User::findByUsernameAndPasswordAndEmail() ; // null
68
70
69
71
# stub for method of parent class
70
72
# if User extends ActiveRecord
71
-
72
73
test::double('ActiveRecord', ['save' => false]);
73
74
$user = new User(['name' => 'davert']);
74
75
$user->save(); // false
@@ -77,12 +78,12 @@ $user->save(); // false
77
78
```
78
79
79
80
* api
80
- * ` param ` $classOrObject
81
- * ` param array ` $params
81
+ * ` param string|object ` $classOrObject
82
+ * ` param array ` $params [ 'methodName' => 'returnValue' ]
82
83
* throws \Exception
83
- * return Verifier
84
+ * return Verifier Usually Proxy\ClassProxy|Proxy\InstanceProxy
84
85
85
- #### * public static* spec($classOrObject, $params = null )
86
+ #### * public static* spec($classOrObject, array $params = Array ( ) )
86
87
If you follow TDD/BDD practices a test should be written before the class is defined.
87
88
If you would call undefined class in a test, a fatal error will be triggered.
88
89
Instead you can use ` test::spec ` method that will create a proxy for an undefined class.
@@ -94,22 +95,21 @@ $userClass->defined(); // false
94
95
?>
95
96
```
96
97
97
- You can create instances of undefined classes and play with them.
98
+ You can create instances of undefined classes and play with them:
98
99
99
100
``` php
100
101
<?php
101
102
$user = test::spec('User')->construct();
102
103
$user->setName('davert');
103
104
$user->setNumPosts(count($user->getPosts()));
104
105
$this->assertEquals('davert', $user->getName()); // fail
105
-
106
106
?>
107
107
```
108
108
109
- The test will be executed normally and will fail on the first assertion.
109
+ The test will be executed normally and will fail at the first assertion.
110
110
111
111
` test::spec()->construct ` creates an instance of ` AspectMock\Proxy\Anything `
112
- which tries to not cause errors whatever you try to do with it.
112
+ which tries not to cause errors whatever you try to do with it.
113
113
114
114
``` php
115
115
<?php
@@ -122,19 +122,18 @@ foreach ($user->names as $name) {
122
122
?>
123
123
```
124
124
125
- None of this calls will trigger error on your test.
125
+ None of those calls will trigger an error in your test.
126
126
Thus, you can write a valid test before the class is declared.
127
127
128
128
If class is already defined, ` test::spec ` will act as ` test::double ` .
129
129
130
130
* api
131
- * ` param ` $classOrObject
131
+ * ` param string|object ` $classOrObject
132
132
* ` param array ` $params
133
- * return Verifier
134
-
133
+ * return Verifier Usually Proxy\ClassProxy|Proxy\InstanceProxy
135
134
136
135
#### * public static* methods($classOrObject, array $only = Array ( ) )
137
- Replaces all methods in a class with a dummies, except specified.
136
+ Replaces all methods in a class with dummies, except those specified in the ` $only ` param .
138
137
139
138
``` php
140
139
<?php
@@ -145,7 +144,7 @@ $user->getName(); // jon
145
144
?>
146
145
```
147
146
148
- You can create a dummy without a constructor with all methods disabled
147
+ You can create a dummy without a constructor with all methods disabled:
149
148
150
149
``` php
151
150
<?php
@@ -155,13 +154,13 @@ test::methods($user, []);
155
154
```
156
155
157
156
* api
158
- * ` param ` $classOrObject
159
- * ` param array ` $only
160
- * return Core \ClassProxy|Core \InstanceProxy
157
+ * ` param string|object ` $classOrObject
158
+ * ` param string[] ` $only
159
+ * return Verifier Usually Proxy \ClassProxy|Proxy \InstanceProxy
161
160
* throws \Exception
162
161
163
- #### * public static* func($namespace, $function , $body)
164
- Replaces function in provided namespace with user-defined function or value that function returns;
162
+ #### * public static* func($namespace, $functionName , $body)
163
+ Replaces function in provided namespace with user-defined function or value that function returns.
165
164
Function is restored to original on cleanup.
166
165
167
166
``` php
@@ -180,7 +179,7 @@ test::func('demo', 'date', function($format) {
180
179
181
180
```
182
181
183
- Mocked functions can be verified for calls.
182
+ Mocked functions can be verified for calls:
184
183
185
184
``` php
186
185
<?php
@@ -191,9 +190,9 @@ $func->verifyInvoked();
191
190
$func->verifyInvokedOnce(['Y']);
192
191
```
193
192
194
- * ` param ` $namespace
195
- * ` param ` $function
196
- * ` param ` $body
193
+ * ` param string ` $namespace
194
+ * ` param string ` $functionName
195
+ * ` param mixed ` $body whatever a function might return or Callable substitute
197
196
* return Proxy\FuncProxy
198
197
199
198
#### * public static* clean($classOrInstance = null)
@@ -216,5 +215,13 @@ test::clean($user);
216
215
```
217
216
218
217
* api
218
+ * ` param string|object ` $classOrObject
219
+ * return void
219
220
220
221
#### * public static* cleanInvocations()
222
+ Clears mock verifications but not stub definitions.
223
+
224
+ * api
225
+ * return void
226
+
227
+
0 commit comments