Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

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