Download the PHP package c-malet/class-test without Composer
On this page you can find all versions of the php package c-malet/class-test. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package class-test
Class test
PHP testing library focused on testing classes mocking all of their constructor parameters, using Prophecy.
The main objective is to make testing a class quicker by automatizing the tested class instantiation with dummy parameters, and retrieve a whole new instance for each test. Particularly useful to test classes with many objects as constructor parameters that you don't want to be executed, such as helpers, loggers, services, repositories, ...
This library is based on the PHP object mocking framework Prophecy, and requires to know how to test with it, learn more about it here
Simple example
Installation
Prerequisites
Requires PHP 8.1 or greater and PHPUnit ^9.0 || ^10.0
Composer
Run composer command composer require c-malet/class-test
Usage
Test set up
Set up the test class for the tested class as such :
Extend the ClassTestCase class (which extends the PHPUnit TestCase)
Implement the two mandatory methods defining the tested class :
Testing
For each test, a whole new instance of the tested class is created and can be retrieved :
You can override the getTestedClass
method in each test case to inform your IDE the class type of $someClass
It returns a "real" instance of the tested class, it is not a mock, and the whole code of this class will be executed.
Contrariwise, every constructor parameter provided that matches an instantiatable class is transformed into a revealed Prophecy, in such a way that they will automatically handle any parameter and always return null by default.
These Prophecy mocks can be retrieved anytime during tests, to do anything you could do with Prophecy alone, using the getMock
method,
allowing you to override the default dummy set up as needed for your tests.
Mocks can be retrieved by their class name or by key, as described earlier in the 'Test set up' part
If your IDE fails to resolve the methods from the class name and gives warning, you can also get ProphecyMethod objects this way :
If you wish to use the internal mocks container for any other class to mock, for instance mocks you'd want to use in your tested methods, you can also create mocks and dummies that can be retrieved as other mocks with addNewMock
or addNewDummy
Full example
Contributing
See the CONTRIBUTING file.
Install / update project
You can install project with the following command:
And update with the following command:
NB: For the components, the composer.lock
file is not committed.
Testing & CI (Continuous Integration)
Tests
You can run unit tests (with coverage) on your side with following command:
For prettier output (but without coverage), you can use the following command:
Code Style
You also can run code style check with following commands:
You also can run code style fixes with following commands:
Static Analysis
To perform a static analyze of your code (with phpstan, lvl 9 at default), you can use the following command:
To ensure you code still compatible with current supported version at Deezer and futures versions of php, you need to run the following commands (both are required for full support):
Minimal supported version:
Maximal supported version:
CI Simulation
And the last "helper" commands, you can run before commit and push, is:
All versions of class-test with dependencies
phpunit/phpunit Version ^9.0||^10.0
phpspec/prophecy Version ^1.0
phpspec/prophecy-phpunit Version ^2.0