Skip to content

Commit

Permalink
test: Introduce case for LessRuleset
Browse files Browse the repository at this point in the history
  • Loading branch information
nilmerg committed Aug 28, 2023
1 parent c8b396c commit c9a5ae4
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 0 deletions.
112 changes: 112 additions & 0 deletions tests/LessRulesetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

namespace ipl\Tests\Web;

use ErrorException;
use ipl\Tests\Web\Lib\LessRulesetWithTestableRenderCss;
use ipl\Web\LessRuleset;

class LessRulesetTest extends TestCase
{
public function testSetWithSelectorIsCorrectlyRendered()
{
$set = LessRulesetWithTestableRenderCss::create('.foo', ['width' => 'auto']);

$this->assertSame('.foo', $set->getSelector());
$this->assertSame(
<<<'EOT'
.foo {
width: auto;
}
EOT
,
$set->renderCss()
);
}

public function testSetWithoutSelectorIsCorrectlyRendered()
{
$set = new LessRulesetWithTestableRenderCss();
$set->setProperties(['width' => 'auto', 'height' => 'auto']);

$this->assertSame(['width' => 'auto', 'height' => 'auto'], $set->getProperties());
$this->assertSame(
<<<'EOT'
width: auto;
height: auto;
EOT
,
$set->renderCss()
);
}

public function testNestedSetsAreCorrectlyRendered()
{
$set = LessRulesetWithTestableRenderCss::create('.level1', ['width' => 'auto']);
$set->addRuleset(
LessRulesetWithTestableRenderCss::create('.level2', ['width' => '2em'])
->add('.level3', ['width' => '1em'])
);

$this->assertSame(
<<<'EOT'
.level1 {
width: auto;
.level2 {
width: 2em;
.level3 {
width: 1em;
}
}
}
EOT
,
$set->renderCss()
);
}

public function testSetsCanBeAdjustedAfterCreation()
{
$set = LessRulesetWithTestableRenderCss::create('.foo', ['width' => 'auto']);
$set->setProperty('line-height', 1.5);
$set['color'] = '#abc';

$this->assertSame('#abc', $set['color']);
$this->assertSame('1.5', $set->getProperty('line-height'));
$this->assertSame(
<<<'EOT'
.foo {
width: auto;
line-height: 1.5;
color: #abc;
}
EOT
,
$set->renderCss()
);
}

public function testAccessingAMissingPropertyThrowsIfGetPropertyIsUsed()
{
$set = new LessRuleset();

try {
$set->getProperty('missing');
} catch (ErrorException $_) {
// $this->expectException() didn't work on GitHub for an unknown reason
$this->assertTrue(true);
}
}

public function testAccessingAMissingPropertyThrowsIfOffsetAccessIsUsed()
{
$set = new LessRuleset();

try {
$set['missing'];
} catch (ErrorException $_) {
// $this->expectException() didn't work on GitHub for an unknown reason
$this->assertTrue(true);
}
}
}
10 changes: 10 additions & 0 deletions tests/Lib/LessRulesetWithTestableRenderCss.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace ipl\Tests\Web\Lib;

use ipl\Web\LessRuleset;

class LessRulesetWithTestableRenderCss extends LessRuleset
{
use TestableRenderCss;
}
13 changes: 13 additions & 0 deletions tests/Lib/TestableRenderCss.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace ipl\Tests\Web\Lib;

use ipl\Web\LessRuleset;

trait TestableRenderCss
{
public function renderCss(): string
{
return $this->renderLess();
}
}

0 comments on commit c9a5ae4

Please sign in to comment.