SchemaCacheTest.php 5.1 KB

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