73 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			73 lines
		
	
	
		
			1.6 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 gendata() {
 | |
|     msg::note("gendata");
 | |
|     foreach (self::DATA as $key => $item) {
 | |
|       msg::info("yield $key");
 | |
|       yield $key => $item;
 | |
|       sleep(2);
 | |
|     }
 | |
|     msg::note("fin gendata");
 | |
|   }
 | |
| 
 | |
|   function _testRows(iterable $rows) {
 | |
|     $count = 0;
 | |
|     foreach ($rows as $key => $row) {
 | |
|       msg::info("got $key => {a={$row["a"]}, b={$row["b"]}}");
 | |
|       $count++;
 | |
|     }
 | |
|     self::assertSame(3, $count);
 | |
|   }
 | |
|   function _testGet(string $dataId, callable $gencompute) {
 | |
|     msg::section($dataId);
 | |
|     cache::nc();
 | |
| 
 | |
|     msg::step("premier");
 | |
|     $rows = cache::get($dataId, $gencompute());
 | |
|     $this->_testRows($rows);
 | |
|     msg::step("deuxième");
 | |
|     $rows = cache::get($dataId, $gencompute());
 | |
|     $this->_testRows($rows);
 | |
| 
 | |
|     msg::step("vider le cache");
 | |
|     cache::nc(true, true);
 | |
| 
 | |
|     msg::step("premier");
 | |
|     $rows = cache::get($dataId, $gencompute());
 | |
|     $this->_testRows($rows);
 | |
|     msg::step("deuxième");
 | |
|     $rows = cache::get($dataId, $gencompute());
 | |
|     $this->_testRows($rows);
 | |
|   }
 | |
| 
 | |
|   function testGetStatic() {
 | |
|     $this->_testGet("getStatic", function() {
 | |
|       return static function () {
 | |
|         msg::note("getdata");
 | |
|         return self::DATA;
 | |
|       };
 | |
|     });
 | |
|   }
 | |
| 
 | |
|   function testGetGenerator() {
 | |
|     $this->_testGet("getGenerator", function() {
 | |
|       return $this->gendata();
 | |
|     });
 | |
|   }
 | |
| 
 | |
|   function testAll() {
 | |
|     
 | |
|   }
 | |
| }
 |