Skip to content

Commit 345dff6

Browse files
authored
Update codebase to PHP 7.4 (#197)
* Update codebase to PHP 7.4 * Update docs * Fix review comments
1 parent 124fa59 commit 345dff6

33 files changed

+430
-508
lines changed

README.md

+16-18
Original file line numberDiff line numberDiff line change
@@ -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+
4847
function 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+
6463
class UserService {
6564
function createUserByName($name) {
6665
$user = new User;
6766
$user->setName($name);
6867
$user->save();
6968
}
7069
}
71-
?>
7270
```
7371

7472
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
7977

8078
``` php
8179
<?php
80+
8281
function 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
9897
function 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+
115114
namespace demo;
116115
test::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+
126126
function 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

138137
To 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+
180180
include __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

190189
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
195194

196195
``` php
197196
<?php
197+
198198
include __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

209208
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:
217216

218217
``` php
219218
<?php
219+
220220
include __DIR__.'/../vendor/autoload.php'; // composer autoload
221221

222222
$kernel = \AspectMock\Kernel::getInstance();
@@ -226,7 +226,6 @@ $kernel->init([
226226
]);
227227
require '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+
274274
use AspectMock\Test as test;
275275

276276
class 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+
301300
namespace Codeception\Module;
302301

303302
class CodeHelper extends \Codeception\Module
@@ -307,7 +306,6 @@ class CodeHelper extends \Codeception\Module
307306
\AspectMock\Test::clean();
308307
}
309308
}
310-
?>
311309
```
312310

313311
## Improvements?

RoboFile.php

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
require_once 'vendor/autoload.php';
34

45
class Robofile extends \Robo\Tasks

composer.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"name": "codeception/aspect-mock",
33
"description": "Experimental Mocking Framework powered by Aspects",
4+
"license": "MIT",
45
"authors": [
56
{
67
"name": "Michael Bodnarchuk",
@@ -21,7 +22,7 @@
2122
"require-dev": {
2223
"codeception/codeception": "^4.1",
2324
"codeception/verify": "^2.1",
24-
"codeception/specify": "^1.4"
25-
},
26-
"license": "MIT"
25+
"codeception/specify": "^1.4",
26+
"consolidation/robo": "^3.0"
27+
}
2728
}

docs/ClassProxy.md

+12-46
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,22 @@ ClassProxy represents a class of your project.
1010
* It can be used to check class definitions.
1111

1212

13-
``` php
13+
```php
1414
<?php
1515
$userModel = test::double('UserModel');
1616
UserModel::tableName();
1717
$user = $userModel->construct();
1818
$user->save();
1919
$userModel->verifyInvoked('tableName');
2020
$userModel->verifyInvoked('save');
21-
?>
2221
```
2322

2423
You can get a class name of a proxy via `className` property.
2524

26-
``` php
25+
```php
2726
<?php
2827
$userModel = test::double('UserModel');
2928
$userModel->className; // UserModel
30-
?>
3129
```
3230

3331
Also, you can get the list of calls for a specific method.
@@ -37,65 +35,50 @@ Also, you can get the list of calls for a specific method.
3735
$user = test::double('UserModel');
3836
$user->someMethod('arg1', 'arg2');
3937
$user->getCallsForMethod('someMethod') // [ ['arg1', 'arg2'] ]
40-
?>
4138
```
4239

4340
#### *public* getCallsForMethod($method)
4441
#### *public* isDefined()
4542
Returns true if class exists.
4643
Returns false if class is not defined yet, and was declared via `test::spec`.
4744

48-
* return bool
49-
5045
#### *public* interfaces()
5146
Returns an array with all interface names of a class
5247

53-
* return array
54-
5548
#### *public* parent()
5649
Returns a name of the parent of a class.
5750

5851
* return null
5952

6053
#### *public* hasMethod($method)
61-
* `param` $method
62-
* return bool
54+
* `param mixed` $method
6355

6456
#### *public* hasProperty($property)
65-
* `param` $property
66-
* return bool
57+
* `param mixed` $property
6758

6859
#### *public* traits()
6960
Returns array of all trait names of a class.
7061

71-
* return array
72-
7362
#### *public* construct()
7463
Creates an instance of a class via constructor.
7564

76-
``` php
65+
```php
7766
<?
7867
$user = test::double('User')->construct([
7968
'name' => 'davert',
8069
'email' => '[email protected]'
8170
]);
82-
83-
?>
8471
```
85-
* return object
8672

8773
#### *public* make()
8874
Creates a class instance without calling a constructor.
8975

90-
``` php
76+
```php
9177
<?
9278
$user = test::double('User')->make();
93-
94-
?>
9579
```
96-
* return object
9780

98-
#### *public* verifyInvoked($name, $params = null)
81+
#### *public* verifyInvoked($name, array $params = null)
9982
Verifies a method was invoked at least once.
10083
In second argument you can specify with which params method expected to be invoked;
10184

@@ -104,38 +87,24 @@ In second argument you can specify with which params method expected to be invok
10487
$user->verifyInvoked('save');
10588
$user->verifyInvoked('setName',['davert']);
10689

107-
?>
10890
```
10991

110-
* `param` $name
111-
* `param null` $params
112-
* throws \PHPUnit_Framework_ExpectationFailedException
113-
* `param array` $params
114-
* throws fail
115-
116-
#### *public* verifyInvokedOnce($name, $params = null)
92+
#### *public* verifyInvokedOnce($name, array $params = null)
11793
Verifies that method was invoked only once.
11894

119-
* `param` $name
120-
* `param array` $params
121-
122-
#### *public* verifyInvokedMultipleTimes($name, $times, $params = null)
95+
#### *public* verifyInvokedMultipleTimes($name, $times, array $params = null)
12396
Verifies that method was called exactly $times times.
12497

12598
``` php
12699
<?php
127100
$user->verifyInvokedMultipleTimes('save',2);
128101
$user->verifyInvokedMultipleTimes('dispatchEvent',3,['before_validate']);
129102
$user->verifyInvokedMultipleTimes('dispatchEvent',4,['after_save']);
130-
?>
131103
```
132104

133-
* `param` $name
134-
* `param` $times
135-
* `param array` $params
136-
* throws \PHPUnit_Framework_ExpectationFailedException
105+
* throws ExpectationFailedException
137106

138-
#### *public* verifyNeverInvoked($name, $params = null)
107+
#### *public* verifyNeverInvoked($name, array $params = null)
139108
Verifies that method was not called.
140109
In second argument with which arguments is not expected to be called.
141110

@@ -146,11 +115,8 @@ $user->verifyNeverInvoked('setName'); // fail
146115
$user->verifyNeverInvoked('setName',['davert']); // fail
147116
$user->verifyNeverInvoked('setName',['bob']); // success
148117
$user->verifyNeverInvoked('setName',[]); // success
149-
?>
150118
```
151119

152-
* `param` $name
153-
* `param null` $params
154-
* throws \PHPUnit_Framework_ExpectationFailedException
120+
* throws ExpectationFailedException
155121

156122

0 commit comments

Comments
 (0)