Skip to content

Commit

Permalink
test: Introduce case for Style
Browse files Browse the repository at this point in the history
  • Loading branch information
nilmerg committed Aug 24, 2023
1 parent 9aae808 commit 5bb0a3b
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
10 changes: 10 additions & 0 deletions tests/Lib/StyleWithTestableRenderCss.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace ipl\Tests\Web\Lib;

use ipl\Web\Style;

class StyleWithTestableRenderCss extends Style
{
use TestableRenderCss;
}
95 changes: 95 additions & 0 deletions tests/StyleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

namespace ipl\Tests\Web;

use ipl\Html\Html;
use ipl\Tests\Web\Lib\StyleWithTestableRenderCss;
use ipl\Web\Style;

class StyleTest extends TestCase
{
public function testNonceIsCorrectlyRendered()
{
$style = new StyleWithTestableRenderCss();
$style->setNonce('12345rtzujiklö');

$this->assertSame(
'<style nonce="12345rtzujiklö"></style>',
$style->render()
);
}

public function testModuleScopeIsCorrectlyRendered()
{
$style = new StyleWithTestableRenderCss();
$style->setModule('foo');
$style->add('#bar', ['width' => 'auto']);

$this->assertSame(
<<<'EOT'
<style>.icinga-module.module-foo {
#bar {
width: auto;
}
}</style>
EOT
,
$style->render()
);
}

public function testAddForAutomaticallyGeneratesIds()
{
$div = Html::tag('div');

$style = new Style();
$style->addFor($div, ['width' => 'auto']);

$this->assertNotNull($div->getAttribute('id')->getValue());
}

public function testAddForRespectsExistingIds()
{
$div = Html::tag('div', ['id' => 'foo']);

$style = new StyleWithTestableRenderCss();
$style->addFor($div, ['width' => 'auto']);

$this->assertSame(
<<<'EOT'
<style>#foo {
width: auto;
}</style>
EOT
,
$style->render()
);
}

/**
* @depends testNonceIsCorrectlyRendered
* @depends testModuleScopeIsCorrectlyRendered
* @depends testAddForRespectsExistingIds
*/
public function testCanBeCastedToString()
{
$div = Html::tag('div', ['id' => 'foo']);

$style = new StyleWithTestableRenderCss();
$style->setNonce('12345rtzujiklö');
$style->setModule('foo');
$style->addFor($div, ['width' => 'auto']);

$this->assertSame(
<<<'EOT'
<style nonce="12345rtzujiklö">.icinga-module.module-foo {
#foo {
width: auto;
}
}</style>
EOT
,
(string) $style
);
}
}

0 comments on commit 5bb0a3b

Please sign in to comment.