SchemaCacheTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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.5.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\Schema\CachedCollection;
  19. use Cake\Datasource\ConnectionManager;
  20. use Cake\ORM\OrmCache;
  21. use Cake\TestSuite\TestCase;
  22. /**
  23. * OrmCache test.
  24. */
  25. class OrmCacheTest 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 OrmCache('test');
  77. $ormCache->clear();
  78. $this->assertInstanceOf('Cake\Database\Schema\CachedCollection', $ds->schemaCollection());
  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 OrmCache('test');
  90. $ormCache->build();
  91. $this->assertInstanceOf('Cake\Database\Schema\CachedCollection', $ds->schemaCollection());
  92. }
  93. /**
  94. * Test build() with no args.
  95. *
  96. * @return void
  97. */
  98. public function testBuildNoArgs()
  99. {
  100. $this->cache->expects($this->at(3))
  101. ->method('write')
  102. ->with('test_articles');
  103. $ormCache = new OrmCache('test');
  104. $ormCache->build();
  105. }
  106. /**
  107. * Test build() with one arg.
  108. *
  109. * @return void
  110. */
  111. public function testBuildNamedModel()
  112. {
  113. $this->cache->expects($this->once())
  114. ->method('write')
  115. ->with('test_articles');
  116. $this->cache->expects($this->never())
  117. ->method('delete');
  118. $ormCache = new OrmCache('test');
  119. $ormCache->build('articles');
  120. }
  121. /**
  122. * Test build() overwrites cached data.
  123. *
  124. * @return void
  125. */
  126. public function testBuildOverwritesExistingData()
  127. {
  128. $this->cache->expects($this->once())
  129. ->method('write')
  130. ->with('test_articles');
  131. $this->cache->expects($this->never())
  132. ->method('read');
  133. $this->cache->expects($this->never())
  134. ->method('delete');
  135. $ormCache = new OrmCache('test');
  136. $ormCache->build('articles');
  137. }
  138. /**
  139. * Test getting an instance with an invalid connection name.
  140. *
  141. * @expectedException \Cake\Datasource\Exception\MissingDatasourceConfigException
  142. * @return void
  143. */
  144. public function testInvalidConnection()
  145. {
  146. new OrmCache('invalid-connection');
  147. }
  148. /**
  149. * Test clear() with no args.
  150. *
  151. * @return void
  152. */
  153. public function testClearNoArgs()
  154. {
  155. $this->cache->expects($this->at(3))
  156. ->method('delete')
  157. ->with('test_articles');
  158. $ormCache = new OrmCache('test');
  159. $ormCache->clear();
  160. }
  161. /**
  162. * Test clear() with a model name.
  163. *
  164. * @return void
  165. */
  166. public function testClearNamedModel()
  167. {
  168. $this->cache->expects($this->never())
  169. ->method('write');
  170. $this->cache->expects($this->once())
  171. ->method('delete')
  172. ->with('test_articles');
  173. $ormCache = new OrmCache('test');
  174. $ormCache->clear('articles');
  175. }
  176. /**
  177. * Tests getting a schema config from a connection instance
  178. *
  179. * @return void
  180. */
  181. public function testGetSchemaWithConnectionInstance()
  182. {
  183. $ds = ConnectionManager::get('test');
  184. $ormCache = new OrmCache($ds);
  185. $result = $ormCache->getSchema($ds);
  186. $this->assertInstanceOf(CachedCollection::class, $result);
  187. }
  188. /**
  189. * Test passing invalid object
  190. *
  191. * @expectedException \InvalidArgumentException
  192. * @expectedExceptionMessage SchemaCache::getSchema() expects `Cake\Datasource\ConnectionInterface`, `stdClass` given.
  193. * @return void
  194. */
  195. public function testPassingInvalidObject()
  196. {
  197. new OrmCache(new \stdClass());
  198. }
  199. }