Skip to content

Commit 2110170

Browse files
committed
Initial base TestCase
1 parent 5423fca commit 2110170

File tree

1 file changed

+70
-1
lines changed

1 file changed

+70
-1
lines changed

unit-tests/Extension/TestCase.php

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,75 @@
1919

2020
namespace Zephir\Parser\Tests;
2121

22-
abstract class TestCase extends \PHPUnit_Framework_TestCase
22+
use FilesystemIterator;
23+
use RecursiveIteratorIterator;
24+
use RecursiveDirectoryIterator;
25+
use PHPUnit_Framework_TestCase;
26+
use PHPUnit_Framework_Exception;
27+
28+
abstract class TestCase extends PHPUnit_Framework_TestCase
2329
{
30+
public function setUp()
31+
{
32+
if (!file_exists(ZEPHIR_PARSER_OUTPUT)) {
33+
mkdir(ZEPHIR_PARSER_OUTPUT, 0755, true);
34+
} else {
35+
$this->cleanOutputDir();
36+
}
37+
}
38+
39+
protected function parseFile($file)
40+
{
41+
$path = ZEPHIR_PARSER_DATA . DIRECTORY_SEPARATOR . ltrim($file, '\\/');
42+
43+
return zephir_parse_file(file_get_contents($path), $path);
44+
}
45+
46+
protected function parseContent($content)
47+
{
48+
$stream = fopen('php://memory','r+');
49+
50+
if (!is_resource($stream)) {
51+
throw new PHPUnit_Framework_Exception('Unable to create stream to prepare content');
52+
}
53+
54+
fwrite($stream, $content);
55+
rewind($stream);
56+
57+
$path = ZEPHIR_PARSER_OUTPUT . DIRECTORY_SEPARATOR . md5($content) . '.zep';
58+
59+
if (file_put_contents($path, $stream) === false) {
60+
throw new PHPUnit_Framework_Exception('Unable to write content to the temporary file');
61+
}
62+
63+
return zephir_parse_file(file_get_contents($path), $path);
64+
}
65+
66+
/**
67+
* Clean output directory from the generated files.
68+
*/
69+
protected function cleanOutputDir()
70+
{
71+
$directoryIterator = new RecursiveDirectoryIterator(
72+
ZEPHIR_PARSER_OUTPUT,
73+
FilesystemIterator::KEY_AS_PATHNAME |
74+
FilesystemIterator::CURRENT_AS_FILEINFO |
75+
FilesystemIterator::SKIP_DOTS
76+
);
77+
78+
$iterator = iterator_to_array(
79+
new RecursiveIteratorIterator($directoryIterator, RecursiveIteratorIterator::CHILD_FIRST)
80+
);
81+
82+
foreach ($iterator as $file) {
83+
/* @var \SplFileInfo $file */
84+
if ($file->isFile()) {
85+
if (strpos($file->getBasename(), '.') !== 0) {
86+
unlink($file->getRealPath());
87+
} elseif ($file->isDir()) {
88+
rmdir($file->getRealPath());
89+
}
90+
}
91+
}
92+
}
2493
}

0 commit comments

Comments
 (0)