<?php
namespace nulib\cache;

use nulib\cl;
use nulib\output\msg;

class cacheTest extends _TestCase {
  const DATA = [
    "fr" => ["a" => "un", "b" => "deux"],
    "eng" => ["a" => "one", "b" => "two"],
    ["a" => 1, "b" => 2],
  ];

  function gendata() {
    foreach (self::DATA as $key => $item) {
      msg::info("yield $key");
      yield $key => $item;
      sleep(2);
    }
    msg::info("fin gendata");
  }

  function _testRows(iterable $rows) {
    $count = 0;
    foreach ($rows as $key => $row) {
      msg::info("got $key => ".var_export($row, true));
      $count++;
    }
    self::assertSame(3, $count);
  }

  function testUsage() {
    msg::section("all");
    $rows = cache::all($this->gendata(),"gendata");
    $this->_testRows($rows);

    msg::section("get");
    $rows = cache::get(function() {
      return self::DATA;
    },"gendata");
    $this->_testRows($rows);
  }

  function testNc() {
    cache::nc();

    msg::section("first pass");
    $rows = cache::all($this->gendata(),"gendata");
    $this->_testRows($rows);

    msg::section("second pass");
    $rows = cache::all($this->gendata(),"gendata");
    $this->_testRows($rows);
  }
}
