SchemaCacheTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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\Database;
  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->method('write')
  102. ->will($this->returnValue(true));
  103. $this->cache->expects($this->at(3))
  104. ->method('write')
  105. ->with('test_articles');
  106. $ormCache = new SchemaCache($ds);
  107. $ormCache->build();
  108. }
  109. /**
  110. * Test build() with one arg.
  111. *
  112. * @return void
  113. */
  114. public function testBuildNamedModel()
  115. {
  116. $ds = ConnectionManager::get('test');
  117. $this->cache->expects($this->once())
  118. ->method('write')
  119. ->with('test_articles')
  120. ->will($this->returnValue(true));
  121. $this->cache->expects($this->never())
  122. ->method('delete');
  123. $ormCache = new SchemaCache($ds);
  124. $ormCache->build('articles');
  125. }
  126. /**
  127. * Test build() overwrites cached data.
  128. *
  129. * @return void
  130. */
  131. public function testBuildOverwritesExistingData()
  132. {
  133. $ds = ConnectionManager::get('test');
  134. $this->cache->expects($this->once())
  135. ->method('write')
  136. ->with('test_articles')
  137. ->will($this->returnValue(true));
  138. $this->cache->expects($this->never())
  139. ->method('read');
  140. $this->cache->expects($this->never())
  141. ->method('delete');
  142. $ormCache = new SchemaCache($ds);
  143. $ormCache->build('articles');
  144. }
  145. /**
  146. * Test clear() with no args.
  147. *
  148. * @return void
  149. */
  150. public function testClearNoArgs()
  151. {
  152. $ds = ConnectionManager::get('test');
  153. $this->cache->expects($this->at(3))
  154. ->method('delete')
  155. ->with('test_articles');
  156. $ormCache = new SchemaCache($ds);
  157. $ormCache->clear();
  158. }
  159. /**
  160. * Test clear() with a model name.
  161. *
  162. * @return void
  163. */
  164. public function testClearNamedModel()
  165. {
  166. $ds = ConnectionManager::get('test');
  167. $this->cache->expects($this->never())
  168. ->method('write');
  169. $this->cache->expects($this->once())
  170. ->method('delete')
  171. ->with('test_articles');
  172. $ormCache = new SchemaCache($ds);
  173. $ormCache->clear('articles');
  174. }
  175. /**
  176. * Tests getting a schema config from a connection instance
  177. *
  178. * @return void
  179. */
  180. public function testGetSchemaWithConnectionInstance()
  181. {
  182. $ds = ConnectionManager::get('test');
  183. $ormCache = new SchemaCache($ds);
  184. $result = $ormCache->getSchema($ds);
  185. $this->assertInstanceOf(CachedCollection::class, $result);
  186. }
  187. }