horat1us / php-method-injection
PHP Trait to inject methods
Installs: 1 539
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 3
Forks: 0
Open Issues: 0
Requires
- php: >=7.0
Requires (Dev)
- phpunit/phpunit: 5.2.*
This package is auto-updated.
Last update: 2024-10-29 04:59:18 UTC
README
This trait allows you to extend your object with custom method dynamically.
Usage
You can use DynamicObject or implement interface InjectMethodsInterface with trait InjectMethods on your custom object.
Simple example will output:
Hello, Alexander
Hello, Letnikow
Array
(
[arguments] => Array
(
[0] => Man
)
[methodName] => welcome
[object] => Horat1us\MethodInjection\DynamicObject
[message] => Injected method welcome not found in Horat1us\MethodInjection\DynamicObject
)
About
This package is created for using in tests. For example, you have interface
<?php interface ServiceInterface { public function run($args); }
and you need to test arguments which will be passed to this service. You can define fake service:
<?php use Horat1us\MethodInjection\InjectMethodsInterface; use Horat1us\MethodInjection\InjectMethods; /** * @method Test($args) */ class FakeService implements InjectMethodsInterface, ServiceInterface { use InjectMethods; public function run($args) { $this->Test($args); } }
and use it in your test service
<?php $service = new FakeService(['Test' => function() { // Your tests here }]); ServiceRunner::run($service);
Install
composer require horat1us/php-method-injection
Tests
phpunit
License
This project is open-sourced software licensed under the MIT license