Completed
Push — refonte ( c098ff...64e01a )
by Arnaud
02:21
created

AdminTestBase::setPrivateProperty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
dl 8
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
3
namespace LAG\AdminBundle\Tests;
4
5
use Closure;
6
use Exception;
7
use PHPUnit\Framework\TestCase;
8
use PHPUnit_Framework_MockObject_MockObject;
9
use ReflectionClass;
10
use Symfony\Component\Config\FileLocator;
11
use Symfony\Component\DependencyInjection\ContainerBuilder;
12
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
13
14
class AdminTestBase extends TestCase
15
{
16
    /**
17
     * Assert that the given service class is configured in the services.yaml
18
     *
19
     * @param string $serviceClass
20
     */
21
    public function assertServiceExists(string $serviceClass)
22
    {
23
        $containerBuilder = new ContainerBuilder();
24
        $locator = new FileLocator([
25
            __DIR__.'/../../src/Resources/config',
26
        ]);
27
        $loader = new YamlFileLoader($containerBuilder, $locator);
28
        $loader->load('services.yaml');
29
        $exists = false;
30
31
        foreach ($containerBuilder->getDefinitions() as $definition) {
32
            if ($serviceClass === $definition->getClass()) {
33
                $exists = true;
34
            }
35
        }
36
        $this->assertTrue($exists);
37
    }
38
39
    /**
40
     * Assert that an exception is raised in the given code.
41
     *
42
     * @param $exceptionClass
43
     * @param Closure $closure
44
     */
45
    protected function assertExceptionRaised($exceptionClass, Closure $closure)
46
    {
47
        $e = null;
48
        $isClassValid = false;
49
        $message = '';
50
51
        try {
52
            $closure();
53
        } catch (Exception $e) {
54
            if (get_class($e) == $exceptionClass) {
55
                $isClassValid = true;
56
            }
57
            $message = $e->getMessage();
58
        }
59
        $this->assertNotNull($e, 'No Exception was thrown');
60
        $this->assertTrue($isClassValid, sprintf('Expected %s, got %s (Exception message : "%s")',
61
            $exceptionClass,
62
            get_class($e),
63
            $message
64
        ));
65
    }
66
    
67
    /**
68
     * @param $class
69
     *
70
     * @return PHPUnit_Framework_MockObject_MockObject|mixed
71
     */
72
    protected function getMockWithoutConstructor($class)
73
    {
74
        return $this
75
            ->getMockBuilder($class)
76
            ->disableOriginalConstructor()
77
            ->getMock()
78
        ;
79
    }
80
    
81 View Code Duplication
    protected function setPrivateProperty($object, $property, $value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
82
    {
83
        $reflection = new ReflectionClass($object);
84
        
85
        $property = $reflection->getProperty($property);
86
        $property->setAccessible(true);
87
        $property->setValue($object, $value);
88
    }
89
    
90 View Code Duplication
    protected function getPrivateProperty($object, $property)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
91
    {
92
        $reflection = new ReflectionClass($object);
93
        
94
        $property = $reflection->getProperty($property);
95
        $property->setAccessible(true);
96
        
97
        return $property->getValue($object);
98
    }
99
}
100