107 lines
		
	
	
		
			2.8 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			107 lines
		
	
	
		
			2.8 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?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 _testRows(iterable $rows, int $expectedCount) {
 | |
|     $count = 0;
 | |
|     foreach ($rows as $key => $row) {
 | |
|       $parts = ["got $key => {"];
 | |
|       $i = 0;
 | |
|       foreach ($row as $k => $v) {
 | |
|         if ($i++ > 0) $parts[] = ", ";
 | |
|         $parts[] = "$k=$v";
 | |
|       }
 | |
|       $parts[] = "}";
 | |
|       msg::info(implode("", $parts));
 | |
|       $count++;
 | |
|     }
 | |
|     self::assertSame($expectedCount, $count);
 | |
|   }
 | |
|   
 | |
|   function _testGet(string $dataId, int $expectedCount, callable $gencompute) {
 | |
|     msg::section($dataId);
 | |
|     cache::nc(true, true);
 | |
| 
 | |
|     msg::step("premier");
 | |
|     $rows = cache::get($dataId, $gencompute());
 | |
|     $this->_testRows($rows, $expectedCount);
 | |
|     msg::step("deuxième");
 | |
|     $rows = cache::get($dataId, $gencompute());
 | |
|     $this->_testRows($rows, $expectedCount);
 | |
| 
 | |
|     msg::step("vider le cache");
 | |
|     cache::nc(true, true);
 | |
| 
 | |
|     msg::step("premier");
 | |
|     $rows = cache::get($dataId, $gencompute());
 | |
|     $this->_testRows($rows, $expectedCount);
 | |
|     msg::step("deuxième");
 | |
|     $rows = cache::get($dataId, $gencompute());
 | |
|     $this->_testRows($rows, $expectedCount);
 | |
|   }
 | |
| 
 | |
|   function testGetStatic() {
 | |
|     $this->_testGet("getStatic", 3, function () {
 | |
|       return static function () {
 | |
|         msg::note("getdata");
 | |
|         return self::DATA;
 | |
|       };
 | |
|     });
 | |
|   }
 | |
| 
 | |
|   function testGetGenerator() {
 | |
|     $this->_testGet("getGenerator", 3, function () {
 | |
|       return static function () {
 | |
|         msg::note("gendata");
 | |
|         foreach (self::DATA as $key => $item) {
 | |
|           msg::info("yield $key");
 | |
|           yield $key => $item;
 | |
|           sleep(2);
 | |
|         }
 | |
|         msg::note("fin gendata");
 | |
|       };
 | |
|     });
 | |
|   }
 | |
| 
 | |
|   function _testAll(string $cursorId, int $expectedCount, callable $gencompute) {
 | |
|     msg::section($cursorId);
 | |
|     cache::nc(true, true);
 | |
| 
 | |
|     msg::step("premier");
 | |
|     $rows = cache::all($cursorId, $gencompute());
 | |
|     $this->_testRows($rows, $expectedCount);
 | |
|     msg::step("deuxième");
 | |
|     $rows = cache::all($cursorId, $gencompute());
 | |
|     $this->_testRows($rows, $expectedCount);
 | |
| 
 | |
|     msg::step("vider le cache");
 | |
|     cache::nc(true, true);
 | |
| 
 | |
|     msg::step("premier");
 | |
|     $rows = cache::all($cursorId, $gencompute());
 | |
|     $this->_testRows($rows, $expectedCount);
 | |
|     msg::step("deuxième");
 | |
|     $rows = cache::all($cursorId, $gencompute());
 | |
|     $this->_testRows($rows, $expectedCount);
 | |
|   }
 | |
| 
 | |
|   function testAllGenerator() {
 | |
|     $this->_testAll("allGenerator", 4, function() {
 | |
|       return static function() {
 | |
|         $db = new SourceDb();
 | |
|         msg::note("query source");
 | |
|         yield from $db->all("select * from source");
 | |
|       };
 | |
|     });
 | |
|   }
 | |
| }
 |