@@ -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,7 @@ 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 [ Go! AOP Requirements] ( https://github.com/goaop/framework#requirements )
153151
154152## Installation
155153
@@ -177,14 +175,14 @@ Include `AspectMock\Kernel` class into your tests bootstrap file.
177175
178176``` php
179177<?php
178+
180179include __DIR__.'/../vendor/autoload.php'; // composer autoload
181180
182181$kernel = \AspectMock\Kernel::getInstance();
183182$kernel->init([
184183 'debug' => true,
185184 'includePaths' => [__DIR__.'/../src']
186185]);
187- ?>
188186```
189187
190188If your project uses Composer's autoloader, that's all you need to get started.
@@ -195,6 +193,7 @@ If you use a custom autoloader (like in Yii/Yii2 frameworks), you should explici
195193
196194``` php
197195<?php
196+
198197include __DIR__.'/../vendor/autoload.php'; // composer autoload
199198
200199$kernel = \AspectMock\Kernel::getInstance();
@@ -203,7 +202,6 @@ $kernel->init([
203202 'includePaths' => [__DIR__.'/../src']
204203]);
205204$kernel->loadFile('YourAutoloader.php'); // path to your autoloader
206- ?>
207205```
208206
209207Load all autoloaders of your project this way, if you do not rely on Composer entirely.
@@ -217,6 +215,7 @@ Explicitly load all required files before testing:
217215
218216``` php
219217<?php
218+
220219include __DIR__.'/../vendor/autoload.php'; // composer autoload
221220
222221$kernel = \AspectMock\Kernel::getInstance();
@@ -226,7 +225,6 @@ $kernel->init([
226225]);
227226require 'YourAutoloader.php';
228227$kernel->loadPhpFiles('/../common');
229- ?>
230228```
231229
232230### Customization
@@ -244,14 +242,14 @@ Example:
244242
245243``` php
246244<?php
245+
247246$kernel = \AspectMock\Kernel::getInstance();
248247$kernel->init([
249248 'appDir' => __DIR__ . '/../../',
250249 'cacheDir' => '/tmp/myapp',
251250 'includePaths' => [__DIR__.'/../src']
252251 'excludePaths' => [__DIR__] // tests dir should be excluded
253252]);
254- ?>
255253```
256254
257255[ More configs for different frameworks] ( https://github.com/Codeception/AspectMock/wiki/Example-configs ) .
@@ -271,6 +269,7 @@ Clear the test doubles registry between tests.
271269
272270``` php
273271<?php
272+
274273use AspectMock\Test as test;
275274
276275class UserTest extends \PHPUnit_Framework_TestCase
@@ -287,8 +286,6 @@ class UserTest extends \PHPUnit_Framework_TestCase
287286 \demo\UserModel::tableName();
288287 $user->verifyInvokedMultipleTimes('tableName',2);
289288 }
290-
291- ?>
292289```
293290
294291## Usage in Codeception.
@@ -298,6 +295,7 @@ We recommend including a call to `test::clean()` from your `CodeHelper` class:
298295
299296``` php
300297<?php
298+
301299namespace Codeception\Module;
302300
303301class CodeHelper extends \Codeception\Module
@@ -307,7 +305,6 @@ class CodeHelper extends \Codeception\Module
307305 \AspectMock\Test::clean();
308306 }
309307}
310- ?>
311308```
312309
313310## Improvements?
0 commit comments