Html.children.phpt 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * Test: Nette\Utils\Html children usage.
  4. */
  5. declare(strict_types=1);
  6. use Nette\Utils\Html;
  7. use Tester\Assert;
  8. require __DIR__ . '/../bootstrap.php';
  9. test('add', function () {
  10. $el = Html::el('ul');
  11. $el->create('li')->setText('one');
  12. $el->addHtml(Html::el('li')->setText('two'))->class('hello');
  13. Assert::same('<ul class="hello"><li>one</li><li>two</li></ul>', (string) $el);
  14. // with indentation
  15. Assert::match('
  16. <ul class="hello">
  17. <li>one</li>
  18. <li>two</li>
  19. </ul>
  20. ', $el->render(2), 'indentation');
  21. });
  22. test('', function () {
  23. $el = Html::el(null);
  24. $el->addHtml(Html::el('p')->setText('one'));
  25. $el->addText('<p>two</p>');
  26. $el->addHtml('<p>three</p>');
  27. Assert::same('<p>one</p>&lt;p&gt;two&lt;/p&gt;<p>three</p>', (string) $el);
  28. // ==> Get child:
  29. Assert::true(isset($el[0]));
  30. Assert::same('<p>one</p>', (string) $el[0]);
  31. Assert::same('&lt;p&gt;two&lt;/p&gt;', (string) $el[1]);
  32. Assert::same('<p>three</p>', (string) $el[2]);
  33. Assert::false(isset($el[3]));
  34. });
  35. test('Iterator', function () {
  36. $el = Html::el('select');
  37. $el->create('optgroup')->label('Main')->create('option')->setText('sub one')->create('option')->setText('sub two');
  38. $el->create('option')->setText('Item');
  39. Assert::same('<select><optgroup label="Main"><option>sub one<option>sub two</option></option></optgroup><option>Item</option></select>', (string) $el);
  40. Assert::same(2, count($el));
  41. Assert::same('optgroup', $el[0]->getName());
  42. Assert::same('option', $el[1]->getName());
  43. });
  44. test('counting', function () {
  45. $el = Html::el('ul');
  46. $el->addHtml('li');
  47. $el->addHtml('li');
  48. Assert::count(2, $el);
  49. Assert::count(2, $el->getChildren());
  50. Assert::count(2, iterator_to_array($el->getIterator()));
  51. unset($el[1]);
  52. Assert::count(1, $el->getChildren());
  53. $el->removeChildren();
  54. Assert::count(0, $el->getChildren());
  55. Assert::count(0, iterator_to_array($el->getIterator()));
  56. Assert::same('<ul></ul>', (string) $el);
  57. });
  58. test('cloning', function () {
  59. $el = Html::el('ul');
  60. $el->addHtml(Html::el('li'));
  61. $el2 = clone $el;
  62. Assert::same((string) $el, (string) $el2);
  63. });