/
TestCase.php
213 lines (186 loc) · 5.74 KB
/
TestCase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
/**
* This file is part of tenside/core-bundle.
*
* (c) Christian Schiffler <c.schiffler@cyberspectrum.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* This project is provided in good faith and hope to be usable by anyone.
*
* @package tenside/core-bundle
* @author Christian Schiffler <c.schiffler@cyberspectrum.de>
* @copyright 2015 Christian Schiffler <c.schiffler@cyberspectrum.de>
* @license https://github.com/tenside/core-bundle/blob/master/LICENSE MIT
* @link https://github.com/tenside/core-bundle
* @filesource
*/
namespace Tenside\CoreBundle\Test;
use Symfony\Component\Filesystem\Filesystem;
/**
* This is the base for all test cases.
*/
class TestCase extends \PHPUnit_Framework_TestCase
{
/**
* Temporary working dir.
*
* @var string
*/
protected $workDir;
/**
* The initial working directory.
*
* @var string
*/
private $initialWorkingDirectory;
/**
* {@inheritDoc}
*/
protected function setUp()
{
parent::setUp();
$this->initialWorkingDirectory = getcwd();
// Ensure we have a clean environment.
putenv('COMPOSER=');
}
/**
* {@inheritdoc}
*/
public function tearDown()
{
if (isset($this->initialWorkingDirectory)) {
chdir($this->initialWorkingDirectory);
}
if (isset($this->workDir)) {
$filesystem = new Filesystem();
$filesystem->remove($this->workDir);
}
}
/**
* Retrieve the path to the fixtures directory.
*
* @return string
*/
protected function getFixturesDirectory()
{
return __DIR__ . DIRECTORY_SEPARATOR . 'fixtures';
}
/**
* Create and return the path to a temp dir.
*
* @return string
*/
protected function getTempDir()
{
if (!isset($this->workDir)) {
$temp = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid('tenside-core-test');
mkdir($temp, 0777, true);
$this->workDir = $temp;
}
return $this->workDir;
}
/**
* Retrieve the path of a temp file within the temp dir of the test.
*
* @param string $name Optional name of the file.
*
* @param bool $forceDirectories Optional flag if the parenting dirs should be created.
*
* @return string
*/
public function getTempFile($name = '', $forceDirectories = true)
{
if ('' === $name) {
$name = uniqid();
}
$path = $this->getTempDir() . DIRECTORY_SEPARATOR . $name;
if ($forceDirectories && !is_dir($dir = dirname($path))) {
mkdir($dir, 0777, true);
}
return $path;
}
/**
* Provide a fixture in the temp directory and return the complete path to the new file.
*
* @param string $path The file name of the fixture.
*
* @param string $newPath The new path for the fixture.
*
* @return string
*/
public function provideFixture($path, $newPath = '')
{
if ('' === $newPath) {
$newPath = $path;
}
$tempDir = $this->getTempDir();
$fullPath = $tempDir . DIRECTORY_SEPARATOR . $newPath;
if (!is_dir(dirname($fullPath))) {
mkdir(dirname($fullPath), 0777, true);
}
copy($this->getFixturesDirectory() . DIRECTORY_SEPARATOR . $path, $fullPath);
return $fullPath;
}
/**
* Provide a fixture in the temp directory with the passed data and return the complete path to the new file.
*
* @param string $path The file name of the fixture.
*
* @param string $content The fixture content.
*
* @return string
*/
public function createFixture($path, $content)
{
$tempDir = $this->getTempDir();
$fullPath = $tempDir . DIRECTORY_SEPARATOR . $path;
if (!is_dir(dirname($fullPath))) {
mkdir(dirname($fullPath), 0777, true);
}
file_put_contents($fullPath, $content);
return $fullPath;
}
/**
* Read the content of a fixture to memory and return it.
*
* @param string $path The fixture to read.
*
* @return string
*/
public function readFixture($path)
{
return file_get_contents($this->getFixturesDirectory() . DIRECTORY_SEPARATOR . $path);
}
/**
* Ensure the contents of a zip file are present in the given dir.
*
* @param string $zipFile The source zip to scan (full path).
*
* @param string $destinationDir The directory where the contents shall be checked (relative to temp dir).
*
* @return void
*/
protected function assertZipHasBeenUnpackedTo($zipFile, $destinationDir = '')
{
$destinationDir = $this->getTempDir() . DIRECTORY_SEPARATOR . $destinationDir;
$zip = new \ZipArchive();
$zip->open($zipFile);
for ($i = 0; $i < $zip->numFiles; $i++) {
$stat = $zip->statIndex($i);
$fileName = $stat['name'];
$localFile = $destinationDir . DIRECTORY_SEPARATOR . $fileName;
$this->assertTrue(is_link($localFile) || file_exists($localFile), 'File does not exist ' . $localFile);
if (is_link($destinationDir . DIRECTORY_SEPARATOR . $fileName)
|| is_dir($destinationDir . DIRECTORY_SEPARATOR . $fileName)) {
continue;
}
$this->assertEquals(
sprintf('%u', $stat['crc']),
hexdec(hash_file('crc32b', $destinationDir . DIRECTORY_SEPARATOR . $fileName)),
'CRC mismatch for ' . $fileName
);
}
}
}