Download the PHP package otgs/unit-tests-framework without Composer

On this page you can find all versions of the php package otgs/unit-tests-framework. 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 unit-tests-framework

PhpUnit

[^1]: it is recommended to keep your PHPUnit tests into a subfolder, because there are good changes you want to add other kind of tests (e.g. QUnit, PHPSpec, etc.) [^2]: in case, for whatever reason, you can't do that, copy this file in tests/phpunit or wherever you find it more convenient [^3]: In some cases, you may want to use composer require --dev otgs/unit-tests-framework:dev-develop. When you do that, you may also need to run this command first composer config minimum-stability dev [^4]: If you've placed the phpunit.xml you need to either move to that directory first, or tell phpunit where the file is with phpunit --configuration path/to/phpunit.xml

How to use the OTGS_TestCase class

In order to take the most of this library, all your tests classes should extext OTGS_TestCase.

Once you do that, this will happen:

  1. \OTGS_TestCase::setupBeforeClass:
    • $_GET and $_POST are set to an empty array
    • An instance of FactoryMuffin is provided: you can refer to it as self::$fm (see "Resources and dependencies" for more details)
  2. \OTGS_TestCase::setUp
    • FunctionMocker is initialized (see "Resources and dependencies" for more details)
    • WP_Mock is initialized (see "Resources and dependencies" for more details)
  3. \OTGS_TestCase::tearDown
    • WP_Mock is destroyed
    • FunctionMocker is destroyed
    • Mockery is destroyed (just in case it has been used)
  4. \OTGS_TestCase::tearDownAfterClass
    • Deletes all models created with FactoryMuffin

Mock WP Core functions

This class also provide an helper method to quickly mock the functions defined by WordPress by using the \OTGS_TestCase::get_mocked_wp_core_functions which returns an instance of OTGS_Mocked_WP_Core_Functions.

OTGS_Mocked_WP_Core_Functions organize mocks in methods named using the same name of the file where the function is defined in WordPress codebase.

For instance, to mock of all functions defined in post.php like get_post, in your test you should simply call $this->get_mocked_wp_core_functions()->post().

To mock add_query_arg yo call $this->get_mocked_wp_core_functions()->functions() because add_query_arg is defined in functions.php.

OTGS_Mocked_WP_Core_Functions tries to handle dependencies.

So, if you call $this->get_mocked_wp_core_functions()->post() to mock wp_insert_post, you automatically call $this->get_mocked_wp_core_functions()->post(), so to get all the meta related functions mocked as well.
Finally, there is a "mock all" method you could use (though is discouraged) with $this->mock_all_core_functions().

Stub WP common classes

\OTGS_TestCase provides a helpful way to quickly get a stub of some of the most commonly used classes in WordPress.

By calling $this->stubs->wpdb() you will get a stub you can pass as a dependency of the classes you are testing. If you need to control the behavior of this stub, you just use the standard PHPUnit mock helpers.

E.g. 1:

E.g. 2:

Other stubs you can get:

It is important to know that, if you only need the class to be defined (e.g. hard-dependency, or sub-classing), you don't need to assig the stub to a variable: just call the method.

A good example is with WordPress' widgets, where you may have your own widget which is supposed to extend WP_Widget.

In this case, unless you want to mock some of the WP_Widget methods, you simply call $this->stubs->WP_Widget(), then write your tests.
The class which extends WP_Widget will find a definition of this class, with all the methods (doing nothing).

Resources and dependencies

Below are some resources on writing unit tests which lead to the creation of this library and links to the libraries included here:


All versions of unit-tests-framework with dependencies

PHP Build Version
Package Version
Requires roave/security-advisories Version dev-master
10up/wp_mock Version ^0.4
lucatume/function-mocker Version ^1.3
symfony/dom-crawler Version ^5.0
symfony/css-selector Version ^5.0
phpunit/phpunit Version ^8.5
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 otgs/unit-tests-framework contains the following files

Loading the files please wait ....