Skip to content

Commit

Permalink
Array in attribute value gets joined by space, replaced most assertEq…
Browse files Browse the repository at this point in the history
…uals() with assertTag() in unit test
  • Loading branch information
CHH committed Mar 9, 2012
1 parent 7d3547a commit 2ffa5be
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
1 change: 1 addition & 0 deletions lib/Jazz.php
Expand Up @@ -107,6 +107,7 @@ protected static function renderHtmlTag($node)
$out = "<$tagName";

foreach ($attributes as $attr => $value) {
if (is_array($value)) $value = join(" ", $value);
$out .= " $attr=\"$value\"";
}

Expand Down
49 changes: 37 additions & 12 deletions tests/JazzTest.php
Expand Up @@ -2,42 +2,46 @@

class JazzTest extends \PHPUnit_Framework_TestCase
{
function testSimpleTagWithoutAttributesAndBody()
function testShortTag()
{
$this->assertEquals(
"<br />",
Jazz::render(["#br"])
);
}

function testSecondElementCanBeAttributes()
function testShortTagWithAttributes()
{
$this->assertEquals(
'<img src="/foo/bar.png" />',
$this->assertTag(
["tag" => "img", "attributes" => ["src" => "/foo/bar.png"]],
Jazz::render(["#img", ["src" => "/foo/bar.png"]])
);
}

function testTagsCanHaveTextContent()
function testShortTagWithContent()
{
$this->assertEquals(
"<p>Foo</p>",
$this->assertTag(
["tag" => "p", "content" => "Foo"],
Jazz::render(["#p", "Foo"])
);
}

function testRendersEmptyBody()
{
$this->assertEquals(
"<p></p>",
'<p></p>',
Jazz::render(["#p", ""])
);
}

function testTagWithAttributesAndBody()
{
$this->assertEquals(
'<h1 role="banner">Unicorns and Rainbows</h1>',
$this->assertTag(
[
"tag" => "h1",
"attributes" => ["role" => "banner"],
"content" => "Unicorns and Rainbows"
],
Jazz::render(
["#h1", ["role" => "banner"], "Unicorns and Rainbows"]
)
Expand All @@ -46,8 +50,16 @@ function testTagWithAttributesAndBody()

function testTagWithNestedTags()
{
$this->assertEquals(
'<nav class="main"><a href="/">Front Page</a></nav>',
$this->assertTag(
[
"tag" => "nav",
"attributes" => ["class" => "main"],
"child" => [
"tag" => "a",
"attributes" => ["href" => "/"],
"content" => "Front Page"
]
],
Jazz::render(["#nav", ["class" => "main"], [
["#a", ["href" => "/"], "Front Page"]
]])
Expand Down Expand Up @@ -96,4 +108,17 @@ function testThrowsExceptionWhenTooManyElementsInTag()
{
Jazz::render(["#p", "foo", "bar", "baz"]);
}

function testJoinsMultipleAttributeValues()
{
$expected = [
"tag" => "p",
"attributes" => [
"class" => "foo bar baz"
]
];

$actual = Jazz::render(["#p", ["class" => ["foo", "bar", "baz"], "Foo"]]);
$this->assertTag($expected, $actual);
}
}

0 comments on commit 2ffa5be

Please sign in to comment.