定制 digitalrevolution/accessorpair-constraint 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

digitalrevolution/accessorpair-constraint

Composer 安装命令:

composer require digitalrevolution/accessorpair-constraint

包简介

Digital Revolution PHPUnit Constraint for accessorpairs

README 文档

README

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): self;
    
    /**
     * 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): self;
    
    /**
     * 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): self;

    /**
     * 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): self;

    /**
     * @param string[] $excludedMethods A list of exact method names that should be excluded from the assertions.
     */
    public function setExcludedMethods(array $excludedMethods): self;

    /**
     * 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;
    
    /**
     * Callback function to allow for a custom value provider. For instance for final classes. The argument
     * is the class-string of the value, the return value should be the provided value. Return <code>null</code>
     * to skip the value provider and use the default value providers.
     *
     * @param callable(class-string): ?object $valueProvider
     */
    public function setValueProvider(callable $valueProvider): 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.

digitalrevolution/accessorpair-constraint 适用场景与选型建议

digitalrevolution/accessorpair-constraint 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 127.95k 次下载、GitHub Stars 达 9, 最近一次更新时间为 2020 年 04 月 02 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 digitalrevolution/accessorpair-constraint 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 digitalrevolution/accessorpair-constraint 我们能提供哪些服务?
定制开发 / 二次开发

基于 digitalrevolution/accessorpair-constraint 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 127.95k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 10
  • 点击次数: 16
  • 依赖项目数: 7
  • 推荐数: 0

GitHub 信息

  • Stars: 9
  • Watchers: 1
  • Forks: 10
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-04-02