SchemaCacheTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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\Cache\Engine\NullEngine;
  19. use Cake\Database\Schema\CachedCollection;
  20. use Cake\Database\SchemaCache;
  21. use Cake\Datasource\ConnectionManager;
  22. use Cake\TestSuite\TestCase;
  23. /**
  24. * SchemaCache test.
  25. */
  26. class SchemaCacheTest extends TestCase
  27. {
  28. /**
  29. * Fixtures.
  30. *
  31. * @var array
  32. */
  33. protected $fixtures = ['core.Articles', 'core.Tags'];
  34. /**
  35. * Cache Engine Mock
  36. *
  37. * @var \Cake\Cache\CacheEngine
  38. */
  39. public $cache;
  40. protected $connection;
  41. /**
  42. * setup method
  43. *
  44. * @return void
  45. */
  46. public function setUp(): void
  47. {
  48. parent::setUp();
  49. Cache::setConfig('orm_cache', ['className' => 'Array']);
  50. $this->cache = Cache::pool('orm_cache');
  51. $this->connection = ConnectionManager::get('test');
  52. $this->connection->cacheMetadata('orm_cache');
  53. }
  54. /**
  55. * Teardown
  56. *
  57. * @return void
  58. */
  59. public function tearDown(): void
  60. {
  61. parent::tearDown();
  62. $this->connection->cacheMetadata(false);
  63. unset($this->connection);
  64. Cache::drop('orm_cache');
  65. }
  66. /**
  67. * Test that clear enables the cache if it was disabled.
  68. *
  69. * @return void
  70. */
  71. public function testClearEnablesMetadataCache()
  72. {
  73. $this->connection->cacheMetadata(false);
  74. $ormCache = new SchemaCache($this->connection);
  75. $ormCache->clear();
  76. $this->assertInstanceOf(CachedCollection::class, $this->connection->getSchemaCollection());
  77. }
  78. /**
  79. * Test that build enables the cache if it was disabled.
  80. *
  81. * @return void
  82. */
  83. public function testBuildEnablesMetadataCache()
  84. {
  85. $this->connection->cacheMetadata(false);
  86. $ormCache = new SchemaCache($this->connection);
  87. $ormCache->build();
  88. $this->assertInstanceOf(CachedCollection::class, $this->connection->getSchemaCollection());
  89. }
  90. /**
  91. * Test build() with no args.
  92. *
  93. * @return void
  94. */
  95. public function testBuildNoArgs()
  96. {
  97. $ormCache = new SchemaCache($this->connection);
  98. $ormCache->build();
  99. $this->assertNotEmpty($this->cache->get('test_articles'));
  100. }
  101. /**
  102. * Test build() with one arg.
  103. *
  104. * @return void
  105. */
  106. public function testBuildNamedModel()
  107. {
  108. $ormCache = new SchemaCache($this->connection);
  109. $ormCache->build('articles');
  110. $this->assertNotEmpty($this->cache->get('test_articles'));
  111. }
  112. /**
  113. * Test build() overwrites cached data.
  114. *
  115. * @return void
  116. */
  117. public function testBuildOverwritesExistingData()
  118. {
  119. $this->cache->set('test_articles', 'dummy data');
  120. $ormCache = new SchemaCache($this->connection);
  121. $ormCache->build('articles');
  122. $this->assertNotSame('dummy data', $this->cache->get('test_articles'));
  123. }
  124. /**
  125. * Test clear() with no args.
  126. *
  127. * @return void
  128. */
  129. public function testClearNoArgs()
  130. {
  131. $this->cache->set('test_articles', 'dummy data');
  132. $ormCache = new SchemaCache($this->connection);
  133. $ormCache->clear();
  134. $this->assertFalse($this->cache->has('test_articles'));
  135. }
  136. /**
  137. * Test clear() with a model name.
  138. *
  139. * @return void
  140. */
  141. public function testClearNamedModel()
  142. {
  143. $this->cache->set('test_articles', 'dummy data');
  144. $ormCache = new SchemaCache($this->connection);
  145. $ormCache->clear('articles');
  146. $this->assertFalse($this->cache->has('test_articles'));
  147. }
  148. /**
  149. * Tests getting a schema config from a connection instance
  150. *
  151. * @return void
  152. */
  153. public function testGetSchemaWithConnectionInstance()
  154. {
  155. $ormCache = new SchemaCache($this->connection);
  156. $result = $ormCache->getSchema($this->connection);
  157. $this->assertInstanceOf(CachedCollection::class, $result);
  158. }
  159. }