horat1us/php-method-injection

PHP Trait to inject methods

1.0.0 2017-05-08 14:51 UTC

This package is auto-updated.

Last update: 2024-11-29 05:20:39 UTC


README

Scrutinizer Code Quality Code Coverage Build Status Latest Stable Version Total Downloads License

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