97 lines
2.0 KiB
PHP

<?php
namespace nulib\web\content;
use nulib\cl;
use nulib\php\content\c;
use nulib\tests\TestCase;
class vTest extends TestCase {
function testStatic() {
$static = [
[v::H1, "title"],
[v::P, "text"],
];
self::assertSame(<<<EOT
<h1>title</h1>
<p>text</p>
EOT, c::to_string($static));
$static = [
[v::DIV, [
"before",
"class" => "div",
[v::SPAN, "spanned"],
"disabled" => false,
"checked" => true,
"after",
]],
];
self::assertSame(<<<EOT
<div class="div" checked="checked">before<span>spanned</span>after</div>
EOT, c::to_string($static));
}
function testDynamic() {
$dynamic = [
v::h1("title"),
v::p("text"),
];
self::assertSame(<<<EOT
<h1>title</h1>
<p>text</p>
EOT, c::to_string($dynamic));
$dynamic = [
v::div([
"before",
"class" => "div",
v::span("spanned"),
"disabled" => false,
"checked" => true,
"after",
]),
];
self::assertSame(<<<EOT
<div class="div" checked="checked">before<span>spanned</span>after</div>
EOT, c::to_string($dynamic));
}
function testDynamic2() {
$rows = [
["a" => 1, "b" => 2, "c" => 3],
["a" => "un", "b" => "deux", "c" => "trois"],
["a" => "one", "b" => "two", "c" => "three"],
];
$dynamic = v::table([
v::thead(v::tr(function() use ($rows) {
$headers = array_keys(cl::first($rows));
foreach ($headers as $header) {
yield v::th($header);
}
})),
v::tbody(function() use ($rows) {
foreach ($rows as $row) {
yield v::tr(function () use ($row) {
foreach ($row as $col) {
yield v::td($col);
}
});
}
}),
]);
self::assertSame(<<<EOT
<table>
<thead>
<tr><th>a</th><th>b</th><th>c</th></tr>
</thead>
<tbody>
<tr><td>1</td><td>2</td><td>3</td></tr>
<tr><td>un</td><td>deux</td><td>trois</td></tr>
<tr><td>one</td><td>two</td><td>three</td></tr>
</tbody>
</table>
EOT, c::to_string($dynamic));
}
}