SchemaCacheTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.6.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\ORM;
  16. use Cake\Cache\Cache;
  17. use Cake\Cache\CacheEngine;
  18. use Cake\Database\SchemaCache;
  19. use Cake\Database\Schema\CachedCollection;
  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. public $fixtures = ['core.articles', 'core.tags'];
  33. /**
  34. * Cache Engine Mock
  35. *
  36. * @var \Cake\Cache\CacheEngine
  37. */
  38. public $cache;
  39. /**
  40. * setup method
  41. *
  42. * @return void
  43. */
  44. public function setUp()
  45. {
  46. parent::setUp();
  47. $this->cache = $this->getMockBuilder(CacheEngine::class)->getMock();
  48. $this->cache->expects($this->any())
  49. ->method('init')
  50. ->will($this->returnValue(true));
  51. Cache::setConfig('orm_cache', $this->cache);
  52. $ds = ConnectionManager::get('test');
  53. $ds->cacheMetadata('orm_cache');
  54. }
  55. /**
  56. * Teardown
  57. *
  58. * @return void
  59. */
  60. public function tearDown()
  61. {
  62. parent::tearDown();
  63. Cache::drop('orm_cache');
  64. $ds = ConnectionManager::get('test');
  65. $ds->cacheMetadata(false);
  66. }
  67. /**
  68. * Test that clear enables the cache if it was disabled.
  69. *
  70. * @return void
  71. */
  72. public function testClearEnablesMetadataCache()
  73. {
  74. $ds = ConnectionManager::get('test');
  75. $ds->cacheMetadata(false);
  76. $ormCache = new SchemaCache($ds);
  77. $ormCache->clear();
  78. $this->assertInstanceOf(CachedCollection::class, $ds->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. $ds = ConnectionManager::get('test');
  88. $ds->cacheMetadata(false);
  89. $ormCache = new SchemaCache($ds);
  90. $ormCache->build();
  91. $this->assertInstanceOf(CachedCollection::class, $ds->getSchemaCollection());
  92. }
  93. /**
  94. * Test build() with no args.
  95. *
  96. * @return void
  97. */
  98. public function testBuildNoArgs()
  99. {
  100. $ds = ConnectionManager::get('test');
  101. $this->cache->expects($this->at(3))
  102. ->method('write')
  103. ->with('test_articles');
  104. $ormCache = new SchemaCache($ds);
  105. $ormCache->build();
  106. }
  107. /**
  108. * Test build() with one arg.
  109. *
  110. * @return void
  111. */
  112. public function testBuildNamedModel()
  113. {
  114. $ds = ConnectionManager::get('test');
  115. $this->cache->expects($this->once())
  116. ->method('write')
  117. ->with('test_articles');
  118. $this->cache->expects($this->never())
  119. ->method('delete');
  120. $ormCache = new SchemaCache($ds);
  121. $ormCache->build('articles');
  122. }
  123. /**
  124. * Test build() overwrites cached data.
  125. *
  126. * @return void
  127. */
  128. public function testBuildOverwritesExistingData()
  129. {
  130. $ds = ConnectionManager::get('test');
  131. $this->cache->expects($this->once())
  132. ->method('write')
  133. ->with('test_articles');
  134. $this->cache->expects($this->never())
  135. ->method('read');
  136. $this->cache->expects($this->never())
  137. ->method('delete');
  138. $ormCache = new SchemaCache($ds);
  139. $ormCache->build('articles');
  140. }
  141. /**
  142. * Test clear() with no args.
  143. *
  144. * @return void
  145. */
  146. public function testClearNoArgs()
  147. {
  148. $ds = ConnectionManager::get('test');
  149. $this->cache->expects($this->at(3))
  150. ->method('delete')
  151. ->with('test_articles');
  152. $ormCache = new SchemaCache($ds);
  153. $ormCache->clear();
  154. }
  155. /**
  156. * Test clear() with a model name.
  157. *
  158. * @return void
  159. */
  160. public function testClearNamedModel()
  161. {
  162. $ds = ConnectionManager::get('test');
  163. $this->cache->expects($this->never())
  164. ->method('write');
  165. $this->cache->expects($this->once())
  166. ->method('delete')
  167. ->with('test_articles');
  168. $ormCache = new SchemaCache($ds);
  169. $ormCache->clear('articles');
  170. }
  171. /**
  172. * Tests getting a schema config from a connection instance
  173. *
  174. * @return void
  175. */
  176. public function testGetSchemaWithConnectionInstance()
  177. {
  178. $ds = ConnectionManager::get('test');
  179. $ormCache = new SchemaCache($ds);
  180. $result = $ormCache->getSchema($ds);
  181. $this->assertInstanceOf(CachedCollection::class, $result);
  182. }
  183. }