|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the EloyekunlePermissionsBundle package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Elijah Oyekunle <https://elijahoyekunle.com/> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Eloyekunle\PermissionsBundle\Tests\DependencyInjection; |
|
13
|
|
|
|
|
14
|
|
|
use Eloyekunle\PermissionsBundle\DependencyInjection\EloyekunlePermissionsExtension; |
|
15
|
|
|
use PHPUnit\Framework\TestCase; |
|
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
17
|
|
|
use Symfony\Component\Yaml\Parser; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* EloyekunlePermissionsExtension Test. |
|
21
|
|
|
* |
|
22
|
|
|
* @author Elijah Oyekunle <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
class EloyekunlePermissionsExtensionTest extends TestCase |
|
25
|
|
|
{ |
|
26
|
|
|
/** @var ContainerBuilder */ |
|
27
|
|
|
private $container; |
|
28
|
|
|
|
|
29
|
|
|
/** @var EloyekunlePermissionsExtension */ |
|
30
|
|
|
private $extension; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* {@inheritdoc} |
|
34
|
|
|
*/ |
|
35
|
|
|
public function setUp() |
|
36
|
|
|
{ |
|
37
|
|
|
$this->container = new ContainerBuilder(); |
|
38
|
|
|
$this->extension = new EloyekunlePermissionsExtension(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* {@inheritdoc} |
|
43
|
|
|
*/ |
|
44
|
|
|
public function tearDown() |
|
45
|
|
|
{ |
|
46
|
|
|
unset($this->container, $this->extension); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException |
|
51
|
|
|
*/ |
|
52
|
|
|
public function testLoadThrowsExceptionUnlessDatabaseDriverSet() |
|
53
|
|
|
{ |
|
54
|
|
|
$config = $this->getEmptyConfig(); |
|
55
|
|
|
unset($config['db_driver']); |
|
56
|
|
|
$this->extension->load([$config], $this->container); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException |
|
61
|
|
|
*/ |
|
62
|
|
|
public function testLoadThrowsExceptionUnlessDatabaseDriverIsValid() |
|
63
|
|
|
{ |
|
64
|
|
|
$config = $this->getEmptyConfig(); |
|
65
|
|
|
$config['db_driver'] = 'bar'; |
|
66
|
|
|
$this->extension->load([$config], $this->container); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function testLoadManagerClassWithDefaults() |
|
70
|
|
|
{ |
|
71
|
|
|
$this->createEmptyConfig(); |
|
72
|
|
|
|
|
73
|
|
|
$this->assertParameter('orm', 'eloyekunle_permissions.storage'); |
|
74
|
|
|
$this->assertAlias('eloyekunle_permissions.role_manager.default', 'eloyekunle_permissions.role_manager'); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function testModelClassWithDefaults() |
|
78
|
|
|
{ |
|
79
|
|
|
$this->createEmptyConfig(); |
|
80
|
|
|
|
|
81
|
|
|
$this->assertParameter('Acme\MyBundle\Document\Role', 'eloyekunle_permissions.model.role.class'); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
protected function createEmptyConfig() |
|
85
|
|
|
{ |
|
86
|
|
|
$config = $this->getEmptyConfig(); |
|
87
|
|
|
$this->extension->load(array($config), $this->container); |
|
88
|
|
|
$this->assertTrue($this->container instanceof ContainerBuilder); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
protected function createFullConfig() |
|
92
|
|
|
{ |
|
93
|
|
|
$config = $this->getFullConfig(); |
|
94
|
|
|
$this->extension->load(array($config), $this->container); |
|
95
|
|
|
$this->assertTrue($this->container instanceof ContainerBuilder); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* getEmptyConfig. |
|
100
|
|
|
* |
|
101
|
|
|
* @return array |
|
102
|
|
|
*/ |
|
103
|
|
|
protected function getEmptyConfig() |
|
104
|
|
|
{ |
|
105
|
|
|
$yaml = <<<EOF |
|
106
|
|
|
db_driver: orm |
|
107
|
|
|
role_class: Acme\MyBundle\Document\Role |
|
108
|
|
|
EOF; |
|
109
|
|
|
$parser = new Parser(); |
|
110
|
|
|
|
|
111
|
|
|
return $parser->parse($yaml); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* getFullConfig. |
|
116
|
|
|
* |
|
117
|
|
|
* @return array |
|
118
|
|
|
*/ |
|
119
|
|
|
protected function getFullConfig() |
|
120
|
|
|
{ |
|
121
|
|
|
$yaml = <<<EOF |
|
122
|
|
|
db_driver: orm |
|
123
|
|
|
role_class: Acme\MyBundle\Document\Role |
|
124
|
|
|
module: |
|
125
|
|
|
definitions_path: /var/www/config/modules |
|
126
|
|
|
EOF; |
|
127
|
|
|
$parser = new Parser(); |
|
128
|
|
|
|
|
129
|
|
|
return $parser->parse($yaml); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @param string $value |
|
134
|
|
|
* @param string $key |
|
135
|
|
|
*/ |
|
136
|
|
|
private function assertAlias($value, $key) |
|
137
|
|
|
{ |
|
138
|
|
|
$this->assertSame($value, (string) $this->container->getAlias($key), sprintf('%s alias is correct', $key)); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @param mixed $value |
|
143
|
|
|
* @param string $key |
|
144
|
|
|
*/ |
|
145
|
|
|
private function assertParameter($value, $key) |
|
146
|
|
|
{ |
|
147
|
|
|
$this->assertSame($value, $this->container->getParameter($key), sprintf('%s parameter is correct', $key)); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @param string $id |
|
152
|
|
|
*/ |
|
153
|
|
|
private function assertHasDefinition($id) |
|
154
|
|
|
{ |
|
155
|
|
|
$this->assertTrue($this->container->hasDefinition($id) ?: $this->container->hasParameter($id)); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @param string $id |
|
160
|
|
|
*/ |
|
161
|
|
|
private function assertNotHasDefinition($id) |
|
162
|
|
|
{ |
|
163
|
|
|
$this->assertTrue($this->container->hasDefinition($id) ?: $this->container->hasParameter($id)); |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|