From a3acb83cec85f314a06a49691e15065bd95bd86f Mon Sep 17 00:00:00 2001 From: Eugene Matvejev Date: Wed, 22 Jun 2016 11:29:53 +0300 Subject: [PATCH 1/4] first tests --- .scrutinizer.yml | 17 +-- composer.json | 7 +- tests/phpunit/ProcessorTest.php | 105 ++++++++++++++++++ .../IntegrationTestSuite.php | 51 +++++++++ tests/shared-fixtures/composer.phpunit.json | 35 ++++++ 5 files changed, 206 insertions(+), 9 deletions(-) create mode 100644 tests/phpunit/ProcessorTest.php create mode 100644 tests/shared-enviroment/IntegrationTestSuite.php create mode 100644 tests/shared-fixtures/composer.phpunit.json 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 2bb7869..cf0abee 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" + ] }, "require": { "php": ">= 5.6.0 || 7.0.0 - 7.0.4 || >= 7.0.6", diff --git a/tests/phpunit/ProcessorTest.php b/tests/phpunit/ProcessorTest.php new file mode 100644 index 0000000..3293701 --- /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::SUPPORTED_FORMATTERS 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..9661dfc --- /dev/null +++ b/tests/shared-enviroment/IntegrationTestSuite.php @@ -0,0 +1,51 @@ +getMethod($methodName); + $method->setAccessible(true); + + return $method->invokeArgs($object, $methodArguments); + } + + public static function getRootDirectory() : string + { + return dirname(__DIR__); + } + + public static function getSharedFixturesDirectory() : string + { + 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) : string + { + 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" + } +} From 0ea0bf733cf8964523f49d96290f8d8cdacb1974 Mon Sep 17 00:00:00 2001 From: Eugene Matvejev Date: Wed, 22 Jun 2016 12:17:03 +0300 Subject: [PATCH 2/4] fix build --- tests/shared-enviroment/IntegrationTestSuite.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/tests/shared-enviroment/IntegrationTestSuite.php b/tests/shared-enviroment/IntegrationTestSuite.php index 9661dfc..ae038e5 100644 --- a/tests/shared-enviroment/IntegrationTestSuite.php +++ b/tests/shared-enviroment/IntegrationTestSuite.php @@ -27,12 +27,18 @@ protected function invokeMethod($object, string $methodName, array $methodArgume return $method->invokeArgs($object, $methodArguments); } - public static function getRootDirectory() : string + /** + * @return string + */ + public static function getRootDirectory() { return dirname(__DIR__); } - public static function getSharedFixturesDirectory() : string + /** + * @return string + */ + public static function getSharedFixturesDirectory() { return static::getRootDirectory() . '/shared-fixtures'; } @@ -44,7 +50,7 @@ public static function getSharedFixturesDirectory() : string * * @return string */ - public static function getSharedFixtureContent(string $filename) : string + public static function getSharedFixtureContent(string $filename) { return file_get_contents(static::getSharedFixturesDirectory() . "/$filename"); } From 3059099279ffba5bc1b848d8e5fac6547e93c519 Mon Sep 17 00:00:00 2001 From: Eugene Matvejev Date: Wed, 22 Jun 2016 12:23:40 +0300 Subject: [PATCH 3/4] fix build --- tests/shared-enviroment/IntegrationTestSuite.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/shared-enviroment/IntegrationTestSuite.php b/tests/shared-enviroment/IntegrationTestSuite.php index ae038e5..557ae00 100644 --- a/tests/shared-enviroment/IntegrationTestSuite.php +++ b/tests/shared-enviroment/IntegrationTestSuite.php @@ -19,7 +19,7 @@ abstract class IntegrationTestSuite extends \PHPUnit_Framework_TestCase * @return mixed * @throws \Exception */ - protected function invokeMethod($object, string $methodName, array $methodArguments = []) + protected function invokeMethod($object, $methodName, array $methodArguments = []) { $method = (new \ReflectionClass(get_class($object)))->getMethod($methodName); $method->setAccessible(true); From 8d43dafcd503189c05e3698dcd9435494e996c30 Mon Sep 17 00:00:00 2001 From: Eugene Matvejev Date: Wed, 22 Jun 2016 12:29:17 +0300 Subject: [PATCH 4/4] use static arrays instead of const arrays as HHVM do not support arrays in constants yet --- src/Container/File.php | 4 ++-- src/Processor/Processor.php | 6 +++--- tests/phpunit/ProcessorTest.php | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) 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 index 3293701..7edeec2 100644 --- a/tests/phpunit/ProcessorTest.php +++ b/tests/phpunit/ProcessorTest.php @@ -93,7 +93,7 @@ public function processFileExpectedException() */ public function getFormatterClass() { - foreach (Processor::SUPPORTED_FORMATTERS as $formatter) { + foreach (Processor::$supportedFormatters as $formatter) { $expected = 'Leafo\\ScssPhp\\Formatter\\' . ucfirst($formatter); $this->assertEquals(