Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

The most powerful and flexible mocking framework for PHPUnit / Codeception.

License

NotificationsYou must be signed in to change notification settings

Codeception/AspectMock

Repository files navigation

AspectMock is not an ordinary PHP mocking framework.With the power of Aspect Oriented programming and the awesomeGo-AOP library,AspectMock allows you to stub and mock practically anything in your PHP code!

Documentation |Test Doubles Builder |ClassProxy |InstanceProxy |FuncProxy

Actions StatusLatest Stable VersionTotal DownloadsMonthly DownloadsStandWithUkraine

Motivation

PHP is a language that was not designed to be testable. Really.How would you fake thetime() function to produce the same result for each test call?Is there any way to stub a static method of a class? Can you redefine a class method at runtime?Dynamic languages like Ruby or JavaScript allow us to do this.These features are essential for testing. AspectMock to the rescue!

Thousands of lines of untested code are written everyday in PHP.In most cases, this code is not actually bad,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.

With AspectMock you can unit-test practically any OOP code. PHP powered with AOP incorporates features of dynamic languages we have long been missing. There is no excuse for not testing your code.You do not have to rewrite it from scratch to make it testable. Just install AspectMock with PHPUnit or Codeception and try to write some tests. It's really, really simple!

Features

  • Create test doubles forstatic methods.
  • Create test doubles forclass methods called anywhere.
  • Redefine methods on the fly.
  • Simple syntax that's easy to remember.

Code Pitch

Allows stubbing and mocking of static methods.

Let's redefine static methods and verify their calls at runtime.

<?phpfunctiontestTableName(){$this->assertSame('users', UserModel::tableName());$userModel = test::double('UserModel', ['tableName' =>'my_users']);$this->assertSame('my_users', UserModel::tableName());$userModel->verifyInvoked('tableName');}

Allows replacement of class methods.

Testing code developed with theActiveRecord pattern. Does the use of the ActiveRecord pattern sound like bad practice? No. But the code below is untestable in classic unit testing.

<?phpclass UserService {functioncreateUserByName($name) {$user =newUser;$user->setName($name);$user->save();    }}

Without AspectMock you need to introduceUser as an explicit dependency into classUserService to get it tested.But lets leave the code as it is. It works. Nevertheless, we should still test it to avoid regressions.

We don't want the$user->save method to actually get executed, as it will hit the database.Instead we will replace it with a dummy and verify that it gets called bycreateUserByName:

<?phpfunctiontestUserCreate(){$user = test::double('User', ['save' =>null]);$service =newUserService;$service->createUserByName('davert');$this->assertSame('davert',$user->getName());$user->verifyInvoked('save');}

Intercept even parent class methods and magic methods

<?php// User extends ActiveRecordfunctiontestUserCreate(){$AR = test::double('ActiveRecord', ['save' =>null]));test::double('User', ['findByNameAndEmail' =>newUser(['name' =>'jon'])]));$user = User::findByNameAndEmail('jon','jon@coltrane.com');// magic method$this->assertSame('jon',$user->getName());$user->save(['name' =>'miles']);// ActiveRecord->save did not hit database$AR->verifyInvoked('save');$this->assertSame('miles',$user->getName());}

Override even standard PHP functions

<?phpnamespacedemo;test::func('demo','time','now');$this->assertSame('now',time());

Beautifully simple

Only 4 methods are necessary for method call verification and one method to define test doubles:

<?phpfunctiontestSimpleStubAndMock(){$user = test::double(newUser, ['getName' =>'davert']);$this->assertSame('davert',$user->getName());$user->verifyInvoked('getName');$user->verifyInvokedOnce('getName');$user->verifyNeverInvoked('setName');$user->verifyInvokedMultipleTimes('setName',1);}

To check that methodsetName was called withdavert as argument.

<?php$user->verifyMethodInvoked('setName', ['davert']);

Wow! But how does it work?

No PECL extensions is required. TheGo! AOP library does the heavy lifting by patching autoloaded PHP classes on the fly. By introducing pointcuts to every method call, Go! allows intercepting practically any call to a method. AspectMock is a very tiny framework consisting of only 8 files using the power of theGo! AOP Framework. Check out Aspect Oriented Development and the Go! library itself.

Requirements

  • PHP 8.2+
  • Go! AOP 4.x

Installation

1. Add aspect-mock to your composer.json.

(lines with goaop @dev are needed depending on theminimum-stability in your composer settings)

{"require-dev": {"codeception/aspect-mock": "*","goaop/framework": "@dev","goaop/parser-reflection": "@dev"}}

2. Install AspectMock with Go! AOP as a dependency.

php composer.phar update

Configuration

IncludeAspectMock\Kernel class into your tests bootstrap file.

With Composer's Autoloader

<?phpinclude__DIR__.'/../vendor/autoload.php';// composer autoload$kernel = \AspectMock\Kernel::getInstance();$kernel->init(['debug' =>true,'includePaths' => [__DIR__.'/../src']]);

If your project uses Composer's autoloader, that's all you need to get started.

With Custom Autoloader

If you use a custom autoloader (like in Yii/Yii2 frameworks), you should explicitly point AspectMock to modify it:

<?phpinclude__DIR__.'/../vendor/autoload.php';// composer autoload$kernel = \AspectMock\Kernel::getInstance();$kernel->init(['debug' =>true,'includePaths' => [__DIR__.'/../src']]);$kernel->loadFile('YourAutoloader.php');// path to your autoloader

Load all autoloaders of your project this way, if you do not rely on Composer entirely.

Without Autoloader

If it still doesn't work for you...

Explicitly load all required files before testing:

<?phpinclude__DIR__.'/../vendor/autoload.php';// composer autoload$kernel = \AspectMock\Kernel::getInstance();$kernel->init(['debug' =>true,'includePaths' => [__DIR__.'/../src']]);require'YourAutoloader.php';$kernel->loadPhpFiles('/../common');

Customization

There are a few options you can customize setting up AspectMock. All them are defined in Go! Framework.They might help If you still didn't get AspectMock running on your project.

  • appDir defines the root of web application which is being tested. All classes outside the root will be replaced with the proxies generated by AspectMock. By default it is a directory in whichvendor dir of composer if located.If you don't use Composer or you have custom path to composer's vendor's folder, you should specify appDir
  • cacheDir a dir where updated source PHP files can be stored. If this directory is not set, proxie classes will be built on each run. Otherwise all PHP files used in tests will be updated with aspect injections and stored intocacheDir path.
  • includePaths directories with files that should be enhanced by Go Aop. Should point to your applications source files as well as framework files and any libraries you use..
  • excludePaths a paths in which PHP files should not be affected by aspects.You should exclude your tests files from interception.

Example:

<?php$kernel = \AspectMock\Kernel::getInstance();$kernel->init(['appDir'    =>__DIR__ .'/../../','cacheDir'  =>'/tmp/myapp','includePaths' => [__DIR__.'/../src']'excludePaths' => [__DIR__]// tests dir should be excluded]);

More configs for different frameworks.

It's pretty important to configure AspectMock properly. Otherwise it may not work as expected or you get side effects. Please make sure you included all files that you need to mock, but your test files as well as testing frameworks are excluded.

Usage in PHPUnit

Use newly createdbootstrap in yourphpunit.xml configuration. Also disablebackupGlobals:

<phpunitbootstrap="bootstrap.php"backupGlobals="false">

Clear the test doubles registry between tests.

<?phpuseAspectMock\Testastest;class UserTestextends \PHPUnit_Framework_TestCase{protectedfunctiontearDown()    {        test::clean();// remove all registered test doubles    }publicfunctiontestDoubleClass()    {$user = test::double('demo\UserModel', ['save' =>null]);        \demo\UserModel::tableName();        \demo\UserModel::tableName();$user->verifyInvokedMultipleTimes('tableName',2);    }

Usage in Codeception.

IncludeAspectMock\Kernel intotests/_bootstrap.php.We recommend including a call totest::clean() from yourCodeHelper class:

<?phpnamespaceCodeception\Module;class CodeHelperextends \Codeception\Module{function_after(\Codeception\TestCase$test){\AspectMock\Test::clean();}}

Improvements?

There is guaranteed to be room for improvements. This framework was not designed to do everything you might ever need (see notes below). But if you feel like you require a feature, please submit a Pull Request. It's pretty easy since there's not much code, and the Go! library is very well documented.

Credits

Follow@codeception for updates.

Developed byMichael Bodnarchuk.

License:MIT.

Powered byGo! Aspect-Oriented Framework

About

The most powerful and flexible mocking framework for PHPUnit / Codeception.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp