|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Migration\Unit\General; |
| 4 | + |
| 5 | +use PHPUnit\Framework\TestCase; |
| 6 | +use Utopia\Migration\Cache; |
| 7 | +use Utopia\Migration\Resource; |
| 8 | +use Utopia\Migration\Resources\Database\Database; |
| 9 | +use Utopia\Migration\Resources\Database\Row; |
| 10 | +use Utopia\Migration\Resources\Database\Table; |
| 11 | + |
| 12 | +class CacheTest extends TestCase |
| 13 | +{ |
| 14 | + public function testTestCache(): void |
| 15 | + { |
| 16 | + $cache = new Cache(); |
| 17 | + |
| 18 | + $db1 = new Database(id: 'db1', name: 'db1'); |
| 19 | + $cache->add($db1); |
| 20 | + |
| 21 | + $this->assertEquals('database', $db1::getName()); |
| 22 | + $this->assertEquals(Resource::STATUS_PENDING, $db1->getStatus()); |
| 23 | + $this->assertArrayHasKey('database', $cache->getAll()); |
| 24 | + $this->assertEquals(1, count($cache->getAll()['database'])); |
| 25 | + $this->assertEquals(1, count($cache->get('database'))); |
| 26 | + |
| 27 | + $db2 = new Database(id: 'db2', name: 'db2'); |
| 28 | + $cache->add($db2); |
| 29 | + |
| 30 | + $this->assertEquals('database', $db1::getName()); |
| 31 | + $this->assertEquals(Resource::STATUS_PENDING, $db1->getStatus()); |
| 32 | + $this->assertArrayHasKey('database', $cache->getAll()); |
| 33 | + $this->assertEquals(2, count($cache->getAll()['database'])); |
| 34 | + $this->assertEquals(2, count($cache->get('database'))); |
| 35 | + |
| 36 | + /** |
| 37 | + * Add same resource second time, check overwrite |
| 38 | + */ |
| 39 | + $cache->add($db2); |
| 40 | + $this->assertEquals(2, count($cache->get('database'))); |
| 41 | + |
| 42 | + |
| 43 | + $db1->setStatus(Resource::STATUS_SUCCESS); |
| 44 | + $this->assertEquals(Resource::STATUS_SUCCESS, $db1->getStatus()); |
| 45 | + |
| 46 | + /** |
| 47 | + * Update cache |
| 48 | + */ |
| 49 | + $cache->update($db1); |
| 50 | + $this->assertEquals(2, count($cache->getAll()['database'])); |
| 51 | + $this->assertEquals(2, count($cache->get('database'))); |
| 52 | + |
| 53 | + $key = $cache->resolveResourceCacheKey($db1); |
| 54 | + |
| 55 | + /** |
| 56 | + * @var $resource Resource |
| 57 | + */ |
| 58 | + $resource = $cache->get('database')[$key]; |
| 59 | + $this->assertEquals('success', $resource->getStatus()); |
| 60 | + |
| 61 | + $table = new Table( |
| 62 | + database: $db1, |
| 63 | + id: 'table1', |
| 64 | + name: 'table', |
| 65 | + ); |
| 66 | + $this->assertEquals('table', $table::getName()); |
| 67 | + $this->assertEquals(Resource::STATUS_PENDING, $table->getStatus()); |
| 68 | + |
| 69 | + $cache->add($table); |
| 70 | + $this->assertEquals(1, count($cache->getAll()['table'])); |
| 71 | + $this->assertEquals(1, count($cache->get('table'))); |
| 72 | + |
| 73 | + /** |
| 74 | + * Check overwrite |
| 75 | + */ |
| 76 | + $cache->add($table); |
| 77 | + $this->assertEquals(1, count($cache->getAll()['table'])); |
| 78 | + $this->assertEquals(1, count($cache->get('table'))); |
| 79 | + |
| 80 | + |
| 81 | + $row = new Row( |
| 82 | + id: 'row1', |
| 83 | + table: $table, |
| 84 | + ); |
| 85 | + |
| 86 | + $this->assertEquals('row', $row::getName()); |
| 87 | + $this->assertEquals(Resource::STATUS_PENDING, $row->getStatus()); |
| 88 | + |
| 89 | + $cache->add($row); |
| 90 | + $this->assertEquals(1, count($cache->getAll()['row'])); |
| 91 | + $this->assertEquals(1, count($cache->get('row'))); |
| 92 | + |
| 93 | + /** |
| 94 | + * Rows have only counter on status key |
| 95 | + */ |
| 96 | + $this->assertEquals(['pending' => '1'], $cache->get('row')); |
| 97 | + |
| 98 | + $key = $cache->resolveResourceCacheKey($row); |
| 99 | + $this->assertArrayNotHasKey($key, $cache->get('row')); |
| 100 | + |
| 101 | + $row->setStatus(Resource::STATUS_SUCCESS); |
| 102 | + $cache->update($row); |
| 103 | + $this->assertEquals(['pending' => '1', 'success' => '1'], $cache->get('row')); |
| 104 | + |
| 105 | + $row->setStatus(Resource::STATUS_SUCCESS); |
| 106 | + $cache->update($row); |
| 107 | + $this->assertEquals(['pending' => '1', 'success' => '2'], $cache->get('row')); |
| 108 | + } |
| 109 | +} |
0 commit comments