Giter Club home page Giter Club logo

accessorpair-constraint's Introduction

Minimum PHP Version

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);
}

About us

At 123inkt (Part of Digital Revolution B.V.), every day more than 30 developers are working on improving our internal ERP and our several shops. Do you want to join us? We are looking for developers.

accessorpair-constraint's People

Contributors

bram123 avatar prinsfrank avatar avgeeklucky avatar dattaraj-devne avatar frankdekker avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.