Passed
Push — develop ( 3c5942...636dd4 )
by Nikolay
04:57
created

AbstractUnitTest::invokeMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MikoPBX\Tests\Unit;
6
7
use MikoPBX\Core\Config\RegisterDIServices;
8
use Phalcon\Di;
9
use Phalcon\Di\FactoryDefault;
10
use Phalcon\Incubator\Test\PHPUnit\UnitTestCase;
11
use PHPUnit\Framework\IncompleteTestError;
12
13
abstract class AbstractUnitTest extends UnitTestCase
14
{
15
    private bool $loaded = false;
16
17
    public function setUp(): void
18
    {
19
        parent::setUp();
20
        $di = new FactoryDefault\Cli();
21
        Di::reset();
22
        Di::setDefault($di);
23
        require_once __DIR__ . '/../../src/Common/Config/ClassLoader.php';
24
        RegisterDIServices::init();
25
        $this->loaded = true;
26
    }
27
28
    public function __destruct()
29
    {
30
        if (!$this->loaded) {
31
            throw new IncompleteTestError(
32
                "Please run parent::setUp()."
33
            );
34
        }
35
    }
36
37
    /**
38
     * Call protected/private method of a class.
39
     *
40
     * @param object &$object     Instantiated object that we will run method on.
41
     * @param string  $methodName Method name to call
42
     * @param array   $parameters Array of parameters to pass into method.
43
     *
44
     * @return mixed Method return.
45
     * @throws \ReflectionException
46
     */
47
    public function invokeMethod(&$object, $methodName, array $parameters = array())
48
    {
49
        $reflection = new \ReflectionClass(get_class($object));
50
        $method = $reflection->getMethod($methodName);
51
        $method->setAccessible(true);
52
53
        return $method->invokeArgs($object, $parameters);
54
    }
55
}