Skip to content

Commit 06ecebb

Browse files
author
Eugene Matvejev
authored
Merge pull request #47 from eugene-matvejev/first_tests
RC6 - first tests
2 parents fcb52d4 + 8d43daf commit 06ecebb

File tree

7 files changed

+217
-14
lines changed

7 files changed

+217
-14
lines changed

.scrutinizer.yml

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@ build:
1111
environment:
1212
php:
1313
version: "7.0.4"
14-
# tests:
15-
# override:
16-
# -
17-
# command: "php bin/phpunit -c phpunit.xml --colors=always --verbose --coverage-clover=coverage.xml"
18-
# coverage:
19-
# file: "coverage.xml"
20-
# format: "php-clover"
14+
tests:
15+
override:
16+
-
17+
command: "composer validate"
18+
override:
19+
-
20+
command: "php bin/phpunit -c phpunit.xml --colors=always --verbose --coverage-clover=coverage.xml"
21+
coverage:
22+
file: "coverage.xml"
23+
format: "php-clover"

composer.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@
1919
},
2020
"autoload-dev": {
2121
"psr-4": {
22-
"EM\\Tests\\CssCompiler\\": "tests/"
23-
}
22+
"EM\\Tests\\PHPUnit\\": "tests/phpunit"
23+
},
24+
"classmap": [
25+
"tests/shared-enviroment/IntegrationTestSuite.php"
26+
]
2427
},
2528
"config": {
2629
"bin-dir": "bin/"

src/Container/File.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class File
1111
const TYPE_SASS = 'sass';
1212
const TYPE_SCSS = 'scss';
1313
const TYPE_LESS = 'less';
14-
const SUPPORTED_TYPES = [
14+
static $supportedTypes = [
1515
self::TYPE_COMPASS,
1616
self::TYPE_SASS,
1717
self::TYPE_SCSS,
@@ -179,7 +179,7 @@ protected function detectSourceTypeFromPath($path)
179179
{
180180
$extension = strtolower(pathinfo($path, PATHINFO_EXTENSION));
181181

182-
return in_array($extension, static::SUPPORTED_TYPES)
182+
return in_array($extension, static::$supportedTypes)
183183
? $extension
184184
: static::TYPE_UNKNOWN;
185185
}

src/Processor/Processor.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Processor
1616
const FORMATTER_EXPANDED = 'expanded';
1717
const FORMATTER_NESTED = 'nested';
1818
const FORMATTER_COMPACT = 'compact';
19-
const SUPPORTED_FORMATTERS = [
19+
static $supportedFormatters = [
2020
self::FORMATTER_COMPRESSED,
2121
self::FORMATTER_CRUNCHED,
2222
self::FORMATTER_EXPANDED,
@@ -167,8 +167,8 @@ public function processFile(File $file)
167167
*/
168168
protected function getFormatterClass($formatter)
169169
{
170-
if (!in_array($formatter, static::SUPPORTED_FORMATTERS)) {
171-
throw new \InvalidArgumentException('unknown formatter, available options are: ' . print_r(static::SUPPORTED_FORMATTERS, true));
170+
if (!in_array($formatter, static::$supportedFormatters)) {
171+
throw new \InvalidArgumentException('unknown formatter, available options are: ' . print_r(static::$supportedFormatters, true));
172172
}
173173

174174
return 'Leafo\\ScssPhp\\Formatter\\' . ucfirst($formatter);

tests/phpunit/ProcessorTest.php

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
3+
namespace EM\Tests\PHPUnit;
4+
5+
use Composer\IO\IOInterface;
6+
use EM\CssCompiler\Container\File;
7+
use EM\CssCompiler\Processor\Processor;
8+
use EM\Tests\Environment\IntegrationTestSuite;
9+
10+
/**
11+
* @see Processor
12+
*/
13+
class ProcessorTest extends IntegrationTestSuite
14+
{
15+
protected $event;
16+
protected $io;
17+
protected $package;
18+
19+
protected function setUp()
20+
{
21+
$this->io = $this->getMockBuilder(IOInterface::class)->getMock();
22+
}
23+
24+
/**
25+
* @see Processor::attachFiles
26+
* @test
27+
*/
28+
public function attachFiles()
29+
{
30+
$paths = [
31+
static::getSharedFixturesDirectory() . '/sass',
32+
static::getSharedFixturesDirectory() . '/compass'
33+
];
34+
$cacheDir = dirname(dirname(__DIR__)) . '/var/cache';
35+
36+
foreach ($paths as $path) {
37+
$processor = new Processor($this->io);
38+
$processor->attachFiles($path, $cacheDir);
39+
40+
$this->assertCount(2, $processor->getFiles());
41+
}
42+
}
43+
44+
/**
45+
* @see Processor::attachFiles
46+
* @test
47+
*
48+
* @expectedException \Exception
49+
*/
50+
public function attachFilesExpectedException()
51+
{
52+
$path = static::getSharedFixturesDirectory() . '/do-not-exists';
53+
$cacheDir = dirname(dirname(__DIR__)) . '/var/cache';
54+
55+
$processor = new Processor($this->io);
56+
$processor->attachFiles($path, $cacheDir);
57+
58+
$this->assertCount(2, $processor->getFiles());
59+
}
60+
61+
/**
62+
* @see Processor::processFile
63+
* @test
64+
*/
65+
public function processFileSASS()
66+
{
67+
$file = (new File(static::getSharedFixturesDirectory() . '/compass/sass/layout.scss', ''))
68+
->setSourceContentFromSourcePath();
69+
70+
(new Processor($this->io))->processFile($file);
71+
72+
$this->assertNotEquals($file->getParsedContent(), $file->getSourceContent());
73+
}
74+
75+
/**
76+
* @see Processor::processFile
77+
* @test
78+
*
79+
* @expectedException \EM\CssCompiler\Exception\CompilerException
80+
*/
81+
public function processFileExpectedException()
82+
{
83+
$file = (new File(static::getSharedFixturesDirectory() . '/compass/sass/', ''))
84+
->setSourceContentFromSourcePath()
85+
->setType(File::TYPE_UNKNOWN);
86+
87+
(new Processor($this->io))->processFile($file);
88+
}
89+
90+
/**
91+
* @see Processor::getFormatterClass
92+
* @test
93+
*/
94+
public function getFormatterClass()
95+
{
96+
foreach (Processor::$supportedFormatters as $formatter) {
97+
$expected = 'Leafo\\ScssPhp\\Formatter\\' . ucfirst($formatter);
98+
99+
$this->assertEquals(
100+
$expected,
101+
$this->invokeMethod(new Processor($this->io), 'getFormatterClass', [$formatter])
102+
);
103+
}
104+
}
105+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace EM\Tests\Environment;
4+
5+
/**
6+
* @since 0.1
7+
*/
8+
abstract class IntegrationTestSuite extends \PHPUnit_Framework_TestCase
9+
{
10+
/**
11+
* able to invoke any non-static of object and return the result and throws exceptions if so
12+
*
13+
* useful to used to invoke non-public method of the class
14+
*
15+
* @param mixed $object
16+
* @param string $methodName
17+
* @param array $methodArguments
18+
*
19+
* @return mixed
20+
* @throws \Exception
21+
*/
22+
protected function invokeMethod($object, $methodName, array $methodArguments = [])
23+
{
24+
$method = (new \ReflectionClass(get_class($object)))->getMethod($methodName);
25+
$method->setAccessible(true);
26+
27+
return $method->invokeArgs($object, $methodArguments);
28+
}
29+
30+
/**
31+
* @return string
32+
*/
33+
public static function getRootDirectory()
34+
{
35+
return dirname(__DIR__);
36+
}
37+
38+
/**
39+
* @return string
40+
*/
41+
public static function getSharedFixturesDirectory()
42+
{
43+
return static::getRootDirectory() . '/shared-fixtures';
44+
}
45+
46+
/**
47+
* return content of the file in located in tests/shared-fixtures directory
48+
*
49+
* @param string $filename
50+
*
51+
* @return string
52+
*/
53+
public static function getSharedFixtureContent(string $filename)
54+
{
55+
return file_get_contents(static::getSharedFixturesDirectory() . "/$filename");
56+
}
57+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "eugene-matvejev/css-compiler",
3+
"description": "compiles SASS and LESS assets on composer's callback",
4+
"type": "lib",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Eugene Matvejev",
9+
"email": "eugene.matvejev@gmail.com"
10+
}
11+
],
12+
"autoload": {
13+
"psr-4": {
14+
"EM\\CssCompiler\\": "src/"
15+
},
16+
"classmap": [
17+
"ScriptHandler.php"
18+
]
19+
},
20+
"autoload-dev": {
21+
"psr-4": {
22+
"EM\\Tests\\CssCompiler\\": "tests/"
23+
}
24+
},
25+
"require": {
26+
"php": ">= 5.6",
27+
"leafo/lessphp": "^0.5",
28+
"leafo/scssphp": "@dev",
29+
"leafo/scssphp-compass": "@dev"
30+
},
31+
"require-dev": {
32+
"composer/composer": "^1.1",
33+
"phpunit/phpunit": "^5.3"
34+
}
35+
}

0 commit comments

Comments
 (0)