OrmCacheShellTest.php 4.4 KB

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