1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Xiidea\EasyConfigBundle\Tests\Services\Manager; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
7
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
8
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
9
|
|
|
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; |
10
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
11
|
|
|
use Xiidea\EasyConfigBundle\Services\FormGroup\ConfigGroupInterface; |
12
|
|
|
use Xiidea\EasyConfigBundle\Services\Manager\ConfigManager; |
13
|
|
|
use Xiidea\EasyConfigBundle\Services\Repository\ConfigRepositoryInterface; |
14
|
|
|
|
15
|
|
|
class ConfigManagerTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
private $repositoryMock; |
18
|
|
|
private $formFactoryMock; |
19
|
|
|
private $tokenStorageMock; |
20
|
|
|
private $authCheckerMock; |
21
|
|
|
private $configManager; |
22
|
|
|
|
23
|
|
|
protected function setUp(): void |
24
|
|
|
{ |
25
|
|
|
$this->repositoryMock = $this->createMock(ConfigRepositoryInterface::class); |
26
|
|
|
$this->formFactoryMock = $this->createMock(FormFactoryInterface::class); |
27
|
|
|
$this->tokenStorageMock = $this->createMock(TokenStorageInterface::class); |
28
|
|
|
$this->authCheckerMock = $this->createMock(AuthorizationCheckerInterface::class); |
29
|
|
|
$this->configManager = new ConfigManager( |
30
|
|
|
$this->repositoryMock, |
31
|
|
|
$this->formFactoryMock, |
32
|
|
|
$this->tokenStorageMock, |
33
|
|
|
$this->authCheckerMock |
34
|
|
|
); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function testGetConfigurationGroups() |
38
|
|
|
{ |
39
|
|
|
$tokenMock = $this->createMock(TokenInterface::class); |
40
|
|
|
$userMock = $this->createMock(UserInterface::class); |
41
|
|
|
|
42
|
|
|
$username = 'testuser'; |
43
|
|
|
$userMock->expects(self::any())->method('getUserIdentifier')->willReturn($username); |
44
|
|
|
$tokenMock->method('getUser')->willReturn($userMock); |
45
|
|
|
|
46
|
|
|
$this->tokenStorageMock->expects(self::any())->method('getToken')->willReturn($tokenMock); |
47
|
|
|
|
48
|
|
|
// Mock a ConfigGroup |
49
|
|
|
$configGroupMock = $this->createMock(ConfigGroupInterface::class); |
50
|
|
|
$configGroupMock->method('getNameSpace')->willReturn($username.'.someconfig'); |
51
|
|
|
|
52
|
|
|
$this->configManager->addConfigGroup($configGroupMock); |
53
|
|
|
$groups = $this->configManager->getConfigurationGroups(); |
54
|
|
|
|
55
|
|
|
$this->assertArrayHasKey('someconfig', $groups); |
56
|
|
|
$this->assertSame($configGroupMock, $groups['someconfig']); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testGetConfigurationValueByKey() |
60
|
|
|
{ |
61
|
|
|
$key = 'some.key'; |
62
|
|
|
$username = 'testuser'; |
63
|
|
|
$expectedValue = 'Expected Value'; |
64
|
|
|
|
65
|
|
|
$tokenMock = $this->createMock(TokenInterface::class); |
66
|
|
|
$userMock = $this->createMock(UserInterface::class); |
67
|
|
|
|
68
|
|
|
$userMock->method('getUserIdentifier')->willReturn($username); |
69
|
|
|
$tokenMock->method('getUser')->willReturn($userMock); |
70
|
|
|
|
71
|
|
|
$this->tokenStorageMock->method('getToken')->willReturn($tokenMock); |
72
|
|
|
|
73
|
|
|
$this->repositoryMock->expects($this->once()) |
74
|
|
|
->method('getConfigurationByUsernameAndKey') |
75
|
|
|
->with($username, $key) |
76
|
|
|
->willReturn( |
77
|
|
|
[(new \Xiidea\EasyConfigBundle\Model\BaseConfig('config.item.id'))->setValue($expectedValue)] |
78
|
|
|
); |
79
|
|
|
|
80
|
|
|
$result = $this->configManager->getConfigurationValueByKey($key); |
81
|
|
|
|
82
|
|
|
$this->assertEquals($expectedValue, $result); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function testConcatUsernameWithKey() |
86
|
|
|
{ |
87
|
|
|
$tokenMock = $this->createMock(TokenInterface::class); |
88
|
|
|
$userMock = $this->createMock(UserInterface::class); |
89
|
|
|
|
90
|
|
|
$username = 'testuser'; |
91
|
|
|
$userMock->method('getUserIdentifier')->willReturn($username); |
92
|
|
|
$tokenMock->method('getUser')->willReturn($userMock); |
93
|
|
|
|
94
|
|
|
$this->tokenStorageMock->method('getToken')->willReturn($tokenMock); |
95
|
|
|
|
96
|
|
|
// Test when key doesn't start with username |
97
|
|
|
$key = "somekey"; |
98
|
|
|
$expected = "{$username}.{$key}"; |
99
|
|
|
$this->assertEquals($expected, $this->configManager->concatUsernameWithKey($key)); |
100
|
|
|
|
101
|
|
|
// Test when key starts with username |
102
|
|
|
$key = "{$username}.otherkey"; |
103
|
|
|
$this->assertEquals($key, $this->configManager->concatUsernameWithKey($key)); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function testGetConfigurationValueWithConcatenatedKey() |
107
|
|
|
{ |
108
|
|
|
$username = 'testuser'; |
109
|
|
|
$id = 'someconfig'; |
110
|
|
|
$expectedValue = 'TestValue'; |
111
|
|
|
|
112
|
|
|
$tokenMock = $this->createMock(TokenInterface::class); |
113
|
|
|
$userMock = $this->createMock(UserInterface::class); |
114
|
|
|
|
115
|
|
|
$userMock->method('getUserIdentifier')->willReturn($username); |
116
|
|
|
$tokenMock->method('getUser')->willReturn($userMock); |
117
|
|
|
|
118
|
|
|
$this->tokenStorageMock->method('getToken')->willReturn($tokenMock); |
119
|
|
|
$this->repositoryMock->expects($this->once()) |
120
|
|
|
->method('getConfigurationValue') |
121
|
|
|
->with("{$username}.{$id}") |
122
|
|
|
->willReturn($expectedValue); |
123
|
|
|
|
124
|
|
|
$result = $this->configManager->getConfigurationValue($id); |
125
|
|
|
|
126
|
|
|
$this->assertEquals($expectedValue, $result); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function testGetConfigurationValueWithoutConcatenatedKey() |
130
|
|
|
{ |
131
|
|
|
$username = 'testuser'; |
132
|
|
|
$id = 'someconfig'; |
133
|
|
|
$expectedValue = 'TestValue'; |
134
|
|
|
|
135
|
|
|
$tokenMock = $this->createMock(TokenInterface::class); |
136
|
|
|
$userMock = $this->createMock(UserInterface::class); |
137
|
|
|
|
138
|
|
|
$userMock->method('getUserIdentifier')->willReturn($username); |
139
|
|
|
$tokenMock->method('getUser')->willReturn($userMock); |
140
|
|
|
|
141
|
|
|
$this->tokenStorageMock->method('getToken')->willReturn($tokenMock); |
142
|
|
|
$this->repositoryMock->method('getConfigurationValue') |
143
|
|
|
->willReturnOnConsecutiveCalls(null, $expectedValue); |
144
|
|
|
|
145
|
|
|
$result = $this->configManager->getConfigurationValue($id); |
146
|
|
|
|
147
|
|
|
$this->assertEquals($expectedValue, $result); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function testGetConfigurationValueWithTypeCast() |
151
|
|
|
{ |
152
|
|
|
$username = 'testuser'; |
153
|
|
|
$id = 'someconfig'; |
154
|
|
|
$originalValue = '2023-01-01'; |
155
|
|
|
$expectedValue = new \DateTime($originalValue); |
156
|
|
|
|
157
|
|
|
$tokenMock = $this->createMock(TokenInterface::class); |
158
|
|
|
$userMock = $this->createMock(UserInterface::class); |
159
|
|
|
|
160
|
|
|
$userMock->method('getUserIdentifier')->willReturn($username); |
161
|
|
|
$tokenMock->method('getUser')->willReturn($userMock); |
162
|
|
|
|
163
|
|
|
$this->tokenStorageMock->method('getToken')->willReturn($tokenMock); |
164
|
|
|
$this->repositoryMock->expects($this->once()) |
165
|
|
|
->method('getConfigurationValue') |
166
|
|
|
->with("{$username}.{$id}") |
167
|
|
|
->willReturn($originalValue); |
168
|
|
|
|
169
|
|
|
$result = $this->configManager->getConfigurationValue($id, 'date'); |
170
|
|
|
|
171
|
|
|
$this->assertEquals($expectedValue, $result); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|