定制 konsulting/exposer 二次开发

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

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

konsulting/exposer

Composer 安装命令:

composer require konsulting/exposer

包简介

README 文档

README

Although for the most part only public methods of classes are unit tested, it's sometimes necessary or helpful to have access to protected or private methods and properties. Exposer provides a convenient way to do this whilst keeping your tests clear and concise.

Installation

Install via Composer:

 $ composer require konsulting/exposer

Usage

Consider the following class:

class ClassUnderTest
{
    protected $secret = 'My secret';
    
    protected static $anotherSecret = 'My static secret';
    
    protected function add($number1, $number2)
    {
        return $number1 + $number2;
    }
    
    protected static function multiply($number1, $number2)
    {
        return $number1 * $number2;
    }
}

There are multiple ways to test the protected methods and properties on this class.

Base Exposer

The most direct way is with the BaseExposer class. The subject must be passed into each method, and may be either an instance or the (string) class name.

use Konsulting\Exposer\BaseExposer;

// With an instance
BaseExposer::hasMethod(new ClassUnderTest, 'add');                          // true
BaseExposer::invokeMethod(new ClassUnderTest, 'add', [1, 1]);               // 2
BaseExposer::getProperty(new ClassUnderTest, 'secret');                     // 'My secret'

// Static context
BaseExposer::hasMethod(ClassUnderTest::class, 'multiply');                  // true
BaseExposer::invokeMethod(ClassUnderTest::class, 'multiply', [2,2]);        // 4
BaseExposer::getProperty(ClassUnderTest::class, 'anotherSecret');           // 'My static secret'

Exposer

The Exposer class allows the use of a class's inaccessible methods and properties as if they were public.

use Konsulting\Exposer\Exposer;

$exposer = Exposer::make(new ClassUnderTest);

$exposer->add(1, 1);                                // 2
$exposer->multiply(2, 2);                           // 4
$exposer->secret;                                   // 'My secret'
$exposer->anotherSecret;                            // 'My static secret'

// These non-magic methods are also available
$exposer->invokeMethod('add', [1, 1]);               // 2
$exposer->getProperty('secret');                     // 'My secret'

Exposer can also access class's static methods and properties without the need to provide an instance.

use Konsulting\Exposer\Exposer;

$exposer = Exposer::make(ClassUnderTest::class);

$exposer->multiply(2, 2);                           // 4
$exposer->anotherSecret;                            // 'My static secret'

$exposer->invokeMethod('multiply', [2, 2]);         // 4
$exposer->getProperty('anotherSecret');             // 'My static secret'

Note: Static methods and properties are available from an instance context, but of course non-static methods and properties are not available from a static context.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-02-21

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固