Download the PHP package adgoal/aspect-mock without Composer

On this page you can find all versions of the php package adgoal/aspect-mock. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package aspect-mock

AspectMock

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

Documentation | Test Doubles Builder | ClassProxy | InstanceProxy | FuncProxy

Build Status Latest Stable Version Total Downloads Monthly Downloads PHP 7 ready FOSSA Status

Motivation

PHP is a language that was not designed to be testable. Really. How would you fake the time() 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

Code Pitch

Allows stubbing and mocking of static methods.

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

Allows replacement of class methods.

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

Without AspectMock you need to introduce User as an explicit dependency into class UserService 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 by createUserByName:

Intercept even parent class methods and magic methods

Override even standard PHP functions

Beautifully simple

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

To check that method setName was called with davert as argument.

Wow! But how does it work?

No PECL extensions is required. The Go! 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 the Go! AOP Framework. Check out Aspect Oriented Development and the Go! library itself.

Requirements

PHP >= 5.6 + Go! AOP Requirements

Installation

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

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

Configuration

Include AspectMock\Kernel class into your tests bootstrap file.

With Composer's Autoloader

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:

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:

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.

Example:

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 created bootstrap in your phpunit.xml configuration. Also disable backupGlobals:

Clear the test doubles registry between tests.

Usage in Codeception.

Include AspectMock\Kernel into tests/_bootstrap.php. We recommend including a call to test::clean() from your CodeHelper class:

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 @adgoal for updates.

Developed by Michael Bodnarchuk.

License: MIT.

Powered by Go! Aspect-Oriented Framework

License

FOSSA Status


All versions of aspect-mock with dependencies

PHP Build Version
Package Version
Requires php Version >=7.0.0
adgoal/goaop-framework Version ^2.2.1
phpunit/phpunit Version > 6.0.0
symfony/finder Version ~2.4|~3.0|~4.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package adgoal/aspect-mock contains the following files

Loading the files please wait ....