Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/123inkt/accessorpair-constraint
PHPUnit AccessorPair constraint
https://github.com/123inkt/accessorpair-constraint
accessorpair php phpunit phpunit-assertions phpunit-constraint phpunit-extension
Last synced: 24 days ago
JSON representation
PHPUnit AccessorPair constraint
- Host: GitHub
- URL: https://github.com/123inkt/accessorpair-constraint
- Owner: 123inkt
- License: mit
- Created: 2020-03-25T16:16:41.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2023-10-19T12:50:07.000Z (about 1 year ago)
- Last Synced: 2023-12-19T15:02:02.785Z (about 1 year ago)
- Topics: accessorpair, php, phpunit, phpunit-assertions, phpunit-constraint, phpunit-extension
- Language: PHP
- Homepage:
- Size: 250 KB
- Stars: 9
- Watchers: 2
- Forks: 6
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
[![Minimum PHP Version](https://img.shields.io/badge/php-%3E%3D%207.4-8892BF)](https://php.net/)
# 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*/
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
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
property = strtoupper($property);
}public function setProperty(string $property)
{
$this->property = $property;
}public function getProperty(): string
{
return $this->property;
}
}
```##### The unittest
```php*/
class DataClassTest extends TestCase
{
use AccessorPairAsserter;public function testDataClass()
{
static::assertAccessorPairs(DataClass::class, (new ConstraintConfig())->setAssertConstructor(false));
}
}
```##### Possible configuration options
```php