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
|
|
|
* @var FileHandler |
14
|
|
|
*/ |
15
|
|
|
private $fileHandler; |
16
|
|
|
|
17
|
|
|
protected function setUp() { |
18
|
|
|
parent::setUp(); |
19
|
|
|
$fs = new Filesystem(); |
20
|
|
|
$this->fileHandler = new FileHandler($fs, array('--env=prod')); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @dataProvider preparePathProvider |
25
|
|
|
* @param $path |
26
|
|
|
* @param $expected |
27
|
|
|
*/ |
28
|
|
|
public function testPreparePath($path, $expected) { |
29
|
|
|
$this->assertEquals($expected, $this->fileHandler->preparePath($path)); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function preparePathProvider() { |
33
|
|
|
return array( |
34
|
|
|
'without env parameter' => array( |
35
|
|
|
'path' => 'folder/parameters.yml', |
36
|
|
|
'expected' => 'folder/parameters.yml' |
37
|
|
|
), |
38
|
|
|
'with env parameter' => array( |
39
|
|
|
'path' => '{env}/parameters.yml', |
40
|
|
|
'expected' => 'prod/parameters.yml' |
41
|
|
|
), |
42
|
|
|
'with different parameter' => array( |
43
|
|
|
'path' => '{enev}/parameters.yml', |
44
|
|
|
'expected' => '{enev}/parameters.yml' |
45
|
|
|
) |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param $name |
51
|
|
|
* @param $expected |
52
|
|
|
* |
53
|
|
|
* @dataProvider argumentValueProvider |
54
|
|
|
*/ |
55
|
|
|
public function testArgumentValue($name, $expected) { |
56
|
|
|
$this->assertEquals($expected, $this->fileHandler->getArgumentValue($name)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function argumentValueProvider() { |
60
|
|
|
return array( |
61
|
|
|
'env found' => array( |
62
|
|
|
'name' => 'env', |
63
|
|
|
'expected' => 'prod', |
64
|
|
|
), |
65
|
|
|
'--env not found' => array( |
66
|
|
|
'name' => '--env', |
67
|
|
|
'expected' => false, |
68
|
|
|
) |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param $currentPath |
75
|
|
|
* @param $importPath |
76
|
|
|
* @param $expected |
77
|
|
|
* @dataProvider resolvePathProvider |
78
|
|
|
*/ |
79
|
|
|
public function testResolvePath($currentPath, $importPath, $expected) { |
80
|
|
|
$this->assertEquals($expected, $this->fileHandler->resolvePath($currentPath, $importPath)); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function resolvePathProvider() { |
84
|
|
|
return array( |
85
|
|
|
'parent path' => array( |
86
|
|
|
'currentPath' => '/home/user/dir/current.yml', |
87
|
|
|
'importPath' => '../import.yml', |
88
|
|
|
'expected' => '/home/user/dir/../import.yml', |
89
|
|
|
), |
90
|
|
|
'current path' => array( |
91
|
|
|
'currentPath' => '/home/user/dir/current.yml', |
92
|
|
|
'importPath' => './import.yml', |
93
|
|
|
'expected' => '/home/user/dir/./import.yml', |
94
|
|
|
), |
95
|
|
|
|
96
|
|
|
'current simple path' => array( |
97
|
|
|
'currentPath' => '/home/user/dir/current.yml', |
98
|
|
|
'importPath' => 'import.yml', |
99
|
|
|
'expected' => '/home/user/dir/import.yml', |
100
|
|
|
), |
101
|
|
|
'absolute path' => array( |
102
|
|
|
'currentPath' => '/home/user/dir/current.yml', |
103
|
|
|
'importPath' => '/import.yml', |
104
|
|
|
'expected' => '/import.yml', |
105
|
|
|
) |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function testInitDirectory() { |
110
|
|
|
vfsStreamWrapper::register(); |
111
|
|
|
vfsStreamWrapper::setRoot(new vfsStreamDirectory('root')); |
112
|
|
|
|
113
|
|
|
$buildDirName = 'createthis'; |
114
|
|
|
$buildDir = vfsStream::url('root/' . $buildDirName); |
115
|
|
|
|
116
|
|
|
//currently doesn't exists |
117
|
|
|
$this->assertFalse(vfsStreamWrapper::getRoot()->hasChild($buildDirName)); |
|
|
|
|
118
|
|
|
|
119
|
|
|
//create one |
120
|
|
|
$this->fileHandler->initDirectory($buildDir); |
121
|
|
|
$this->assertTrue(vfsStreamWrapper::getRoot()->hasChild($buildDirName)); |
|
|
|
|
122
|
|
|
vfsStreamWrapper::unregister(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param $expected |
127
|
|
|
* @param $value |
128
|
|
|
* |
129
|
|
|
* @dataProvider testProcessEnvironmentalVariableProvider |
130
|
|
|
*/ |
131
|
|
|
public function testProcessEnvironmentalVariable($value, $expected) { |
132
|
|
|
$this->assertEquals($expected, $this->fileHandler->processEnvironmentalVariable($value)); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function testProcessEnvironmentalVariableProvider() { |
136
|
|
|
return array( |
137
|
|
|
'string' => array( |
138
|
|
|
'value' => 'string', |
139
|
|
|
'expected' => 'string' |
140
|
|
|
), |
141
|
|
|
'number' => array( |
142
|
|
|
'value' => 444, |
143
|
|
|
'expected' => 444 |
144
|
|
|
), |
145
|
|
|
'env variable' => array( |
146
|
|
|
'value' => '%env(PATH)%', |
147
|
|
|
'expected' => getenv('PATH') |
148
|
|
|
), |
149
|
|
|
|
150
|
|
|
'string like env' => array( |
151
|
|
|
'value' => 'env(PATH)%', |
152
|
|
|
'expected' => 'env(PATH)%' |
153
|
|
|
) |
154
|
|
|
); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
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: