@@ -7,12 +7,10 @@ AspectMock allows you to stub and mock practically anything in your PHP code!
77
88** 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 )
99
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 )
1111[ ![ Latest Stable Version] ( https://poser.pugx.org/codeception/aspect-mock/v/stable.png )] ( https://packagist.org/packages/codeception/aspect-mock )
1212[ ![ Total Downloads] ( https://poser.pugx.org/codeception/aspect-mock/downloads )] ( https://packagist.org/packages/codeception/aspect-mock )
1313[ ![ 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-
1614
1715## Motivation
1816
@@ -45,14 +43,14 @@ Let's redefine static methods and verify their calls at runtime.
4543
4644``` php
4745<?php
46+
4847function testTableName()
4948{
5049 $this->assertEquals('users', UserModel::tableName());
5150 $userModel = test::double('UserModel', ['tableName' => 'my_users']);
5251 $this->assertEquals('my_users', UserModel::tableName());
5352 $userModel->verifyInvoked('tableName');
5453}
55- ?>
5654```
5755
5856#### Allows replacement of class methods.
@@ -61,14 +59,14 @@ Testing code developed with the **ActiveRecord** pattern. Does the use of the Ac
6159
6260``` php
6361<?php
62+
6463class UserService {
6564 function createUserByName($name) {
6665 $user = new User;
6766 $user->setName($name);
6867 $user->save();
6968 }
7069}
71- ?>
7270```
7371
7472Without 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
7977
8078``` php
8179<?php
80+
8281function testUserCreate()
8382{
8483 $user = test::double('User', ['save' => null]);
@@ -87,13 +86,13 @@ function testUserCreate()
8786 $this->assertEquals('davert', $user->getName());
8887 $user->verifyInvoked('save');
8988}
90- ?>
9189```
9290
9391#### Intercept even parent class methods and magic methods
9492
9593``` php
9694<?php
95+
9796// User extends ActiveRecord
9897function testUserCreate()
9998{
@@ -105,13 +104,13 @@ function testUserCreate()
105104 $AR->verifyInvoked('save');
106105 $this->assertEquals('miles', $user->getName());
107106}
108- ?>
109107```
110108
111109#### Override even standard PHP functions
112110
113111``` php
114112<?php
113+
115114namespace demo;
116115test::func('demo', 'time', 'now');
117116$this->assertEquals('now', time());
@@ -123,24 +122,23 @@ Only 4 methods are necessary for method call verification and one method to defi
123122
124123``` php
125124<?php
125+
126126function testSimpleStubAndMock()
127- {
127+ {
128128 $user = test::double(new User, ['getName' => 'davert']);
129129 $this->assertEquals('davert', $user->getName());
130130 $user->verifyInvoked('getName');
131131 $user->verifyInvokedOnce('getName');
132132 $user->verifyNeverInvoked('setName');
133133 $user->verifyInvokedMultipleTimes('setName',1);
134134}
135- ?>
136135```
137136
138137To check that method ` setName ` was called with ` davert ` as argument.
139138
140139``` php
141140<?php
142141$user->verifyMethodInvoked('setName', ['davert']);
143- ?>
144142```
145143
146144## Wow! But how does it work?
@@ -149,7 +147,8 @@ No PECL extensions is required. The [Go! AOP](http://go.aopphp.com/) library doe
149147
150148## Requirements
151149
152- PHP >= 5.6 + [ Go! AOP Requirements] ( https://github.com/goaop/framework#requirements )
150+ * ` PHP 7.4 ` .
151+ * ` Go! AOP 3.0 `
153152
154153## Installation
155154
@@ -177,14 +176,14 @@ Include `AspectMock\Kernel` class into your tests bootstrap file.
177176
178177``` php
179178<?php
179+
180180include __DIR__.'/../vendor/autoload.php'; // composer autoload
181181
182182$kernel = \AspectMock\Kernel::getInstance();
183183$kernel->init([
184184 'debug' => true,
185185 'includePaths' => [__DIR__.'/../src']
186186]);
187- ?>
188187```
189188
190189If 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
195194
196195``` php
197196<?php
197+
198198include __DIR__.'/../vendor/autoload.php'; // composer autoload
199199
200200$kernel = \AspectMock\Kernel::getInstance();
@@ -203,7 +203,6 @@ $kernel->init([
203203 'includePaths' => [__DIR__.'/../src']
204204]);
205205$kernel->loadFile('YourAutoloader.php'); // path to your autoloader
206- ?>
207206```
208207
209208Load 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:
217216
218217``` php
219218<?php
219+
220220include __DIR__.'/../vendor/autoload.php'; // composer autoload
221221
222222$kernel = \AspectMock\Kernel::getInstance();
@@ -226,7 +226,6 @@ $kernel->init([
226226]);
227227require 'YourAutoloader.php';
228228$kernel->loadPhpFiles('/../common');
229- ?>
230229```
231230
232231### Customization
@@ -244,14 +243,14 @@ Example:
244243
245244``` php
246245<?php
246+
247247$kernel = \AspectMock\Kernel::getInstance();
248248$kernel->init([
249249 'appDir' => __DIR__ . '/../../',
250250 'cacheDir' => '/tmp/myapp',
251251 'includePaths' => [__DIR__.'/../src']
252252 'excludePaths' => [__DIR__] // tests dir should be excluded
253253]);
254- ?>
255254```
256255
257256[ More configs for different frameworks] ( https://github.com/Codeception/AspectMock/wiki/Example-configs ) .
@@ -271,6 +270,7 @@ Clear the test doubles registry between tests.
271270
272271``` php
273272<?php
273+
274274use AspectMock\Test as test;
275275
276276class UserTest extends \PHPUnit_Framework_TestCase
@@ -287,8 +287,6 @@ class UserTest extends \PHPUnit_Framework_TestCase
287287 \demo\UserModel::tableName();
288288 $user->verifyInvokedMultipleTimes('tableName',2);
289289 }
290-
291- ?>
292290```
293291
294292## Usage in Codeception.
@@ -298,6 +296,7 @@ We recommend including a call to `test::clean()` from your `CodeHelper` class:
298296
299297``` php
300298<?php
299+
301300namespace Codeception\Module;
302301
303302class CodeHelper extends \Codeception\Module
@@ -307,7 +306,6 @@ class CodeHelper extends \Codeception\Module
307306 \AspectMock\Test::clean();
308307 }
309308}
310- ?>
311309```
312310
313311## Improvements?
0 commit comments