OrmCacheShellTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Shell;
  16. use Cake\Cache\Cache;
  17. use Cake\Datasource\ConnectionManager;
  18. use Cake\Shell\OrmCacheShell;
  19. use Cake\TestSuite\TestCase;
  20. /**
  21. * OrmCacheShell test.
  22. */
  23. class OrmCacheShellTest extends TestCase
  24. {
  25. /**
  26. * Fixtures.
  27. *
  28. * @var array
  29. */
  30. public $fixtures = ['core.articles', 'core.tags'];
  31. /**
  32. * setup method
  33. *
  34. * @return void
  35. */
  36. public function setUp()
  37. {
  38. parent::setUp();
  39. $this->io = $this->getMockBuilder('Cake\Console\ConsoleIo')->getMock();
  40. $this->shell = new OrmCacheShell($this->io);
  41. $this->cache = $this->getMockBuilder('Cake\Cache\CacheEngine')->getMock();
  42. $this->cache->expects($this->any())
  43. ->method('init')
  44. ->will($this->returnValue(true));
  45. Cache::config('orm_cache', $this->cache);
  46. $ds = ConnectionManager::get('test');
  47. $ds->cacheMetadata('orm_cache');
  48. }
  49. /**
  50. * Teardown
  51. *
  52. * @return void
  53. */
  54. public function tearDown()
  55. {
  56. parent::tearDown();
  57. Cache::drop('orm_cache');
  58. $ds = ConnectionManager::get('test');
  59. $ds->cacheMetadata(false);
  60. }
  61. /**
  62. * Test that clear enables the cache if it was disabled.
  63. *
  64. * @return void
  65. */
  66. public function testClearEnablesMetadataCache()
  67. {
  68. $ds = ConnectionManager::get('test');
  69. $ds->cacheMetadata(false);
  70. $this->shell->params['connection'] = 'test';
  71. $this->shell->clear();
  72. $this->assertInstanceOf('Cake\Database\Schema\CachedCollection', $ds->schemaCollection());
  73. }
  74. /**
  75. * Test that build enables the cache if it was disabled.
  76. *
  77. * @return void
  78. */
  79. public function testBuildEnablesMetadataCache()
  80. {
  81. $ds = ConnectionManager::get('test');
  82. $ds->cacheMetadata(false);
  83. $this->shell->params['connection'] = 'test';
  84. $this->shell->build();
  85. $this->assertInstanceOf('Cake\Database\Schema\CachedCollection', $ds->schemaCollection());
  86. }
  87. /**
  88. * Test build() with no args.
  89. *
  90. * @return void
  91. */
  92. public function testBuildNoArgs()
  93. {
  94. $this->cache->expects($this->at(3))
  95. ->method('write')
  96. ->with('test_articles');
  97. $this->shell->params['connection'] = 'test';
  98. $this->shell->build();
  99. }
  100. /**
  101. * Test build() with one arg.
  102. *
  103. * @return void
  104. */
  105. public function testBuildNamedModel()
  106. {
  107. $this->cache->expects($this->once())
  108. ->method('write')
  109. ->with('test_articles');
  110. $this->cache->expects($this->never())
  111. ->method('delete');
  112. $this->shell->params['connection'] = 'test';
  113. $this->shell->build('articles');
  114. }
  115. /**
  116. * Test build() overwrites cached data.
  117. *
  118. * @return void
  119. */
  120. public function testBuildOverwritesExistingData()
  121. {
  122. $this->cache->expects($this->once())
  123. ->method('write')
  124. ->with('test_articles');
  125. $this->cache->expects($this->never())
  126. ->method('read');
  127. $this->cache->expects($this->never())
  128. ->method('delete');
  129. $this->shell->params['connection'] = 'test';
  130. $this->shell->build('articles');
  131. }
  132. /**
  133. * Test build() with a non-existing connection name.
  134. *
  135. * @return void
  136. */
  137. public function testBuildInvalidConnection()
  138. {
  139. $this->expectException(\Cake\Datasource\Exception\MissingDatasourceConfigException::class);
  140. $this->shell->params['connection'] = 'derpy-derp';
  141. $this->shell->build('articles');
  142. }
  143. /**
  144. * Test clear() with an invalid connection name.
  145. *
  146. * @return void
  147. */
  148. public function testClearInvalidConnection()
  149. {
  150. $this->expectException(\Cake\Datasource\Exception\MissingDatasourceConfigException::class);
  151. $this->shell->params['connection'] = 'derpy-derp';
  152. $this->shell->clear('articles');
  153. }
  154. /**
  155. * Test clear() with no args.
  156. *
  157. * @return void
  158. */
  159. public function testClearNoArgs()
  160. {
  161. $this->cache->expects($this->at(3))
  162. ->method('delete')
  163. ->with('test_articles');
  164. $this->shell->params['connection'] = 'test';
  165. $this->shell->clear();
  166. }
  167. /**
  168. * Test clear() with a model name.
  169. *
  170. * @return void
  171. */
  172. public function testClearNamedModel()
  173. {
  174. $this->cache->expects($this->never())
  175. ->method('write');
  176. $this->cache->expects($this->once())
  177. ->method('delete')
  178. ->with('test_articles');
  179. $this->shell->params['connection'] = 'test';
  180. $this->shell->clear('articles');
  181. }
  182. }