Skip to content

Commit b9979b0

Browse files
committed
merged with master
2 parents b0b9df7 + f22b615 commit b9979b0

2 files changed

Lines changed: 45 additions & 46 deletions

File tree

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Is there any way to stub a static method of a class? Can you redefine a class me
2121
Dynamic languages like Ruby or JavaScript allow us to do this.
2222
These features are essential for testing. AspectMock to the rescue!
2323

24-
Dozens of lines of untested code are written everyday in PHP.
24+
Thousands of lines of untested code are written everyday in PHP.
2525
In most cases, this code is not actually bad,
2626
but PHP does not provide capabilities to test it. You may suggest rewriting it from scratch following test driven design practices and use dependency injection wherever possible. Should this be done for stable working code? Well, there are much better ways to waste time.
2727

@@ -126,10 +126,10 @@ function testSimpleStubAndMock()
126126
{
127127
$user = test::double(new User, ['getName' => 'davert']);
128128
$this->assertEquals('davert', $user->getName());
129-
$user->verifyMethodInvoked('getName');
130-
$user->verifyMethodInvokedOnce('getName');
131-
$user->verifyMethodNeverInvoked('setName');
132-
$user->verifyMethodInvokedMultipleTimes('setName',1);
129+
$user->verifyInvoked('getName');
130+
$user->verifyInvokedOnce('getName');
131+
$user->verifyNeverInvoked('setName');
132+
$user->verifyInvokedMultipleTimes('setName',1);
133133
}
134134
?>
135135
```
@@ -321,4 +321,4 @@ Daveloped by **Michael Bodnarchuk**.
321321

322322
License: **MIT**.
323323

324-
Powered by [Go! Aspect-Oriented Framework](http://go.aopphp.com/)
324+
Powered by [Go! Aspect-Oriented Framework](http://go.aopphp.com/)

src/AspectMock/Test.php

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
/**
1111
* `AspectMock\Test` class is a builder of test doubles.
1212
* Any object can be enhanced and turned to a test double with the call to `double` method.
13+
* Mocking abstract classes and interfaces is not supported at this time.
1314
* This allows to redefine any method of object with your own, and adds mock verification methods.
1415
*
1516
* **Recommended Usage**:
@@ -23,14 +24,14 @@
2324
class Test {
2425

2526
/**
26-
* test::double registers class or object to track its calls.
27+
* `test::double` registers class or object to track its calls.
2728
* In second argument you may pass values that mocked mathods should return.
2829
*
29-
* Returns either of [**ClassProxy**](https://github.com/Codeception/AspectMock/blob/master/docs/ClassProxy.md)
30-
* or [**InstanceProxy**](https://github.com/Codeception/AspectMock/blob/master/docs/InstanceProxy.md).
31-
* Proxies are used to verify method invocations, and some other useful things.
30+
* Returns either of [**ClassProxy**](https://github.com/Codeception/AspectMock/blob/master/docs/ClassProxy.md) (when a string was passed)
31+
* or [**InstanceProxy**](https://github.com/Codeception/AspectMock/blob/master/docs/InstanceProxy.md) (when an object was passed).
32+
* Proxies are used to verify method invocations, and some other useful things (check out the links above for more).
3233
*
33-
* Example:
34+
* Examples:
3435
*
3536
* ``` php
3637
* <?php
@@ -69,14 +70,13 @@ class Test {
6970
* # create an instance of mocked class
7071
* test::double('User')->construct(['name' => 'davert']); // via constructir
7172
* test::double('User')->make(); // without calling constructor
72-
*
73+
*
7374
* # stub for magic method
7475
* test::double('User', ['findByUsernameAndPasswordAndEmail' => false]);
75-
* User::findByUsernameAndPasswordAndEmail; // null
76-
*
76+
* User::findByUsernameAndPasswordAndEmail(); // null
77+
*
7778
* # stub for method of parent class
7879
* # if User extends ActiveRecord
79-
*
8080
* test::double('ActiveRecord', ['save' => false]);
8181
* $user = new User(['name' => 'davert']);
8282
* $user->save(); // false
@@ -85,12 +85,12 @@ class Test {
8585
* ```
8686
*
8787
* @api
88-
* @param $classOrObject
89-
* @param array $params
88+
* @param string|object $classOrObject
89+
* @param array $params [ 'methodName' => 'returnValue' ]
9090
* @throws \Exception
91-
* @return Verifier
91+
* @return Verifier Usually Proxy\ClassProxy|Proxy\InstanceProxy
9292
*/
93-
public static function double($classOrObject, $params = array())
93+
public static function double($classOrObject, array $params = array())
9494
{
9595
$classOrObject = Registry::getRealClassOrObject($classOrObject);
9696
if (is_string($classOrObject)) {
@@ -120,22 +120,21 @@ public static function double($classOrObject, $params = array())
120120
* ?>
121121
* ```
122122
*
123-
* You can create instances of undefined classes and play with them.
123+
* You can create instances of undefined classes and play with them:
124124
*
125125
* ``` php
126126
* <?php
127127
* $user = test::spec('User')->construct();
128128
* $user->setName('davert');
129129
* $user->setNumPosts(count($user->getPosts()));
130130
* $this->assertEquals('davert', $user->getName()); // fail
131-
*
132131
* ?>
133132
* ```
134133
*
135-
* The test will be executed normally and will fail on the first assertion.
134+
* The test will be executed normally and will fail at the first assertion.
136135
*
137136
* `test::spec()->construct` creates an instance of `AspectMock\Proxy\Anything`
138-
* which tries to not cause errors whatever you try to do with it.
137+
* which tries not to cause errors whatever you try to do with it.
139138
*
140139
* ``` php
141140
* <?php
@@ -148,27 +147,26 @@ public static function double($classOrObject, $params = array())
148147
* ?>
149148
* ```
150149
*
151-
* None of this calls will trigger error on your test.
150+
* None of those calls will trigger an error in your test.
152151
* Thus, you can write a valid test before the class is declared.
153152
*
154153
* If class is already defined, `test::spec` will act as `test::double`.
155154
*
156155
* @api
157-
* @param $classOrObject
156+
* @param string|object $classOrObject
158157
* @param array $params
159-
* @return Verifier
160-
*
158+
* @return Verifier Usually Proxy\ClassProxy|Proxy\InstanceProxy
161159
*/
162-
public static function spec($classOrObject, $params = array())
160+
public static function spec($classOrObject, array $params = array())
163161
{
164162
if (is_object($classOrObject)) return self::double($classOrObject, $params);
165163
if (class_exists($classOrObject)) return self::double($classOrObject, $params);
166-
164+
167165
return new AnythingClassProxy($classOrObject);
168166
}
169167

170168
/**
171-
* Replaces all methods in a class with a dummies, except specified.
169+
* Replaces all methods in a class with dummies, except those specified in the `$only` param.
172170
*
173171
* ``` php
174172
* <?php
@@ -179,19 +177,19 @@ public static function spec($classOrObject, $params = array())
179177
* ?>
180178
* ```
181179
*
182-
* You can create a dummy without a constructor with all methods disabled
180+
* You can create a dummy without a constructor with all methods disabled:
183181
*
184182
* ``` php
185183
* <?php
186184
* $user = test::double('User')->make();
187185
* test::methods($user, []);
188186
* ?>
189187
* ```
190-
*2
188+
*
191189
* @api
192-
* @param $classOrObject
193-
* @param array $only
194-
* @return Core\ClassProxy|Core\InstanceProxy
190+
* @param string|object $classOrObject
191+
* @param string[] $only
192+
* @return Verifier Usually Proxy\ClassProxy|Proxy\InstanceProxy
195193
* @throws \Exception
196194
*/
197195
public static function methods($classOrObject, array $only = array())
@@ -215,7 +213,7 @@ public static function methods($classOrObject, array $only = array())
215213
}
216214

217215
/**
218-
* Replaces function in provided namespace with user-defined function or value that function returns;
216+
* Replaces function in provided namespace with user-defined function or value that function returns.
219217
* Function is restored to original on cleanup.
220218
*
221219
* ```php
@@ -234,7 +232,7 @@ public static function methods($classOrObject, array $only = array())
234232
*
235233
* ```
236234
*
237-
* Mocked functions can be verified for calls.
235+
* Mocked functions can be verified for calls:
238236
*
239237
* ```php
240238
* <?php
@@ -245,15 +243,15 @@ public static function methods($classOrObject, array $only = array())
245243
* $func->verifyInvokedOnce(['Y']);
246244
* ```
247245
*
248-
* @param $namespace
249-
* @param $function
250-
* @param $body
246+
* @param string $namespace
247+
* @param string $functionName
248+
* @param mixed $body whatever a function might return or Callable substitute
251249
* @return Proxy\FuncProxy
252250
*/
253-
public static function func($namespace, $function, $body)
251+
public static function func($namespace, $functionName, $body)
254252
{
255-
Core\Registry::registerFunc($namespace, $function, $body);
256-
return new Proxy\FuncProxy($namespace, $function);
253+
Core\Registry::registerFunc($namespace, $functionName, $body);
254+
return new Proxy\FuncProxy($namespace, $functionName);
257255
}
258256

259257
/**
@@ -276,22 +274,23 @@ public static function func($namespace, $function, $body)
276274
* ```
277275
*
278276
* @api
277+
* @param string|object $classOrObject
278+
* @return void
279279
*/
280280
public static function clean($classOrInstance = null)
281281
{
282282
Core\Registry::clean($classOrInstance);
283283
}
284284

285-
/*
285+
/**
286286
* Clears mock verifications but not stub definitions.
287287
*
288-
*
289288
* @api
289+
* @return void
290290
*/
291291
public static function cleanInvocations()
292292
{
293293
Core\Registry::cleanInvocations();
294294
}
295295

296-
297296
}

0 commit comments

Comments
 (0)