Download the PHP package arron/testit without Composer

On this page you can find all versions of the php package arron/testit. 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 testit

TestIt Library

TestIt is an add-on library for PHPUnit that extends mocking engine and allows comfort testing of class interactions with surrounding environment. Are you tired using $this->getMock()->expects()->method()->with()->will() constructions? TestIt is for you :-)

Usual unit test consists from several steps:

TestIt can help you with most of the steps.

Compatibility

In latest version we are running our unit tests against PHP versions 7.0, 7.1, 7.2.

Please report any bugs into GitHub. Thank you.

License

You can use TestIt library under MIT license.

Installation

Use composer to install TestIt. Just add arron/testit project as your dependency.

Testing with TestIt

TestIt offers own TestCase class that is inherited from \PHPUnit_Framework_TestCase and enhancing it with some features. It is meant to be base class for your tests. It is helping you with creating mocks of your class dependencies and verifying those dependencies are called as expected. To use this features, just inherit your test base class from \Arron\TestIt\TestCase class and you are free to go.

The best way how to profit from this library is to integrate it to your tests so you will be able to use all those features even more easily.

For examples, check examples directory.

Creating of the object to test

There is abstract method createTestObject() defined in \Arron\TestIt\TestCase namespace. It should return created instance ready to use for testing. So createTestObject() is the right place for any initialization mock injections etc. It will be called during the setUp call. So just concentrate on new object creation, the rest is in hands of TestIt :-) If you need to call any expectations about any dependencies called in the time of test object creation, overwrite initializationExpectations method and place your expectations there. This method will be called just before object creation in the setUp method.

Mocking of dependencies

TestIt is helping you with creating of mocked classes. It contains something like object locator so, it is not creating new mock every time. But you can get the best value if you integrate TestIt capabilities with your DI.

Creating mocked class

You create mock by simply call simple method in your test class:

Notice that all created mocks have to have unique name. With this name, you are referencing them in your expectations. It also allows you to have more mocks of the same class (with different names). Mock-creation engine uses native PHPUnit mocking, automatically mocking all public methods and adds call tracking feature. All you have to do is to pass the mock to your class you are testing to be called from there.

Creating mocks of global functions

TestIt allows you to mock global functions. Do it by calling mockGlobalFunction function.

Namespace argument is the namespace your time() function is called from. If not provided, it will be set to the namespace mockGlobalFunction is called from.

This is done by a namespace trick. If you are in namespace and call function, it will be searched first in the current namespace and then in the global space. So if not defined in the namespace, it will fall-back to global function. mockGlobalFunction will define mock of the function in specific namespace so the call will no longer fall-back to global space and mocked function will be called. Notice, that once the function is defined in some namespace, it will be called from anywhere in this namespace. Nevertheless TestIt will check and won't redefine it again. But in terms of readability of your code, call mockGlobalFunction everywhere you need it to be clear you are mocking something.

Therefore there is limitation here. You can mock only functions that are called with unqualified name.

There is one thing that is not working yet. Functions with output argument can be mocked but output argument won't work. The main problem is, how to detect output arguments? Any ideas? :-)

Expecting what should happen

In your tests, you are testing inputs and outputs, but you also have to test class interactions with its dependencies. Meaning you have to test, that there were correct methods called with correct parameters in correct order. These methods usually return some data so it is necessary to ensure, that this data will be returned.

In TestIt it is very easy to do so.

Methods arguments will be expected to be exactly the same as you passed them (in the array). Empty array means no arguments, NULL means that you don't care so any arguments will be accepted. Optional arguments (with default value) can be omitted.

As for return value, all values passed here will be returned by mock call unchanged except instances of \Exception class (and its ancestors of course). These instances will be thrown as exception. It allows you to simulate cases where one of your dependencies fails.

Defining state of tested object

Before launching the test, you must put tested object to defined state. Against this state you will assert changes after the test. In order to have real unit test, you shouldn’t use object's set methods etc. TestIt provides you two methods for this purpose.

setPropertyInTestSubject

getPropertyFromTestSubject

You can set/get any defined property from tested object. It is using reflection. I noticed random occurrences of errors here. I wasn't able to catch specific circumstances so far, but most of the time, it is working fine :-)

Call method to test

Use getTestObject() method to access instance of test object. If not created yet, fresh instance will be created using the createTestObject() method.

From time to time, it can be useful to call/test also a protected/private method. TestIt provides you a simple method to call these methods.

It is not a good habit to call inaccessible methods but it can be handy :-)

Asserting end conditions

TestIt will automatically assert your expectations. For every dependency call these assertions will be done runtime (in this order):

Remote debugging of PHP cli scripts

From version 1.2.0 TestIt comes with bash script, that allowes you to debug PHP cli scripts, running on server, on your local mashine. You can find debugphpscript in your vendor/bin directory. All you have to do is configure your IDE to be able of remote debugging with xDebug (consult help for your IDE), set your IDE for listening for incoming xDebug session and than launch your cli script you want to debug through debugphpscript. You have to specify session_id of xDebug session, in some cases (ex. PhpStorm) you have to specify name of you server configuration in IDE, IP address of your local mashine is needed and of course, you have to specify what command you want to run.


All versions of testit with dependencies

PHP Build Version
Package Version
Requires php Version >=7.0
phpunit/phpunit Version >=6.5 <8.0.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 arron/testit contains the following files

Loading the files please wait ....