@@ -7,12 +7,10 @@ AspectMock allows you to stub and mock practically anything in your PHP code!
7
7
8
8
** Documentation** | [ Test Doubles Builder] ( https://github.com/Codeception/AspectMock/blob/master/docs/Test.md ) | [ ClassProxy] ( https://github.com/Codeception/AspectMock/blob/master/docs/ClassProxy.md ) | [ InstanceProxy] ( https://github.com/Codeception/AspectMock/blob/master/docs/InstanceProxy.md ) | [ FuncProxy] ( https://github.com/Codeception/AspectMock/blob/master/docs/FuncProxy.md )
9
9
10
- [ ![ Build Status] ( https://travis-ci.org /Codeception/AspectMock.png?branch=master )] ( https://travis-ci.org /Codeception/AspectMock )
10
+ [ ![ Actions Status] ( https://github.com /Codeception/AspectMock/workflows/CI/badge.svg )] ( https://github.com /Codeception/AspectMock/actions )
11
11
[ ![ Latest Stable Version] ( https://poser.pugx.org/codeception/aspect-mock/v/stable.png )] ( https://packagist.org/packages/codeception/aspect-mock )
12
12
[ ![ Total Downloads] ( https://poser.pugx.org/codeception/aspect-mock/downloads )] ( https://packagist.org/packages/codeception/aspect-mock )
13
13
[ ![ Monthly Downloads] ( https://poser.pugx.org/codeception/aspect-mock/d/monthly )] ( https://packagist.org/packages/codeception/aspect-mock )
14
- [ ![ PHP 7 ready] ( http://php7ready.timesplinter.ch/Codeception/AspectMock/master/badge.svg )] ( https://packagist.org/packages/codeception/aspect-mock )
15
-
16
14
17
15
## Motivation
18
16
@@ -45,14 +43,14 @@ Let's redefine static methods and verify their calls at runtime.
45
43
46
44
``` php
47
45
<?php
46
+
48
47
function testTableName()
49
48
{
50
49
$this->assertEquals('users', UserModel::tableName());
51
50
$userModel = test::double('UserModel', ['tableName' => 'my_users']);
52
51
$this->assertEquals('my_users', UserModel::tableName());
53
52
$userModel->verifyInvoked('tableName');
54
53
}
55
- ?>
56
54
```
57
55
58
56
#### Allows replacement of class methods.
@@ -61,14 +59,14 @@ Testing code developed with the **ActiveRecord** pattern. Does the use of the Ac
61
59
62
60
``` php
63
61
<?php
62
+
64
63
class UserService {
65
64
function createUserByName($name) {
66
65
$user = new User;
67
66
$user->setName($name);
68
67
$user->save();
69
68
}
70
69
}
71
- ?>
72
70
```
73
71
74
72
Without AspectMock you need to introduce ` User ` as an explicit dependency into class ` UserService ` to get it tested.
@@ -79,6 +77,7 @@ Instead we will replace it with a dummy and verify that it gets called by `creat
79
77
80
78
``` php
81
79
<?php
80
+
82
81
function testUserCreate()
83
82
{
84
83
$user = test::double('User', ['save' => null]);
@@ -87,13 +86,13 @@ function testUserCreate()
87
86
$this->assertEquals('davert', $user->getName());
88
87
$user->verifyInvoked('save');
89
88
}
90
- ?>
91
89
```
92
90
93
91
#### Intercept even parent class methods and magic methods
94
92
95
93
``` php
96
94
<?php
95
+
97
96
// User extends ActiveRecord
98
97
function testUserCreate()
99
98
{
@@ -105,13 +104,13 @@ function testUserCreate()
105
104
$AR->verifyInvoked('save');
106
105
$this->assertEquals('miles', $user->getName());
107
106
}
108
- ?>
109
107
```
110
108
111
109
#### Override even standard PHP functions
112
110
113
111
``` php
114
112
<?php
113
+
115
114
namespace demo;
116
115
test::func('demo', 'time', 'now');
117
116
$this->assertEquals('now', time());
@@ -123,24 +122,23 @@ Only 4 methods are necessary for method call verification and one method to defi
123
122
124
123
``` php
125
124
<?php
125
+
126
126
function testSimpleStubAndMock()
127
- {
127
+ {
128
128
$user = test::double(new User, ['getName' => 'davert']);
129
129
$this->assertEquals('davert', $user->getName());
130
130
$user->verifyInvoked('getName');
131
131
$user->verifyInvokedOnce('getName');
132
132
$user->verifyNeverInvoked('setName');
133
133
$user->verifyInvokedMultipleTimes('setName',1);
134
134
}
135
- ?>
136
135
```
137
136
138
137
To check that method ` setName ` was called with ` davert ` as argument.
139
138
140
139
``` php
141
140
<?php
142
141
$user->verifyMethodInvoked('setName', ['davert']);
143
- ?>
144
142
```
145
143
146
144
## Wow! But how does it work?
@@ -149,7 +147,8 @@ No PECL extensions is required. The [Go! AOP](http://go.aopphp.com/) library doe
149
147
150
148
## Requirements
151
149
152
- PHP >= 5.6 + [ Go! AOP Requirements] ( https://github.com/goaop/framework#requirements )
150
+ * ` PHP 7.4 ` .
151
+ * ` Go! AOP 3.0 `
153
152
154
153
## Installation
155
154
@@ -177,14 +176,14 @@ Include `AspectMock\Kernel` class into your tests bootstrap file.
177
176
178
177
``` php
179
178
<?php
179
+
180
180
include __DIR__.'/../vendor/autoload.php'; // composer autoload
181
181
182
182
$kernel = \AspectMock\Kernel::getInstance();
183
183
$kernel->init([
184
184
'debug' => true,
185
185
'includePaths' => [__DIR__.'/../src']
186
186
]);
187
- ?>
188
187
```
189
188
190
189
If your project uses Composer's autoloader, that's all you need to get started.
@@ -195,6 +194,7 @@ If you use a custom autoloader (like in Yii/Yii2 frameworks), you should explici
195
194
196
195
``` php
197
196
<?php
197
+
198
198
include __DIR__.'/../vendor/autoload.php'; // composer autoload
199
199
200
200
$kernel = \AspectMock\Kernel::getInstance();
@@ -203,7 +203,6 @@ $kernel->init([
203
203
'includePaths' => [__DIR__.'/../src']
204
204
]);
205
205
$kernel->loadFile('YourAutoloader.php'); // path to your autoloader
206
- ?>
207
206
```
208
207
209
208
Load all autoloaders of your project this way, if you do not rely on Composer entirely.
@@ -217,6 +216,7 @@ Explicitly load all required files before testing:
217
216
218
217
``` php
219
218
<?php
219
+
220
220
include __DIR__.'/../vendor/autoload.php'; // composer autoload
221
221
222
222
$kernel = \AspectMock\Kernel::getInstance();
@@ -226,7 +226,6 @@ $kernel->init([
226
226
]);
227
227
require 'YourAutoloader.php';
228
228
$kernel->loadPhpFiles('/../common');
229
- ?>
230
229
```
231
230
232
231
### Customization
@@ -244,14 +243,14 @@ Example:
244
243
245
244
``` php
246
245
<?php
246
+
247
247
$kernel = \AspectMock\Kernel::getInstance();
248
248
$kernel->init([
249
249
'appDir' => __DIR__ . '/../../',
250
250
'cacheDir' => '/tmp/myapp',
251
251
'includePaths' => [__DIR__.'/../src']
252
252
'excludePaths' => [__DIR__] // tests dir should be excluded
253
253
]);
254
- ?>
255
254
```
256
255
257
256
[ More configs for different frameworks] ( https://github.com/Codeception/AspectMock/wiki/Example-configs ) .
@@ -271,6 +270,7 @@ Clear the test doubles registry between tests.
271
270
272
271
``` php
273
272
<?php
273
+
274
274
use AspectMock\Test as test;
275
275
276
276
class UserTest extends \PHPUnit_Framework_TestCase
@@ -287,8 +287,6 @@ class UserTest extends \PHPUnit_Framework_TestCase
287
287
\demo\UserModel::tableName();
288
288
$user->verifyInvokedMultipleTimes('tableName',2);
289
289
}
290
-
291
- ?>
292
290
```
293
291
294
292
## Usage in Codeception.
@@ -298,6 +296,7 @@ We recommend including a call to `test::clean()` from your `CodeHelper` class:
298
296
299
297
``` php
300
298
<?php
299
+
301
300
namespace Codeception\Module;
302
301
303
302
class CodeHelper extends \Codeception\Module
@@ -307,7 +306,6 @@ class CodeHelper extends \Codeception\Module
307
306
\AspectMock\Test::clean();
308
307
}
309
308
}
310
- ?>
311
309
```
312
310
313
311
## Improvements?
0 commit comments