1
|
|
|
<?php |
2
|
|
|
namespace Paro\EnvironmentParameters\Tests; |
3
|
|
|
|
4
|
|
|
use org\bovigo\vfs\vfsStream; |
5
|
|
|
use org\bovigo\vfs\vfsStreamDirectory; |
6
|
|
|
use org\bovigo\vfs\vfsStreamWrapper; |
7
|
|
|
use Paro\EnvironmentParameters\FileHandler; |
8
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
9
|
|
|
|
10
|
|
|
class FileHandlerTest extends \PHPUnit_Framework_TestCase |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var FileHandler |
15
|
|
|
*/ |
16
|
|
|
private $fileHandler; |
17
|
|
|
|
18
|
|
|
protected function setUp() |
19
|
|
|
{ |
20
|
|
|
parent::setUp(); |
21
|
|
|
$fs = new Filesystem(); |
22
|
|
|
$this->fileHandler = new FileHandler($fs, array('--env=prod')); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @dataProvider preparePathProvider |
27
|
|
|
* @param $path |
28
|
|
|
* @param $expected |
29
|
|
|
*/ |
30
|
|
|
public function testPreparePath($path, $expected) |
31
|
|
|
{ |
32
|
|
|
$this->assertEquals($expected, $this->fileHandler->preparePath($path)); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function preparePathProvider() |
36
|
|
|
{ |
37
|
|
|
return array( |
38
|
|
|
'without env parameter' => array( |
39
|
|
|
'path' => 'folder/parameters.yml', |
40
|
|
|
'expected' => 'folder/parameters.yml' |
41
|
|
|
), |
42
|
|
|
'with env parameter' => array( |
43
|
|
|
'path' => '{env}/parameters.yml', |
44
|
|
|
'expected' => 'prod/parameters.yml' |
45
|
|
|
), |
46
|
|
|
'with different parameter' => array( |
47
|
|
|
'path' => '{enev}/parameters.yml', |
48
|
|
|
'expected' => '{enev}/parameters.yml' |
49
|
|
|
) |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param $name |
55
|
|
|
* @param $expected |
56
|
|
|
* |
57
|
|
|
* @dataProvider argumentValueProvider |
58
|
|
|
*/ |
59
|
|
|
public function testArgumentValue($name, $expected) |
60
|
|
|
{ |
61
|
|
|
$this->assertEquals($expected, $this->fileHandler->getArgumentValue($name)); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function argumentValueProvider() |
65
|
|
|
{ |
66
|
|
|
return array( |
67
|
|
|
'env found' => array( |
68
|
|
|
'name' => 'env', |
69
|
|
|
'expected' => 'prod', |
70
|
|
|
), |
71
|
|
|
'--env not found' => array( |
72
|
|
|
'name' => '--env', |
73
|
|
|
'expected' => false, |
74
|
|
|
) |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param $currentPath |
81
|
|
|
* @param $importPath |
82
|
|
|
* @param $expected |
83
|
|
|
* @dataProvider resolvePathProvider |
84
|
|
|
*/ |
85
|
|
|
public function testResolvePath($currentPath, $importPath, $expected) |
86
|
|
|
{ |
87
|
|
|
$this->assertEquals($expected, $this->fileHandler->resolvePath($currentPath, $importPath)); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function resolvePathProvider() |
91
|
|
|
{ |
92
|
|
|
return array( |
93
|
|
|
'parent path' => array( |
94
|
|
|
'currentPath' => '/home/user/dir/current.yml', |
95
|
|
|
'importPath' => '../import.yml', |
96
|
|
|
'expected' => '/home/user/dir/../import.yml', |
97
|
|
|
), |
98
|
|
|
'current path' => array( |
99
|
|
|
'currentPath' => '/home/user/dir/current.yml', |
100
|
|
|
'importPath' => './import.yml', |
101
|
|
|
'expected' => '/home/user/dir/./import.yml', |
102
|
|
|
), |
103
|
|
|
|
104
|
|
|
'current simple path' => array( |
105
|
|
|
'currentPath' => '/home/user/dir/current.yml', |
106
|
|
|
'importPath' => 'import.yml', |
107
|
|
|
'expected' => '/home/user/dir/import.yml', |
108
|
|
|
), |
109
|
|
|
'absolute path' => array( |
110
|
|
|
'currentPath' => '/home/user/dir/current.yml', |
111
|
|
|
'importPath' => '/import.yml', |
112
|
|
|
'expected' => '/import.yml', |
113
|
|
|
) |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function testInitDirectory() |
118
|
|
|
{ |
119
|
|
|
vfsStreamWrapper::register(); |
120
|
|
|
vfsStreamWrapper::setRoot(new vfsStreamDirectory('root')); |
121
|
|
|
|
122
|
|
|
$buildDirName = 'createthis'; |
123
|
|
|
$buildDir = vfsStream::url('root/' . $buildDirName); |
124
|
|
|
|
125
|
|
|
//currently doesn't exists |
126
|
|
|
$this->assertFalse(vfsStreamWrapper::getRoot()->hasChild($buildDirName)); |
|
|
|
|
127
|
|
|
|
128
|
|
|
//create one |
129
|
|
|
$this->fileHandler->initDirectory($buildDir); |
130
|
|
|
$this->assertTrue(vfsStreamWrapper::getRoot()->hasChild($buildDirName)); |
|
|
|
|
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param $expected |
135
|
|
|
* @param $value |
136
|
|
|
* |
137
|
|
|
* @dataProvider testProcessEnvironmentalVariableProvider |
138
|
|
|
*/ |
139
|
|
|
public function testProcessEnvironmentalVariable($value, $expected) |
140
|
|
|
{ |
141
|
|
|
$this->assertEquals($expected, $this->fileHandler->processEnvironmentalVariable($value)); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function testProcessEnvironmentalVariableProvider() |
145
|
|
|
{ |
146
|
|
|
return array( |
147
|
|
|
'string' => array( |
148
|
|
|
'value' => 'string', |
149
|
|
|
'expected' => 'string' |
150
|
|
|
), |
151
|
|
|
'number' => array( |
152
|
|
|
'value' => 444, |
153
|
|
|
'expected' => 444 |
154
|
|
|
), |
155
|
|
|
'env variable' => array( |
156
|
|
|
'value' => '%env(PATH)%', |
157
|
|
|
'expected' => getenv('PATH') |
158
|
|
|
), |
159
|
|
|
|
160
|
|
|
'string like env' => array( |
161
|
|
|
'value' => 'env(PATH)%', |
162
|
|
|
'expected' => 'env(PATH)%' |
163
|
|
|
) |
164
|
|
|
); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: