Passed
Push — master ( 134f11...d02ad4 )
by Alexander
08:37
created

TestCase::getInaccessibleProperty()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
nc 4
nop 3
dl 0
loc 14
rs 9.9666
c 1
b 0
f 0
1
<?php
2
3
namespace Yiisoft\Mailer\SwiftMailer\Tests;
4
5
use PHPUnit\Framework\TestCase as BaseTestCase;
6
use Psr\Container\ContainerInterface;
7
use Yiisoft\Di\Container;
8
use Yiisoft\Mailer\MailerInterface;
9
use Yiisoft\Mailer\SwiftMailer\Mailer;
10
11
abstract class TestCase extends BaseTestCase
12
{
13
    /**
14
     * @var ContainerInterface $container
15
     */
16
    private $container;
17
18
    protected function setUp(): void
19
    {
20
        parent::setUp();
21
        $config = require __DIR__ . '/config.php';
22
        $this->container = new Container($config);
23
    }
24
25
    protected function tearDown(): void
26
    {
27
        $this->container = null;
28
        parent::tearDown();
29
    }
30
31
    protected function get($id)
32
    {
33
        return $this->container->get($id);
34
    }
35
36
    /**
37
     * @return Mailer mailer instance.
38
     */
39
    protected function getMailer(): Mailer
40
    {
41
        return $this->get(MailerInterface::class);
42
    }
43
44
    /**
45
     * Gets an inaccessible object property.
46
     * @param $object
47
     * @param $propertyName
48
     * @param bool $revoke whether to make property inaccessible after getting
49
     * @return mixed
50
     * @throws \ReflectionException
51
     */
52
    protected function getInaccessibleProperty($object, $propertyName, bool $revoke = true)
53
    {
54
        $class = new \ReflectionClass($object);
55
        while (!$class->hasProperty($propertyName)) {
56
            $class = $class->getParentClass();
57
        }
58
        $property = $class->getProperty($propertyName);
59
        $property->setAccessible(true);
60
        $result = $property->getValue($object);
61
        if ($revoke) {
62
            $property->setAccessible(false);
63
        }
64
65
        return $result;
66
    }
67
}
68