SchemaCacheTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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\CacheEngine;
  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. public $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. $this->cache = $this->getMockBuilder(CacheEngine::class)->getMock();
  50. $this->cache->expects($this->any())
  51. ->method('init')
  52. ->will($this->returnValue(true));
  53. Cache::setConfig('orm_cache', $this->cache);
  54. $this->connection = ConnectionManager::get('test');
  55. $this->connection->cacheMetadata('orm_cache');
  56. }
  57. /**
  58. * Teardown
  59. *
  60. * @return void
  61. */
  62. public function tearDown(): void
  63. {
  64. parent::tearDown();
  65. $this->connection->cacheMetadata(false);
  66. unset($this->connection);
  67. Cache::drop('orm_cache');
  68. }
  69. /**
  70. * Test that clear enables the cache if it was disabled.
  71. *
  72. * @return void
  73. */
  74. public function testClearEnablesMetadataCache()
  75. {
  76. $this->connection->cacheMetadata(false);
  77. $ormCache = new SchemaCache($this->connection);
  78. $ormCache->clear();
  79. $this->assertInstanceOf(CachedCollection::class, $this->connection->getSchemaCollection());
  80. }
  81. /**
  82. * Test that build enables the cache if it was disabled.
  83. *
  84. * @return void
  85. */
  86. public function testBuildEnablesMetadataCache()
  87. {
  88. $this->connection->cacheMetadata(false);
  89. $ormCache = new SchemaCache($this->connection);
  90. $ormCache->build();
  91. $this->assertInstanceOf(CachedCollection::class, $this->connection->getSchemaCollection());
  92. }
  93. /**
  94. * Test build() with no args.
  95. *
  96. * @return void
  97. */
  98. public function testBuildNoArgs()
  99. {
  100. $this->cache->method('set')
  101. ->will($this->returnValue(true));
  102. $this->cache->expects($this->at(3))
  103. ->method('set')
  104. ->with('test_articles')
  105. ->will($this->returnValue(true));
  106. $ormCache = new SchemaCache($this->connection);
  107. $ormCache->build();
  108. }
  109. /**
  110. * Test build() with one arg.
  111. *
  112. * @return void
  113. */
  114. public function testBuildNamedModel()
  115. {
  116. $this->cache->expects($this->once())
  117. ->method('set')
  118. ->with('test_articles')
  119. ->will($this->returnValue(true));
  120. $this->cache->expects($this->never())
  121. ->method('delete');
  122. $ormCache = new SchemaCache($this->connection);
  123. $ormCache->build('articles');
  124. }
  125. /**
  126. * Test build() overwrites cached data.
  127. *
  128. * @return void
  129. */
  130. public function testBuildOverwritesExistingData()
  131. {
  132. $this->cache->expects($this->once())
  133. ->method('set')
  134. ->with('test_articles')
  135. ->will($this->returnValue(true));
  136. $this->cache->expects($this->never())
  137. ->method('get');
  138. $this->cache->expects($this->never())
  139. ->method('delete');
  140. $ormCache = new SchemaCache($this->connection);
  141. $ormCache->build('articles');
  142. }
  143. /**
  144. * Test clear() with no args.
  145. *
  146. * @return void
  147. */
  148. public function testClearNoArgs()
  149. {
  150. $this->cache->method('delete')
  151. ->will($this->returnValue(true));
  152. $this->cache->expects($this->at(3))
  153. ->method('delete')
  154. ->with('test_articles');
  155. $ormCache = new SchemaCache($this->connection);
  156. $ormCache->clear();
  157. }
  158. /**
  159. * Test clear() with a model name.
  160. *
  161. * @return void
  162. */
  163. public function testClearNamedModel()
  164. {
  165. $this->cache->expects($this->never())
  166. ->method('set');
  167. $this->cache->expects($this->once())
  168. ->method('delete')
  169. ->with('test_articles')
  170. ->will($this->returnValue(true));
  171. $ormCache = new SchemaCache($this->connection);
  172. $ormCache->clear('articles');
  173. }
  174. /**
  175. * Tests getting a schema config from a connection instance
  176. *
  177. * @return void
  178. */
  179. public function testGetSchemaWithConnectionInstance()
  180. {
  181. $ormCache = new SchemaCache($this->connection);
  182. $result = $ormCache->getSchema($this->connection);
  183. $this->assertInstanceOf(CachedCollection::class, $result);
  184. }
  185. }