1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MovingImage\Bundle\VMProApiBundle\Tests\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\ClientInterface; |
6
|
|
|
use MovingImage\Bundle\VMProApiBundle\DependencyInjection\VMProApiExtension; |
7
|
|
|
use MovingImage\Client\VMPro\ApiClient\Guzzle5ApiClient; |
8
|
|
|
use MovingImage\Client\VMPro\ApiClient\Guzzle6ApiClient; |
9
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
10
|
|
|
use Symfony\Component\Yaml\Parser; |
11
|
|
|
|
12
|
|
|
class VMProApiExtensionTest extends \PHPUnit_Framework_TestCase |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Get empty configuration set. |
16
|
|
|
* |
17
|
|
|
* @return array |
18
|
|
|
*/ |
19
|
|
|
protected function getEmptyConfig() |
20
|
|
|
{ |
21
|
|
|
$yaml = <<<'EOF' |
22
|
|
|
vm_pro_api: |
23
|
|
|
credentials: |
24
|
|
|
username: ~ |
25
|
|
|
password: ~ |
26
|
|
|
EOF; |
27
|
|
|
$parser = new Parser(); |
28
|
|
|
|
29
|
|
|
return $parser->parse($yaml); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Get empty configuration set. |
34
|
|
|
* |
35
|
|
|
* @return array |
36
|
|
|
*/ |
37
|
|
|
protected function getFullConfig() |
38
|
|
|
{ |
39
|
|
|
$yaml = <<<'EOF' |
40
|
|
|
vm_pro_api: |
41
|
|
|
base_url: http://google.com/ |
42
|
|
|
default_vm_id: 5 |
43
|
|
|
credentials: |
44
|
|
|
username: [email protected] |
45
|
|
|
password: test_password |
46
|
|
|
EOF; |
47
|
|
|
$parser = new Parser(); |
48
|
|
|
|
49
|
|
|
return $parser->parse($yaml); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Assert whether an exception is thrown when required configuration |
54
|
|
|
* key 'vm_pro_api.credentials' is missing. |
55
|
|
|
* |
56
|
|
|
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException |
57
|
|
|
*/ |
58
|
|
View Code Duplication |
public function testConfigurationLoadThrowsExceptionUnlessCredentials() |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
$loader = new VMProApiExtension(); |
61
|
|
|
$config = $this->getEmptyConfig(); |
62
|
|
|
|
63
|
|
|
unset($config['vm_pro_api']['credentials']); |
64
|
|
|
$loader->load($config, new ContainerBuilder()); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Assert whether an exception is thrown when required configuration |
69
|
|
|
* key 'vm_pro_api.credentials.username' is missing. |
70
|
|
|
* |
71
|
|
|
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException |
72
|
|
|
*/ |
73
|
|
View Code Duplication |
public function testConfigurationLoadThrowsExceptionUnlessUsername() |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
$loader = new VMProApiExtension(); |
76
|
|
|
$config = $this->getEmptyConfig(); |
77
|
|
|
|
78
|
|
|
unset($config['vm_pro_api']['credentials']['username']); |
79
|
|
|
$loader->load($config, new ContainerBuilder()); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Assert whether an exception is thrown when required configuration |
84
|
|
|
* key 'vm_pro_api.credentials.password' is missing. |
85
|
|
|
* |
86
|
|
|
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException |
87
|
|
|
*/ |
88
|
|
View Code Duplication |
public function testConfigurationLoadThrowsExceptionUnlessPassword() |
|
|
|
|
89
|
|
|
{ |
90
|
|
|
$loader = new VMProApiExtension(); |
91
|
|
|
$config = $this->getEmptyConfig(); |
92
|
|
|
|
93
|
|
|
unset($config['vm_pro_api']['credentials']['password']); |
94
|
|
|
$loader->load($config, new ContainerBuilder()); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Assert whether the configuration keys and values are successfully |
99
|
|
|
* written as parameters. |
100
|
|
|
*/ |
101
|
|
|
public function testParametersForFullConfig() |
102
|
|
|
{ |
103
|
|
|
$container = new ContainerBuilder(); |
104
|
|
|
$loader = new VMProApiExtension(); |
105
|
|
|
$config = $this->getFullConfig(); |
106
|
|
|
|
107
|
|
|
$loader->load($config, $container); |
108
|
|
|
|
109
|
|
|
$this->assertEquals('http://google.com/', $container->getParameter('vm_pro_api_base_url')); |
110
|
|
|
$this->assertEquals(5, $container->getParameter('vm_pro_api_default_vm_id')); |
111
|
|
|
$this->assertEquals('[email protected]', $container->getParameter('vm_pro_api_credentials_username')); |
112
|
|
|
$this->assertEquals('test_password', $container->getParameter('vm_pro_api_credentials_password')); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Assert whether the configuration keys and values are successfully |
117
|
|
|
* written as parameters with the right default values. |
118
|
|
|
*/ |
119
|
|
|
public function testDefaultParameters() |
120
|
|
|
{ |
121
|
|
|
$container = new ContainerBuilder(); |
122
|
|
|
$loader = new VMProApiExtension(); |
123
|
|
|
$config = $this->getEmptyConfig(); |
124
|
|
|
|
125
|
|
|
$loader->load($config, $container); |
126
|
|
|
|
127
|
|
|
$this->assertEquals('https://api.video-cdn.net/v1/vms/', $container->getParameter('vm_pro_api_base_url')); |
128
|
|
|
$this->assertEquals(0, $container->getParameter('vm_pro_api_default_vm_id')); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Assert whether when Guzzle ^5.0 is installed, the right instance |
133
|
|
|
* of the API client is placed in the dependency injection container. |
134
|
|
|
*/ |
135
|
|
View Code Duplication |
public function testHasGuzzle5Client() |
|
|
|
|
136
|
|
|
{ |
137
|
|
|
if (version_compare(ClientInterface::VERSION, '6.0', '>=')) { |
138
|
|
|
$this->markTestSkipped('Skipping test when Guzzle ~6.0 is installed'); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$container = new ContainerBuilder(); |
142
|
|
|
$loader = new VMProApiExtension(); |
143
|
|
|
$config = $this->getFullConfig(); |
144
|
|
|
|
145
|
|
|
$loader->load($config, $container); |
146
|
|
|
|
147
|
|
|
$this->assertInstanceOf(Guzzle5ApiClient::class, $container->get('vmpro_api.client')); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Assert whether when Guzzle ^6.0 is installed, the right instance |
152
|
|
|
* of the API client is placed in the dependency injection container. |
153
|
|
|
*/ |
154
|
|
View Code Duplication |
public function testHasGuzzle6Client() |
|
|
|
|
155
|
|
|
{ |
156
|
|
|
if (version_compare(ClientInterface::VERSION, '6.0', '<')) { |
157
|
|
|
$this->markTestSkipped('Skipping test when Guzzle ~5.0 is installed'); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
$container = new ContainerBuilder(); |
161
|
|
|
$loader = new VMProApiExtension(); |
162
|
|
|
$config = $this->getFullConfig(); |
163
|
|
|
|
164
|
|
|
$loader->load($config, $container); |
165
|
|
|
|
166
|
|
|
$this->assertInstanceOf(Guzzle6ApiClient::class, $container->get('vmpro_api.client')); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
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.