|
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) |
|
|
|
|
|
|
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) |
|
|
|
|
|
|
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
|
|
|
|
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.