summaryrefslogtreecommitdiff
path: root/vendor/leafo/lessphp/tests/InputTest.php
blob: 32db95bc7b7b8ff8ac8b2e37e1ffbd044fdd99f1 (plain)
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
<?php

require_once __DIR__ . "/../lessc.inc.php";

// Runs all the tests in inputs/ and compares their output to ouputs/

function _dump($value) {
	fwrite(STDOUT, print_r($value, true));
}

function _quote($str) {
	return preg_quote($str, "/");
}

class InputTest extends PHPUnit_Framework_TestCase {
	protected static $importDirs = array("inputs/test-imports");

	protected static $testDirs = array(
		"inputs" => "outputs",
		"inputs_lessjs" => "outputs_lessjs",
	);

	public function setUp() {
		$this->less = new lessc();
		$this->less->importDir = array_map(function($path) {
			return __DIR__ . "/" . $path;
		}, self::$importDirs);
	}

	/**
	 * @dataProvider fileNameProvider
	 */
	public function testInputFile($inFname) {
		if ($pattern = getenv("BUILD")) {
			return $this->buildInput($inFname);
		}

		$outFname = self::outputNameFor($inFname);

		if (!is_readable($outFname)) {
			$this->fail("$outFname is missing, ".
				"consider building tests with BUILD=true");
		}

		$input = file_get_contents($inFname);
		$output = file_get_contents($outFname);

		$this->assertEquals($output, $this->less->parse($input));
	}

	public function fileNameProvider() {
		return array_map(function($a) { return array($a); },
			self::findInputNames());
	}

	// only run when env is set
	public function buildInput($inFname) {
		$css = $this->less->parse(file_get_contents($inFname));
		file_put_contents(self::outputNameFor($inFname), $css);
	}

	static public function findInputNames($pattern="*.less") {
		$files = array();
		foreach (self::$testDirs as $inputDir => $outputDir) {
			$files = array_merge($files, glob(__DIR__ . "/" . $inputDir . "/" . $pattern));
		}

		return array_filter($files, "is_file");
	}

	static public function outputNameFor($input) {
		$front = _quote(__DIR__ . "/");
		$out = preg_replace("/^$front/", "", $input);

		foreach (self::$testDirs as $inputDir => $outputDir) {
			$in = _quote($inputDir . "/");
			$rewritten = preg_replace("/$in/", $outputDir . "/", $out);
			if ($rewritten != $out) {
				$out = $rewritten;
				break;
			}
		}

		$out = preg_replace("/.less$/", ".css", $out);

		return __DIR__ . "/" . $out;
	}
}