| 1 |
<?php |
| 2 |
|
| 3 |
require_once __DIR__ . "/../scss.inc.php"; |
| 4 |
|
| 5 |
// Runs all the tests in inputs/ and compares their output to ouputs/ |
| 6 |
|
| 7 |
function _dump($value) { |
| 8 |
fwrite(STDOUT, print_r($value, true)); |
| 9 |
} |
| 10 |
|
| 11 |
function _quote($str) { |
| 12 |
return preg_quote($str, "/"); |
| 13 |
} |
| 14 |
|
| 15 |
class InputTest extends PHPUnit_Framework_TestCase { |
| 16 |
protected static $inputDir = "inputs"; |
| 17 |
protected static $outputDir = "outputs"; |
| 18 |
|
| 19 |
public function setUp() { |
| 20 |
$this->scss = new scssc(); |
| 21 |
$this->scss->addImportPath(__DIR__ . "/" . self::$inputDir); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* @dataProvider fileNameProvider |
| 26 |
*/ |
| 27 |
public function testInputFile($inFname, $outFname) { |
| 28 |
if (getenv("BUILD")) { |
| 29 |
return $this->buildInput($inFname,$outFname); |
| 30 |
} |
| 31 |
|
| 32 |
if (!is_readable($outFname)) { |
| 33 |
$this->fail("$outFname is missing, ". |
| 34 |
"consider building tests with BUILD=true"); |
| 35 |
} |
| 36 |
|
| 37 |
$input = file_get_contents($inFname); |
| 38 |
$output = file_get_contents($outFname); |
| 39 |
|
| 40 |
$this->assertEquals($output, $this->scss->compile($input)); |
| 41 |
} |
| 42 |
|
| 43 |
public function fileNameProvider() { |
| 44 |
return array_map(function($a) { return array($a, InputTest::outputNameFor($a)); }, |
| 45 |
self::findInputNames()); |
| 46 |
} |
| 47 |
|
| 48 |
// only run when env is set |
| 49 |
public function buildInput($inFname, $outFname) { |
| 50 |
$css = $this->scss->compile(file_get_contents($inFname)); |
| 51 |
file_put_contents($outFname, $css); |
| 52 |
} |
| 53 |
|
| 54 |
static public function findInputNames($pattern="*") { |
| 55 |
$files = glob(__DIR__ . "/" . self::$inputDir . "/" . $pattern); |
| 56 |
$files = array_filter($files, "is_file"); |
| 57 |
if ($pattern = getenv("MATCH")) { |
| 58 |
$files = array_filter($files, function($fname) use ($pattern) { |
| 59 |
return preg_match("/$pattern/", $fname); |
| 60 |
}); |
| 61 |
} |
| 62 |
|
| 63 |
return $files; |
| 64 |
} |
| 65 |
|
| 66 |
static public function outputNameFor($input) { |
| 67 |
$front = _quote(__DIR__ . "/"); |
| 68 |
$out = preg_replace("/^$front/", "", $input); |
| 69 |
|
| 70 |
$in = _quote(self::$inputDir . "/"); |
| 71 |
$out = preg_replace("/$in/", self::$outputDir . "/", $out); |
| 72 |
$out = preg_replace("/.scss$/", ".css", $out); |
| 73 |
|
| 74 |
return __DIR__ . "/" . $out; |
| 75 |
} |
| 76 |
|
| 77 |
static public function buildTests($pattern) { |
| 78 |
$files = self::findInputNames($pattern); |
| 79 |
foreach ($files as $file) { |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
|