Download the PHP package sytzez/data-object-tester without Composer
On this page you can find all versions of the php package sytzez/data-object-tester. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package data-object-tester
Data Object Tester
Should you even unit test your data object classes? With DataObjectTester it's so effortless that you don't even need to ask yourself that question! It automates doing automated tests in PHPUnit for immutable data objects. Within a minute, you'll have written a test that covers all possible cases. Check out the code below to see how it works.
Requirements
- PHP 8.0 or above
- PHPUnit
Installation
`
Usage
Consider the following data class:
To make sure the getter methods always return the given values, all you need to do is write a test that extends the DataObjectTestCase:
(Alternatively to extending the DataObjectTestCase, you could also use TestsDataObjects)
This will test that all the getters exist, and that they give back the values provided in the constructor.
The array in the class expectation lists the getters, in the same order as their respective constructor arguments. For each property, any number of possible values can be given. The tester will construct a couple of objects using those values, and assert that the getters return the right values.
Features
Testing optional arguments
If an argument can be optional (having a default value), you can use a DefaultPropertyCase to define the default expected value.
Testing validation exceptions
If passing certain values should cause an error or exception to be thrown during instantiation,
use the ConstructorExceptionPropertyCase. So if your constructor looks like this:
You can test it like this:
If multiple arguments should cause an exception, the test will assert that one the possible exceptions will be thrown.
Testing transformative properties
If your data class alters the passed in values in some way, you can use the TransformativePropertyCase class in your class expectation.
Let's say your getter looks like this:
Then you could write the class expectation like this:
Testing with closures
You can use the ClosurePropertyCase to do your own validation of getter output by writing a closure returning a bool.
Example:
Test case generator
By default, a minimal amount of objects is created, covering each specified property value at least once.
But if you wish to cover every possible combination of property values, you can use a MaximalCaseGenerator,
by passing it as the second argument to testDataObjects, like so:
In this case, 12 (2 3 2) objects will be instantiated and tested, instead of the minimum of 3 cases.
You can cap the number of objects by providing an argument to the generator, e.g. new MaximalCaseGenerator(20).
The default cap is 100.
You can also provide your own case generators by implementing the CaseGeneratorStrategy.
Special cases
If there is a case not (yet) covered by the package, you can of course still add your own @test methods to the test case.
Code Quality
Unit tests
The project has a testing coverage of 100% of lines using PHPUnit.
PHPStan
PHPstan reports 0 errors on the maximum level 8.
License
The MIT License (MIT). Please see License File for more information.