File indexing completed on 2024-12-22 05:36:22

0001 <?php
0002 
0003 require_once __DIR__ . "/../lessc.inc.php";
0004 
0005 // Runs all the tests in inputs/ and compares their output to ouputs/
0006 
0007 function _dump($value) {
0008   fwrite(STDOUT, print_r($value, true));
0009 }
0010 
0011 function _quote($str) {
0012   return preg_quote($str, "/");
0013 }
0014 
0015 class InputTest extends PHPUnit_Framework_TestCase {
0016   protected static $importDirs = array("inputs/test-imports");
0017 
0018   protected static $testDirs = array(
0019     "inputs" => "outputs",
0020     "inputs_lessjs" => "outputs_lessjs",
0021   );
0022 
0023   public function setUp() {
0024     $this->less = new lessc();
0025     $this->less->importDir = array_map(function($path) {
0026       return __DIR__ . "/" . $path;
0027     }, self::$importDirs);
0028   }
0029 
0030   /**
0031    * @dataProvider fileNameProvider
0032    */
0033   public function testInputFile($inFname) {
0034     if ($pattern = getenv("BUILD")) {
0035       return $this->buildInput($inFname);
0036     }
0037 
0038     $outFname = self::outputNameFor($inFname);
0039 
0040     if (!is_readable($outFname)) {
0041       $this->fail("$outFname is missing, ".
0042         "consider building tests with BUILD=true");
0043     }
0044 
0045     $input = file_get_contents($inFname);
0046     $output = file_get_contents($outFname);
0047 
0048     $this->assertEquals($output, $this->less->parse($input));
0049   }
0050 
0051   public function fileNameProvider() {
0052     return array_map(function($a) { return array($a); },
0053       self::findInputNames());
0054   }
0055 
0056   // only run when env is set
0057   public function buildInput($inFname) {
0058     $css = $this->less->parse(file_get_contents($inFname));
0059     file_put_contents(self::outputNameFor($inFname), $css);
0060   }
0061 
0062   static public function findInputNames($pattern="*.less") {
0063     $files = array();
0064     foreach (self::$testDirs as $inputDir => $outputDir) {
0065       $files = array_merge($files, glob(__DIR__ . "/" . $inputDir . "/" . $pattern));
0066     }
0067 
0068     return array_filter($files, "is_file");
0069   }
0070 
0071   static public function outputNameFor($input) {
0072     $front = _quote(__DIR__ . "/");
0073     $out = preg_replace("/^$front/", "", $input);
0074 
0075     foreach (self::$testDirs as $inputDir => $outputDir) {
0076       $in = _quote($inputDir . "/");
0077       $rewritten = preg_replace("/$in/", $outputDir . "/", $out);
0078       if ($rewritten != $out) {
0079         $out = $rewritten;
0080         break;
0081       }
0082     }
0083 
0084     $out = preg_replace("/.less$/", ".css", $out);
0085 
0086     return __DIR__ . "/" . $out;
0087   }
0088 }
0089