markbaker / spymaster
SpyMaster 是一个小型库,用于测试,允许访问正在测试的类中受保护或私有属性的值,而不需要使用反射修改类。
1.1.0
2020-12-23 15:44 UTC
Requires
- php: ^7.0 || ^8.0
Requires (Dev)
- dealerdirect/phpcodesniffer-composer-installer: ^0.7.0
- phpcompatibility/php-compatibility: ^9.0
- phpdocumentor/phpdocumentor: 2.*
- phploc/phploc: ^4.0
- phpmd/phpmd: 2.*
- phpunit/phpunit: ^6.0 || ^7.0 || ^8.0 || ^9.3
- sebastian/phpcpd: ^3.0 || ^4.0 || ^6.0
- squizlabs/php_codesniffer: ^3.4
- yoast/phpunit-polyfills: 1.x-dev
This package is auto-updated.
Last update: 2024-08-23 23:22:19 UTC
README
SpyMaster 是一个小型库,用于测试,允许访问正在测试的类中受保护或私有属性的值,或执行受保护或私有方法,而不需要使用反射修改类。
要求
- PHP 版本 7.0.0 或更高
安装
使用 composer,可以是
composer require markbaker/spymaster
或将库添加到现有的 composer.json 文件中,并让 composer 的自动加载器发挥作用。
或者您可以从 github 下载文件,并包含 bootstrap.php 文件以启用 SpyMaster 自动加载器
用法
在 /examples
文件夹中有一些使用示例。
// Instantiate your object
$myObject = new myObject();
// Infiltrate a read-only Spy that can view the properties of $myObject
$spy = (new SpyMaster\SpyMaster($myObject))
->infiltrate();
// Access the $value property of $myObject
// Any property of $myObject can be accessed, whether it is public, protected or private
echo $spy->value;
// Instantiate your object
$myObject = new myObject();
// Infiltrate a read-write spy that can both read and modify the properties of $myObject
$spy = (new SpyMaster\SpyMaster($myObject))
->infiltrate(SpyMaster\SpyMaster::SPY_READ_WRITE);
// Access the $value property of $myObject
// Any property of $myObject can be accessed, whether it is public, protected or private
echo $spy->value;
// A Read-Write Spy also allows you to set new values for those properties
$spy->value += 1000;
echo $spy->value;
间谍无法取消属性,也不能访问间谍渗透后动态创建的属性。
要执行对象内的私有或受保护方法,您可以使用 Manipulator
。
// Instantiate your object
$myObject = new myObject();
// Create a Manipulator
$Manipulator = new Manipulator();
// Call the Manipulator's execute() method, passing in the object and name of the method to execute, together with any arguments
$result = $Manipulator->execute($myObject, 'add', 3, 5);
// This example would execute the add() method of the myObject instance with arguments 3 and 5
许可
SpyMaster 在 MIT 许可下发布