Skip to content

Commit

Permalink
Adding some tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
peteboere committed Dec 27, 2013
1 parent c7a9e4d commit 988c17b
Show file tree
Hide file tree
Showing 21 changed files with 1,262 additions and 1 deletion.
1 change: 0 additions & 1 deletion .gitignore
Expand Up @@ -3,4 +3,3 @@ _*
phpunit.xml
composer.lock
vendor
tests
43 changes: 43 additions & 0 deletions tests/bootstrap.php
@@ -0,0 +1,43 @@
<?php

namespace {

if (! $loader = @include __DIR__ . '/../vendor/autoload.php') {
die('You must set up the project dependencies, run the following commands:'.PHP_EOL.
'curl -s http://getcomposer.org/installer | php'.PHP_EOL.
'php composer.phar install'.PHP_EOL);
}

$loader->add('CssCrush\UnitTest', __DIR__ . '/unit');
}

namespace CssCrush\UnitTest
{

function bootstrap_process($options = array())
{
$process = \CssCrush\Crush::$process = new \CssCrush\Process($options);
$process->prepare();
$process->resolveContext();
return $process;
}

function temp_file($contents = '')
{
$temporary_file = tempnam(sys_get_temp_dir(), 'crush');
if ($contents) {
file_put_contents($temporary_file, $contents);
}
return $temporary_file;
}

function stdout($message, $prepend_newline = false)
{
if (! is_string($message)) {
$buffer = ob_start();
print_r($message);
$message = ob_get_clean();
}
fwrite(STDOUT, ($prepend_newline ? "\n" : '') . $message . "\n");
}
}
37 changes: 37 additions & 0 deletions tests/unit/CssCrush/BalancedMatchTest.php
@@ -0,0 +1,37 @@
<?php

namespace CssCrush\UnitTest;

use CssCrush\BalancedMatch;

class BalancedMatchTest extends \PHPUnit_Framework_TestCase
{
public $process;

public function setUp()
{
$this->process = bootstrap_process();
$sample = '@foo; @bar {color: orange;} @baz';

$this->process->stream = new \CssCrush\Stream($sample);
}

public function testMatch()
{
$matches = $this->process->stream->matchAll('~@bar~');
$match_offset = $matches[0][0][1];

$match = new BalancedMatch($this->process->stream, $match_offset);

$this->assertEquals('color: orange;', $match->inside());
$this->assertEquals('@bar {color: orange;}', $match->whole());

$match = new BalancedMatch(clone $this->process->stream, $match_offset);
$match->unWrap();
$this->assertEquals('@foo; color: orange; @baz', $match->stream->__toString());

$match = new BalancedMatch(clone $this->process->stream, $match_offset);
$match->replace('@boo;');
$this->assertEquals('@foo; @boo; @baz', $match->stream->__toString());
}
}
65 changes: 65 additions & 0 deletions tests/unit/CssCrush/ColorTest.php
@@ -0,0 +1,65 @@
<?php

namespace CssCrush\UnitTest;

use CssCrush\Color;

class ColorTest extends \PHPUnit_Framework_TestCase
{
public function testConstruct()
{
$color = new Color('papayawhip');
$this->assertEquals('#ffefd5', (string) $color);

$color = new Color('#ccc');
$this->assertEquals('#cccccc', (string) $color);

$color = new Color('hsla(120,50%,50%,.8)');
$this->assertEquals('rgba(64,191,64,0.8)', (string) $color);
}

public function testAdjust()
{
$color = new Color('rgb(255,0,0)');
$color->toHsl()->adjust(array(0,0,0,-20));
$this->assertEquals('rgba(255,0,0,0.8)', (string) $color);
}

public function testGetHsl()
{
$color = new Color('red');
$this->assertEquals(array(0, 1, .5, 1), $color->getHsl());
}

public function testGetHex()
{
$color = new Color('red');
$this->assertEquals('#ff0000', $color->getHex());
}

public function testGetRgb()
{
$color = new Color('red');
$this->assertEquals(array(255, 0, 0, 1), $color->getRgb());
}

public function testGetComponent()
{
$color = new Color('red');
$this->assertEquals(255, $color->getComponent(0));
}

public function testSetComponent()
{
$color = new Color('red');
$color->setComponent(0, 0);
$this->assertEquals(0, $color->getComponent(0));
}

public function testColorSplit()
{
list($base_color, $opacity) = Color::colorSplit('red');
$this->assertEquals('red', $base_color);
$this->assertEquals(1, $opacity);
}
}
52 changes: 52 additions & 0 deletions tests/unit/CssCrush/DeclarationTest.php
@@ -0,0 +1,52 @@
<?php

namespace CssCrush\UnitTest;

use CssCrush\Crush;
use CssCrush\Declaration;
use CssCrush\Rule;

class DeclarationTest extends \PHPUnit_Framework_TestCase
{
protected $process;
protected $rule;
protected $declaration;

public function setUp()
{
$this->process = bootstrap_process(array('minify' => false));
$this->rule = new Rule('.foo', '-fOo-BAR: (10 + 10)px !important');
$this->declaration = new Declaration('-fOo-BAR', 'baz !important');
}

public function test__construct()
{
$this->assertTrue($this->declaration->important);
$this->assertTrue($this->declaration->valid);

$this->assertEquals('bar', $this->declaration->canonicalProperty);
$this->assertEquals('-foo-bar', $this->declaration->property);
$this->assertEquals('foo', $this->declaration->vendor);
$this->assertEquals('baz', $this->declaration->value);
}

public function test__toString()
{
$this->assertEquals('-foo-bar: baz !important', (string) $this->declaration);
}

public function testProcess()
{
foreach ($this->rule->declarations as $index => $declaration) {
$declaration->process($this->rule);
$this->assertEquals('20px', $declaration->value);
}
}

public function testIndexFunctions()
{
$declaration = new Declaration('color', 'rgba(0,0,0,.5), calc(100px)');
$declaration->indexFunctions();
$this->assertEquals(array('rgba' => true, 'calc' => true), $declaration->functions);
}
}
15 changes: 15 additions & 0 deletions tests/unit/CssCrush/ExtendArgTest.php
@@ -0,0 +1,15 @@
<?php

namespace CssCrush\UnitTest;

use CssCrush\ExtendArg;

class ExtendArgTest extends \PHPUnit_Framework_TestCase
{
public function test__construct()
{
$extend_arg = new ExtendArg('.foo :hover!');
$this->assertEquals('.foo :hover', $extend_arg->name);
$this->assertEquals(':hover', $extend_arg->pseudo);
}
}
74 changes: 74 additions & 0 deletions tests/unit/CssCrush/HookTest.php
@@ -0,0 +1,74 @@
<?php

namespace CssCrush\UnitTest;

use CssCrush\Hook;

class HookTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
Hook::$register = array();
$this->dummy_hook = __NAMESPACE__ . '\dummy_hook';
}

public function tearDown()
{
Hook::$register = array();
}

public function testAdd()
{
Hook::add('foo', $this->dummy_hook);
Hook::add('foo', 'strtoupper');

$this->assertEquals(array('foo' => array($this->dummy_hook=>true, 'strtoupper'=>true)), Hook::$register);

return Hook::$register;
}

/**
* @depends testAdd
*/
public function testRemove($register)
{
Hook::$register = $register;

Hook::remove('foo', 'strtoupper');

$this->assertEquals(array('foo' => array($this->dummy_hook=>true)), Hook::$register);

return Hook::$register;
}

/**
* @depends testRemove
*/
public function testRun($register)
{
Hook::$register = $register;

Hook::run('foo', $this);

$this->assertTrue($this->hookRan);

return Hook::$register;
}

/**
* @depends testRun
*/
public function testReset($register)
{
Hook::$register = $register;

Hook::reset();

$this->assertEquals(array(), Hook::$register);
}
}

function dummy_hook(HookTest $test)
{
$test->hookRan = true;
}
18 changes: 18 additions & 0 deletions tests/unit/CssCrush/LoggerTest.php
@@ -0,0 +1,18 @@
<?php

namespace CssCrush\UnitTest;

use CssCrush\Logger;
use Psr\Log\LoggerInterface;

class LoggerDummy extends Logger implements LoggerInterface {}

class LoggerTest extends \PHPUnit_Framework_TestCase
{
public function testInterface()
{
$logger = new LoggerDummy();

$this->assertTrue($logger instanceof LoggerInterface);
}
}

0 comments on commit 988c17b

Please sign in to comment.