PHP Method Injection Trait
, (*1)
This trait allows you to extend your object with custom method dynamically., (*2)
Usage
You can use DynamicObject
or implement interface InjectMethodsInterface with trait InjectMethods on your custom object., (*3)
Simple example will output:, (*4)
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, (*5)
<?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:, (*6)
<?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, (*7)
<?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, (*8)