Download the PHP package jsiefer/mage-mock without Composer

On this page you can find all versions of the php package jsiefer/mage-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 mage-mock

mage-mock

Creating UnitTests for Magento classes can be hard considering that you have to extend from so many classes which in term require a lot of setup to get initialised in the first place.

The framework mocker will help you to mock the Magento environment.

The idea for a UnitTest here is that it...

All required Magento Classes are generated on the fly by the class-mocker lib.

Setup

Simply load this project using composer:

composer require jsiefer/mage-mock

Create a PHPUnit bootstrap.php file and register the MagentMock to the ClassMocker and enable the ClassMocker.

It is also recommended to setup the ClassMocker test listener so ClassMock object assertions are validated as well. (e.g. $test->expects($this->once())->method('test'))

Same goes for the the MageMock listener, which will reset the Mage class automatically after each test.

Just add listener to your phpunit.xml

How it works

The mocked classes

All classes generated by the class-mocker implement the PHPUnit MockObject interface that lets you alter or assert certain behaviour during runtime as you are familiar when using the standard PHPUnit mocks (e.g. $moock->expects($this->once())...).

Additional fixed behaviour, like simple stub logic, valid for the entire test run can be defined using traits.

MageMock already provides some basic stub logic. So for instance calling save() method on a Model will trigger the _beforeSave() and _afterSave() method.

Traits are used and registered to Mocks, when calling a Method on any generated Mock, the trait method is not getting called straight away, instead the call gets handled by the BaseMock class which is base class for all generated Mocks. The BaseMock will then check for any defined methods in the following order:

  1. Check for any mocked method.

  2. Check for closure functions.

  3. Check for trait methods.

  4. Check for and call magic trait methods (e.g. __call())

    The above traits can use ___call() method to listen to any call.

  5. Default method call (return self)

    Can be changed to return NULL or throw an exception.

The Mage class problem

One of the main challenges during unit testing in Magento is the Mage class. Mocking the behavior of the static Mage class for tests is not straight forward.

To solve this issue, two Mage classes are created. One called the MageFacade-class, which is used for the Mage class to fetch all static calls and delegate it to the MageClass-class, however the MageClass is not static but as well an auto-generated mock that is re-initialized for each test.

This way, we can not only bind trait stubs to the Mage class but also alter the behavior of the Mage class on runtime using the same methods described above.

(See testMockingMage() below)

Another problem is that no configuration XML is available for the unit test, so resolving model names like customer/session needs to be handled in a semi-automated way.

A name resolver can be set to the MageFacade class which will try its best to resolve any given name. You can register your own namespaces there, or create your own name resolver.

(See testSingleton() below)

Further Examples

Suppose you have the following model:

Now lets write a unit test for this class, remember you only want to test your logic. The magento logic of the parent class is not relevant at this point.

The mage-mocker however helps you by implementing some of the most important classes and methods, like the Varien_Object implementation.

All Mage_* and Varien_* classes are created and extended on the fly when running the test. They will have no method implementation, however they all implement the correct class hierarchies and class constants.

e.g:

Lets look at a simple test for the above example.

Since this is a UnitTest only focusing on your class no Magento initialization process is required and the test can run in a few milliseconds.

Note

This is still an early release and a proof of concept. It yet needs to be tested if this approach can be of use. If you have any ideas, feedback or issues let me know.


All versions of mage-mock with dependencies

PHP Build Version
Package Version
Requires jsiefer/class-mocker Version ~0.3
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 jsiefer/mage-mock contains the following files

Loading the files please wait ....