InputTest.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. require_once __DIR__ . "/../lessc.inc.php";
  3. // Runs all the tests in inputs/ and compares their output to ouputs/
  4. function _dump($value) {
  5. fwrite(STDOUT, print_r($value, true));
  6. }
  7. function _quote($str) {
  8. return preg_quote($str, "/");
  9. }
  10. class InputTest extends PHPUnit_Framework_TestCase {
  11. protected static $inputDir = "inputs";
  12. protected static $outputDir = "outputs";
  13. public function setUp() {
  14. $this->less = new lessc();
  15. $this->less->importDir = array(__DIR__ . "/" . self::$inputDir . "/test-imports");
  16. }
  17. /**
  18. * @dataProvider fileNameProvider
  19. */
  20. public function testInputFile($inFname) {
  21. if ($pattern = getenv("BUILD")) {
  22. return $this->buildInput($inFname);
  23. }
  24. $outFname = self::outputNameFor($inFname);
  25. if (!is_readable($outFname)) {
  26. $this->fail("$outFname is missing, ".
  27. "consider building tests with BUILD=true");
  28. }
  29. $input = file_get_contents($inFname);
  30. $output = file_get_contents($outFname);
  31. $this->assertEquals($output, $this->less->parse($input));
  32. }
  33. public function fileNameProvider() {
  34. return array_map(function($a) { return array($a); },
  35. self::findInputNames());
  36. }
  37. // only run when env is set
  38. public function buildInput($inFname) {
  39. $css = $this->less->parse(file_get_contents($inFname));
  40. file_put_contents(self::outputNameFor($inFname), $css);
  41. }
  42. static public function findInputNames($pattern="*.less") {
  43. $files = glob(__DIR__ . "/" . self::$inputDir . "/" . $pattern);
  44. return array_filter($files, "is_file");
  45. }
  46. static public function outputNameFor($input) {
  47. $front = _quote(__DIR__ . "/");
  48. $out = preg_replace("/^$front/", "", $input);
  49. $in = _quote(self::$inputDir . "/");
  50. $out = preg_replace("/$in/", self::$outputDir . "/", $out);
  51. $out = preg_replace("/.less$/", ".css", $out);
  52. return __DIR__ . "/" . $out;
  53. }
  54. }