Skip to content

Commit

Permalink
Merge pull request #2 from preprocess/feature/update-plugin
Browse files Browse the repository at this point in the history
Add a test
  • Loading branch information
assertchris authored Sep 18, 2018
2 parents b34af3c + 65661ec commit 5934b8e
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 115 deletions.
5 changes: 0 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,5 @@
"psr-4": {
"Pre\\Phpx\\Html\\": "source"
}
},
"autoload-dev": {
"files": [
"tests/TestCase.php"
]
}
}
1 change: 0 additions & 1 deletion examples/.gitignore

This file was deleted.

59 changes: 0 additions & 59 deletions examples/custom-renderer-fixture.pre

This file was deleted.

5 changes: 0 additions & 5 deletions examples/custom-renderer.php

This file was deleted.

30 changes: 0 additions & 30 deletions examples/no-preprocessing.php

This file was deleted.

34 changes: 34 additions & 0 deletions source/Children.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Pre\Phpx\Html;

use ArrayIterator;
use IteratorAggregate;

class Children implements IteratorAggregate
{
public function __construct(array $children)
{
$this->children = $children;
}

public function getIterator()
{
return new ArrayIterator($this->children);
}

public function __toString()
{
$result = "";

foreach ($this->children as $child) {
if (is_callable($child) && !is_string($child)) {
$result .= $child();
} else {
$result .= $child;
}
}

return $result;
}
}
8 changes: 6 additions & 2 deletions source/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ private function classNameFrom($className = null, $wrapped = true)
return null;
}

if (is_callable($className)) {
if (is_callable($className) && !is_string($className)) {
$result = $this->classNameFrom($className(), false);
}

Expand Down Expand Up @@ -696,7 +696,7 @@ private function styleFrom($style = null, $wrapped = true)
private function childrenFrom($children = null)
{
if (is_array($children)) {
return join("", $children);
return new Children($children);
}

return $children;
Expand Down Expand Up @@ -727,6 +727,10 @@ private function attributesFrom($props)
$value = $value();
}

if (is_array($value)) {
$value = join(" ", $value);
}

$attributes[] = "{$key}=\"{$value}\"";
}

Expand Down
34 changes: 32 additions & 2 deletions tests/RendererTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,39 @@
<?php

class RendererTest extends TestCase
use PHPUnit\Framework\TestCase;

use function Pre\Phpx\Html\render;

class RenderTest extends TestCase
{
public function test_can_render_html()
{
// TODO
$actual = render("a", [
"className" => [
"link",
function() {
return "lazy-link";
},
],
"style" => [
"border" => "solid 1px #efefef",
"font-weight" => function() {
return "bold";
},
],
"children" => [
"left",
" ",
function() {
return "middle";
},
" ",
"right",
],
]);

$expected = "<a class=\"link lazy-link\" style=\"border: solid 1px #efefef; font-weight: bold;\">left middle right</a>";

$this->assertEquals($expected, $actual, $actual);
}
}
11 changes: 0 additions & 11 deletions tests/TestCase.php

This file was deleted.

0 comments on commit 5934b8e

Please sign in to comment.