SchemaCacheTest.php 5.4 KB

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