<?php
namespace nur\v\base;

use nur\b\ui\IContent;
use nur\b\ui\IPrintable;
use nur\c;
use nur\t\TestCase;
use nur\v\vo;

class ComponentTest extends TestCase {
  function testGetContent() {
    $component = new MyComponent();
    $content = c::string($component->getContent());
    self::assertSame("<h1>le titre</h1>\n&lt;helloworld&gt;<bonjourmonde>print!static-content!generated-content!", $content);
  }

  function testPrint() {
    $component = new MyComponent();
    ob_start(null, 0, PHP_OUTPUT_HANDLER_STDFLAGS ^ PHP_OUTPUT_HANDLER_FLUSHABLE);
    $component->print();
    $content = ob_get_clean();
    self::assertSame("<h1>le titre</h1>\n&lt;helloworld&gt;<bonjourmonde>print!static-content!generated-content!", $content);
  }
}

class ZePrint implements IPrintable {
  function print(): void {
    echo "print!";
  }
}

class ZeStaticContent implements IContent {
  function getContent(): array {
    return ["static-content!"];
  }
}

class ZeGeneratedContent implements IContent {
  function getContent(): iterable {
    yield "generated-content!";
  }
}

class MyComponent extends SimplePrintable {
  function print(): void {
    vo::h1(null);
    vo::h1("le titre");
    vo::write("<hello");
    vo::write("world>");
    vo::write(["<bonjour"]);
    vo::write(["monde>"]);
    vo::write(new ZePrint());
    vo::write(new ZeStaticContent());
    vo::write(new ZeGeneratedContent());
  }
}
