1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MediaMonks\SonataMediaBundle\Tests\Functional; |
4
|
|
|
|
5
|
|
|
use Liip\FunctionalTestBundle\Test\WebTestCase; |
6
|
|
|
use Symfony\Component\DomCrawler\Form; |
7
|
|
|
use Symfony\Bundle\FrameworkBundle\Client; |
8
|
|
|
use Symfony\Component\Finder\Finder; |
9
|
|
|
|
10
|
|
|
abstract class AbstractBaseFunctionTest extends WebTestCase |
11
|
|
|
{ |
12
|
|
|
protected function setUp() |
13
|
|
|
{ |
14
|
|
|
if (version_compare(PHP_VERSION, '5.6.0', '<')) { |
15
|
|
|
$this->markTestSkipped('Functional tests only run on PHP 5.6+'); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
parent::setUp(); |
19
|
|
|
|
20
|
|
|
$this->emptyFolder($this->getMediaPathPublic()); |
21
|
|
|
$this->emptyFolder($this->getMediaPathPrivate()); |
22
|
|
|
|
23
|
|
|
\VCR\VCR::configure() |
24
|
|
|
->setCassettePath($this->getFixturesPath()) |
25
|
|
|
->enableLibraryHooks(['curl']) |
26
|
|
|
->setStorage('json'); |
27
|
|
|
|
28
|
|
|
\VCR\VCR::turnOn(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
|
|
protected function getMediaPathPublic() |
35
|
|
|
{ |
36
|
|
|
return __DIR__.'/web/media/'; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
|
|
protected function getMediaPathPrivate() |
43
|
|
|
{ |
44
|
|
|
return __DIR__.'/var/media/'; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return string |
49
|
|
|
*/ |
50
|
|
|
protected function getFixturesPath() |
51
|
|
|
{ |
52
|
|
|
return __DIR__.'/var/fixtures/'; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param int $amount |
57
|
|
|
* @param string $path |
58
|
|
|
*/ |
59
|
|
|
protected function assertNumberOfFilesInPath($amount, $path) |
60
|
|
|
{ |
61
|
|
|
$finder = new Finder(); |
62
|
|
|
$finder->files()->in($path); |
63
|
|
|
$this->assertEquals($amount, $finder->count()); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $path |
68
|
|
|
* @return bool |
69
|
|
|
*/ |
70
|
|
|
protected function emptyFolder($path) |
71
|
|
|
{ |
72
|
|
|
if (file_exists($path)) { |
73
|
|
|
$di = new \RecursiveDirectoryIterator($path, \FilesystemIterator::SKIP_DOTS); |
74
|
|
|
$ri = new \RecursiveIteratorIterator($di, \RecursiveIteratorIterator::CHILD_FIRST); |
75
|
|
|
foreach ($ri as $file) { |
76
|
|
|
$file->isDir() ? rmdir($file) : unlink($file); |
77
|
|
|
} |
78
|
|
|
} else { |
79
|
|
|
@mkdir($path); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return true; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return Client |
87
|
|
|
*/ |
88
|
|
|
protected function getAuthenticatedClient() |
89
|
|
|
{ |
90
|
|
|
return $this->makeClient(true); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param Form $form |
95
|
|
|
* @param array $asserts |
96
|
|
|
*/ |
97
|
|
|
protected function assertSonataFormValues(Form $form, array $asserts) |
98
|
|
|
{ |
99
|
|
|
foreach ($form->getValues() as $formKey => $formValue) { |
100
|
|
|
foreach ($asserts as $assertKey => $assertValue) { |
101
|
|
|
if (strpos($formKey, sprintf('[%s]', $assertKey)) !== false) { |
102
|
|
|
$this->assertEquals($assertValue, $formValue); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param Form $form |
110
|
|
|
* @return array |
111
|
|
|
*/ |
112
|
|
|
protected function getSonataFormValues(Form $form) |
113
|
|
|
{ |
114
|
|
|
$values = []; |
115
|
|
|
foreach ($form->getValues() as $k => $v) { |
116
|
|
|
if (preg_match('~\[(.*)\]~', $k, $matches)) { |
117
|
|
|
$values[$matches[1]] = $v; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $values; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param Form $form |
126
|
|
|
* @param array $updates |
127
|
|
|
*/ |
128
|
|
|
protected function updateSonataFormValues(Form $form, array $updates) |
129
|
|
|
{ |
130
|
|
|
foreach ($form->getValues() as $formKey => $formValue) { |
131
|
|
|
foreach ($updates as $updateKey => $updateValue) { |
132
|
|
|
if (strpos($formKey, sprintf('[%s]', $updateKey)) !== false) { |
133
|
|
|
$form[$formKey] = $updateValue; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param Form $form |
141
|
|
|
* @return mixed |
142
|
|
|
*/ |
143
|
|
|
protected function getSonataFormBaseKey(Form $form) |
144
|
|
|
{ |
145
|
|
|
foreach ($form->getValues() as $k => $v) { |
146
|
|
|
if (preg_match('~(.*)\[(.*)\]~', $k, $matches)) { |
147
|
|
|
return $matches[1]; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
throw new \Exception('Could not find Sonata base key in form'); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param Form $form |
156
|
|
|
* @param string $file |
157
|
|
|
* @throws \Exception |
158
|
|
|
*/ |
159
|
|
|
protected function setFormBinaryContent(Form $form, $file) |
160
|
|
|
{ |
161
|
|
|
$baseKey = $this->getSonataFormBaseKey($form); |
162
|
|
|
$key = sprintf('%s[binaryContent]', $baseKey); |
163
|
|
|
if (!file_exists($file)) { |
164
|
|
|
throw new \Exception('Upload file does not exist at: '.$file); |
165
|
|
|
} |
166
|
|
|
$form[$key]->upload($file); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: