PHP code example of digitalrevolution / accessorpair-constraint

1. Go to this page and download the library: Download digitalrevolution/accessorpair-constraint library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

digitalrevolution / accessorpair-constraint example snippets




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



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



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



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



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