定制 r83dev/test-accessible 二次开发

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

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

r83dev/test-accessible

最新稳定版本:v1.2.0

Composer 安装命令:

composer require r83dev/test-accessible

包简介

Accessible trait to call protected methods, properties and constants in tests.

README 文档

README

A helper class for testing, especially for unit tests to access inaccessible methods or to get/inject properties.

Installation

Install via composer:

dcrr composer req --dev r83dev/test-accessible

Usage

Use the AccessibleTrait in your test:

class MyTest {
    use R83Dev\TestAccessible\AccessibleTrait;
}

Use one of the helper function to access private or protected stuff of an instantiated object.

Methods

Call inaccessible method

$this->callInaccessibleMethod($object, 'method_name', 'argument1', 'argument2', ...);

Get inaccessible property

$this->getInaccessibleProperty($object, 'property_name');

Set inaccessible property

$this->setInaccessibleProperty($object, 'property_name', 'new_property_value');

Set multiple inaccessible properties

$this->setInaccessibleProperties($object, ['property_name' => 'new_property_value']);

Get inaccessible constant

$this->getInaccessibleConstant($object, 'CONSTANT_NAME');

PHPUnitTest Example

This is just a features demo, not useful in terms of content.

Example of a unit to be tested:

class MyUnit
{
    protected const STATE_ON = 'on';
    protected const STATE_OFF = 'off';
    private string $state = self::STATE_ON;
    
    protected function setState(string $state): void
    {
        $this->state = $state;
    }
    
    public function getState(): string
    {
        return $this->state;
    }
}

Example unittest:

class MyUnitTest extends PHPUnit\Framework\TestCase
{
    use R83Dev\TestAccessible\AccessibleTrait;
    
    private ?MyUnit $unit;

    protected function setUp(): void
    {
        $this->unit = new MyUnit();
    }

    #[\PHPUnit\Framework\Attributes\Test]
    public function getStateShouldReturnCorrectInitialValue(): void
    {
        $this->assertSame(
            $this->getInaccessibleConstant($this->unit, 'STATE_ON'),
            $this->getInaccessibleProperty($this->unit, 'state')
        );
    }

    #[\PHPUnit\Framework\Attributes\Test]
    public function getStateReturnsNewValueFromSetter(): void
    {
        $off = $this->getInaccessibleConstant($this->unit, 'STATE_OFF');
        $this->callInaccessibleMethod($this->unit, 'setState', $off)
        $this->assertSame($off, $this->unit->getState());
    }

    #[\PHPUnit\Framework\Attributes\Test]
    public function getStateReturnsNewValueFromPropertyAllocation(): void
    {
        $this->assertSame(
            'unknown', 
            $this->setInaccessibleProperty($this->unit, 'state', 'unknown')->getState()
        );
    }
}

统计信息

  • 总下载量: 3.1k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-05-30