diff --git a/.scrutinizer.yml b/.scrutinizer.yml index 28afb6e..41fdf94 100644 --- a/.scrutinizer.yml +++ b/.scrutinizer.yml @@ -11,10 +11,13 @@ build: environment: php: version: "7.0.4" -# tests: -# override: -# - -# command: "php bin/phpunit -c phpunit.xml --colors=always --verbose --coverage-clover=coverage.xml" -# coverage: -# file: "coverage.xml" -# format: "php-clover" + tests: + override: + - + command: "composer validate" + override: + - + command: "php bin/phpunit -c phpunit.xml --colors=always --verbose --coverage-clover=coverage.xml" + coverage: + file: "coverage.xml" + format: "php-clover" diff --git a/composer.json b/composer.json index 4c5663d..f6d9c17 100644 --- a/composer.json +++ b/composer.json @@ -19,8 +19,11 @@ }, "autoload-dev": { "psr-4": { - "EM\\Tests\\CssCompiler\\": "tests/" - } + "EM\\Tests\\PHPUnit\\": "tests/phpunit" + }, + "classmap": [ + "tests/shared-enviroment/IntegrationTestSuite.php" + ] }, "config": { "bin-dir": "bin/" diff --git a/src/Container/File.php b/src/Container/File.php index 4918c1e..9569cc7 100644 --- a/src/Container/File.php +++ b/src/Container/File.php @@ -11,7 +11,7 @@ class File const TYPE_SASS = 'sass'; const TYPE_SCSS = 'scss'; const TYPE_LESS = 'less'; - const SUPPORTED_TYPES = [ + static $supportedTypes = [ self::TYPE_COMPASS, self::TYPE_SASS, self::TYPE_SCSS, @@ -179,7 +179,7 @@ protected function detectSourceTypeFromPath($path) { $extension = strtolower(pathinfo($path, PATHINFO_EXTENSION)); - return in_array($extension, static::SUPPORTED_TYPES) + return in_array($extension, static::$supportedTypes) ? $extension : static::TYPE_UNKNOWN; } diff --git a/src/Processor/Processor.php b/src/Processor/Processor.php index 0983bd9..dc949c0 100644 --- a/src/Processor/Processor.php +++ b/src/Processor/Processor.php @@ -16,7 +16,7 @@ class Processor const FORMATTER_EXPANDED = 'expanded'; const FORMATTER_NESTED = 'nested'; const FORMATTER_COMPACT = 'compact'; - const SUPPORTED_FORMATTERS = [ + static $supportedFormatters = [ self::FORMATTER_COMPRESSED, self::FORMATTER_CRUNCHED, self::FORMATTER_EXPANDED, @@ -167,8 +167,8 @@ public function processFile(File $file) */ protected function getFormatterClass($formatter) { - if (!in_array($formatter, static::SUPPORTED_FORMATTERS)) { - throw new \InvalidArgumentException('unknown formatter, available options are: ' . print_r(static::SUPPORTED_FORMATTERS, true)); + if (!in_array($formatter, static::$supportedFormatters)) { + throw new \InvalidArgumentException('unknown formatter, available options are: ' . print_r(static::$supportedFormatters, true)); } return 'Leafo\\ScssPhp\\Formatter\\' . ucfirst($formatter); diff --git a/tests/phpunit/ProcessorTest.php b/tests/phpunit/ProcessorTest.php new file mode 100644 index 0000000..7edeec2 --- /dev/null +++ b/tests/phpunit/ProcessorTest.php @@ -0,0 +1,105 @@ +io = $this->getMockBuilder(IOInterface::class)->getMock(); + } + + /** + * @see Processor::attachFiles + * @test + */ + public function attachFiles() + { + $paths = [ + static::getSharedFixturesDirectory() . '/sass', + static::getSharedFixturesDirectory() . '/compass' + ]; + $cacheDir = dirname(dirname(__DIR__)) . '/var/cache'; + + foreach ($paths as $path) { + $processor = new Processor($this->io); + $processor->attachFiles($path, $cacheDir); + + $this->assertCount(2, $processor->getFiles()); + } + } + + /** + * @see Processor::attachFiles + * @test + * + * @expectedException \Exception + */ + public function attachFilesExpectedException() + { + $path = static::getSharedFixturesDirectory() . '/do-not-exists'; + $cacheDir = dirname(dirname(__DIR__)) . '/var/cache'; + + $processor = new Processor($this->io); + $processor->attachFiles($path, $cacheDir); + + $this->assertCount(2, $processor->getFiles()); + } + + /** + * @see Processor::processFile + * @test + */ + public function processFileSASS() + { + $file = (new File(static::getSharedFixturesDirectory() . '/compass/sass/layout.scss', '')) + ->setSourceContentFromSourcePath(); + + (new Processor($this->io))->processFile($file); + + $this->assertNotEquals($file->getParsedContent(), $file->getSourceContent()); + } + + /** + * @see Processor::processFile + * @test + * + * @expectedException \EM\CssCompiler\Exception\CompilerException + */ + public function processFileExpectedException() + { + $file = (new File(static::getSharedFixturesDirectory() . '/compass/sass/', '')) + ->setSourceContentFromSourcePath() + ->setType(File::TYPE_UNKNOWN); + + (new Processor($this->io))->processFile($file); + } + + /** + * @see Processor::getFormatterClass + * @test + */ + public function getFormatterClass() + { + foreach (Processor::$supportedFormatters as $formatter) { + $expected = 'Leafo\\ScssPhp\\Formatter\\' . ucfirst($formatter); + + $this->assertEquals( + $expected, + $this->invokeMethod(new Processor($this->io), 'getFormatterClass', [$formatter]) + ); + } + } +} diff --git a/tests/shared-enviroment/IntegrationTestSuite.php b/tests/shared-enviroment/IntegrationTestSuite.php new file mode 100644 index 0000000..557ae00 --- /dev/null +++ b/tests/shared-enviroment/IntegrationTestSuite.php @@ -0,0 +1,57 @@ +getMethod($methodName); + $method->setAccessible(true); + + return $method->invokeArgs($object, $methodArguments); + } + + /** + * @return string + */ + public static function getRootDirectory() + { + return dirname(__DIR__); + } + + /** + * @return string + */ + public static function getSharedFixturesDirectory() + { + return static::getRootDirectory() . '/shared-fixtures'; + } + + /** + * return content of the file in located in tests/shared-fixtures directory + * + * @param string $filename + * + * @return string + */ + public static function getSharedFixtureContent(string $filename) + { + return file_get_contents(static::getSharedFixturesDirectory() . "/$filename"); + } +} diff --git a/tests/shared-fixtures/composer.phpunit.json b/tests/shared-fixtures/composer.phpunit.json new file mode 100644 index 0000000..e7e11de --- /dev/null +++ b/tests/shared-fixtures/composer.phpunit.json @@ -0,0 +1,35 @@ +{ + "name": "eugene-matvejev/css-compiler", + "description": "compiles SASS and LESS assets on composer's callback", + "type": "lib", + "license": "MIT", + "authors": [ + { + "name": "Eugene Matvejev", + "email": "eugene.matvejev@gmail.com" + } + ], + "autoload": { + "psr-4": { + "EM\\CssCompiler\\": "src/" + }, + "classmap": [ + "ScriptHandler.php" + ] + }, + "autoload-dev": { + "psr-4": { + "EM\\Tests\\CssCompiler\\": "tests/" + } + }, + "require": { + "php": ">= 5.6", + "leafo/lessphp": "^0.5", + "leafo/scssphp": "@dev", + "leafo/scssphp-compass": "@dev" + }, + "require-dev": { + "composer/composer": "^1.1", + "phpunit/phpunit": "^5.3" + } +}