SchemaCacheTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 3.6.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Database;
  17. use Cake\Cache\Cache;
  18. use Cake\Database\Schema\CachedCollection;
  19. use Cake\Database\SchemaCache;
  20. use Cake\Datasource\ConnectionManager;
  21. use Cake\TestSuite\TestCase;
  22. /**
  23. * SchemaCache test.
  24. */
  25. class SchemaCacheTest extends TestCase
  26. {
  27. /**
  28. * Fixtures.
  29. *
  30. * @var array
  31. */
  32. protected $fixtures = ['core.Articles', 'core.Tags'];
  33. /**
  34. * Cache Engine Mock
  35. *
  36. * @var \Cake\Cache\CacheEngine
  37. */
  38. protected $cache;
  39. /**
  40. * @var \Cake\Datasource\ConnectionInterface
  41. */
  42. protected $connection;
  43. /**
  44. * setup method
  45. *
  46. * @return void
  47. */
  48. public function setUp(): void
  49. {
  50. parent::setUp();
  51. Cache::setConfig('orm_cache', ['className' => 'Array']);
  52. $this->cache = Cache::pool('orm_cache');
  53. $this->connection = ConnectionManager::get('test');
  54. $this->connection->cacheMetadata('orm_cache');
  55. }
  56. /**
  57. * Teardown
  58. *
  59. * @return void
  60. */
  61. public function tearDown(): void
  62. {
  63. parent::tearDown();
  64. $this->connection->cacheMetadata(false);
  65. unset($this->connection);
  66. Cache::drop('orm_cache');
  67. }
  68. /**
  69. * Test that clear enables the cache if it was disabled.
  70. *
  71. * @return void
  72. */
  73. public function testClearEnablesMetadataCache()
  74. {
  75. $this->connection->cacheMetadata(false);
  76. $ormCache = new SchemaCache($this->connection);
  77. $ormCache->clear();
  78. $this->assertInstanceOf(CachedCollection::class, $this->connection->getSchemaCollection());
  79. }
  80. /**
  81. * Test that build enables the cache if it was disabled.
  82. *
  83. * @return void
  84. */
  85. public function testBuildEnablesMetadataCache()
  86. {
  87. $this->connection->cacheMetadata(false);
  88. $ormCache = new SchemaCache($this->connection);
  89. $ormCache->build();
  90. $this->assertInstanceOf(CachedCollection::class, $this->connection->getSchemaCollection());
  91. }
  92. /**
  93. * Test build() with no args.
  94. *
  95. * @return void
  96. */
  97. public function testBuildNoArgs()
  98. {
  99. $ormCache = new SchemaCache($this->connection);
  100. $ormCache->build();
  101. $this->assertNotEmpty($this->cache->get('test_articles'));
  102. }
  103. /**
  104. * Test build() with one arg.
  105. *
  106. * @return void
  107. */
  108. public function testBuildNamedModel()
  109. {
  110. $ormCache = new SchemaCache($this->connection);
  111. $ormCache->build('articles');
  112. $this->assertNotEmpty($this->cache->get('test_articles'));
  113. }
  114. /**
  115. * Test build() overwrites cached data.
  116. *
  117. * @return void
  118. */
  119. public function testBuildOverwritesExistingData()
  120. {
  121. $this->cache->set('test_articles', 'dummy data');
  122. $ormCache = new SchemaCache($this->connection);
  123. $ormCache->build('articles');
  124. $this->assertNotSame('dummy data', $this->cache->get('test_articles'));
  125. }
  126. /**
  127. * Test clear() with no args.
  128. *
  129. * @return void
  130. */
  131. public function testClearNoArgs()
  132. {
  133. $this->cache->set('test_articles', 'dummy data');
  134. $ormCache = new SchemaCache($this->connection);
  135. $ormCache->clear();
  136. $this->assertFalse($this->cache->has('test_articles'));
  137. }
  138. /**
  139. * Test clear() with a model name.
  140. *
  141. * @return void
  142. */
  143. public function testClearNamedModel()
  144. {
  145. $this->cache->set('test_articles', 'dummy data');
  146. $ormCache = new SchemaCache($this->connection);
  147. $ormCache->clear('articles');
  148. $this->assertFalse($this->cache->has('test_articles'));
  149. }
  150. /**
  151. * Tests getting a schema config from a connection instance
  152. *
  153. * @return void
  154. */
  155. public function testGetSchemaWithConnectionInstance()
  156. {
  157. $ormCache = new SchemaCache($this->connection);
  158. $result = $ormCache->getSchema($this->connection);
  159. $this->assertInstanceOf(CachedCollection::class, $result);
  160. }
  161. }