digitalrevolution / accessorpair-constraint
Digital Revolution PHPUnit Constraint for accessorpairs
Installs: 58 004
Dependents: 6
Suggesters: 0
Security: 0
Stars: 9
Watchers: 2
Forks: 8
Open Issues: 2
Requires
- php: >=8.1
- doctrine/inflector: ^2.0
- phpdocumentor/type-resolver: ^1.9
- phpunit/phpunit: ^10.0 || ^11.0
Requires (Dev)
- phpmd/phpmd: @stable
- phpstan/extension-installer: ^1.1
- phpstan/phpstan: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpstan/phpstan-strict-rules: ^1.0
- roave/security-advisories: dev-latest
- squizlabs/php_codesniffer: ^3.6
- dev-master
- v2.4.1
- v2.4.0
- v2.3.1
- v2.3.0
- v2.2.1
- v2.2.0
- v2.1.18
- v2.1.17
- v2.1.16
- v2.1.15
- v2.1.14
- v2.1.13
- v2.1.12
- v2.1.11
- v2.1.10
- v2.1.9
- v2.1.8
- v2.1.7
- v2.1.6
- v2.1.5
- v2.1.4
- v2.1.3
- v2.1.2
- v2.1.1
- v2.1.0
- v2.0.x-dev
- v2.0.2
- v2.0.1
- v2.0.0
- v1.1.2
- v1.1.1
- v1.1.0
- v1.0.2
- v1.0.1
- v1.0.0
- dev-renovate/major-phpstan-packages
This package is auto-updated.
Last update: 2024-11-11 09:42:25 UTC
README
AccessorPair Constraint
A way to automatically unit test (and cover) all getter and setters of your data class.
Installation
$ composer require --dev digitalrevolution/accessorpair-constraint
Usage
Once you've imported the AccessorPairAsserter trait into your own test class,
or TestCase base class, you can call the assertAccessorPairs
method to automatically test all your getters/setters.
If you want to keep track of the coverage, configure the PHPUnit annotation to cover all methods of your class.
Optionally, the asserter can also check the initial values of all your class properties and whether or not calling the getter before having called the setter will work.
Example
<?php use DigitalRevolution\AccessorPairConstraint\AccessorPairAsserter; use PHPUnit\Framework\TestCase; /** * @coversDefaultClass \DataClass * @covers ::<public> */ class DataClassTest extends TestCase { use AccessorPairAsserter; public function testDataClass() { static::assertAccessorPairs(DataClass::class); } }
Example: Simple DataClass
In this example the data class consists of getter and setter methods and a constructor to set the properties. The AccessorPair constraint can match the setter methods with the getter methods and will execute tests for each pair. The constraint is also able to match the constructor parameters with the getter methods and will test these pairs as well.
<?php class DataClass { private $property; private $default; public function __construct(string $property, bool $default) { $this->property = $property; $this->default = $default; } public function getProperty(): string { return $this->property; } public function setProperty(string $param): self { $this->property = $param; return $this; } public function isDefault(): bool { return $this->default; } public function setDefault(bool $param): self { $this->default = $param; return $this; } }
Example: Configuring the constraint
In this example the constructor parameter $property will be matched with the method getProperty, and the method setProperty with getProperty. Because the constructor changes the data, it is not possible for the AccessorPair constraint to assert the correct working of your class. It is still possible to test the method pair setProperty-getProperty using the constraint config.
The data class
<?php class DataClass { private $property; public function __construct(string $property) { $this->property = strtoupper($property); } public function setProperty(string $property) { $this->property = $property; } public function getProperty(): string { return $this->property; } }
The unittest
<?php use DigitalRevolution\AccessorPairConstraint\AccessorPairAsserter; use DigitalRevolution\AccessorPairConstraint\Constraint\ConstraintConfig; use PHPUnit\Framework\TestCase; /** * @coversDefaultClass \DataClass * @covers ::<public> */ class DataClassTest extends TestCase { use AccessorPairAsserter; public function testDataClass() { static::assertAccessorPairs(DataClass::class, (new ConstraintConfig())->setAssertConstructor(false)); } }
Possible configuration options
<?php class ConstraintConfig { /** * Enabled by default. * Let the constraint pair all getter and setter methods, * and pass test data to the setter to assert that the getter returns the exact same value. */ public function setAssertAccessorPair(bool $assertAccessorPair); /** * Enabled by default. * Let the constraint pair the constructor's parameters with the class' getter methods. * These pairs will be tested in the same ways as the getter/setter method pairs. */ public function setAssertConstructor(bool $assertConstructor); /** * Disabled by default. * When enabled, the getter methods are called on an empty instance of the test object. * This makes sure that all the properties have the correct default type, * conforming the getter return typehint. */ public function setAssertPropertyDefaults(bool $assertPropertyDefaults); /** * Enabled by default. * When disabled, only the direct class methods will be asserted and none of the parent's * class methods. */ public function setAssertParentMethods(bool $assertParentMethods); /** * A list of exact method names that should be excluded from the assertions. */ public function setExcludedMethods(array $excludedMethods); /** * Callback function to create the constructor arguments for the class under test. * * Test data or mocks will be used by default. * * @param callable(): mixed[] $callback * @return $this */ public function setConstructorCallback(callable $callback): self; }
About us
At 123inkt (Part of Digital Revolution B.V.), every day more than 50 development professionals are working on improving our internal ERP and our several shops. Do you want to join us? We are looking for developers.