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