OrmCacheShellTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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\Console\Command;
  16. use Cake\Console\Command\OrmCacheShell;
  17. use Cake\Cache\Cache;
  18. use Cake\Datasource\ConnectionManager;
  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.article', 'core.tag'];
  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 build() with no args.
  60. *
  61. * @return void
  62. */
  63. public function testBuildNoArgs() {
  64. $this->cache->expects($this->at(2))
  65. ->method('write')
  66. ->with('test_articles');
  67. $this->shell->params['connection'] = 'test';
  68. $this->shell->build();
  69. }
  70. /**
  71. * Test build() with one arg.
  72. *
  73. * @return void
  74. */
  75. public function testBuildNamedModel() {
  76. $this->cache->expects($this->once())
  77. ->method('write')
  78. ->with('test_articles');
  79. $this->cache->expects($this->never())
  80. ->method('delete');
  81. $this->shell->params['connection'] = 'test';
  82. $this->shell->build('articles');
  83. }
  84. /**
  85. * Test build() overwrites cached data.
  86. *
  87. * @return void
  88. */
  89. public function testBuildOverwritesExistingData() {
  90. $this->cache->expects($this->once())
  91. ->method('write')
  92. ->with('test_articles');
  93. $this->cache->expects($this->never())
  94. ->method('read');
  95. $this->cache->expects($this->never())
  96. ->method('delete');
  97. $this->shell->params['connection'] = 'test';
  98. $this->shell->build('articles');
  99. }
  100. /**
  101. * Test build() with a non-existing connection name.
  102. *
  103. * @expectedException Cake\Datasource\Error\MissingDatasourceConfigException
  104. * @return void
  105. */
  106. public function testBuildInvalidConnection() {
  107. $this->shell->params['connection'] = 'derpy-derp';
  108. $this->shell->build('articles');
  109. }
  110. /**
  111. * Test clear() with an invalid connection name.
  112. *
  113. * @expectedException Cake\Datasource\Error\MissingDatasourceConfigException
  114. * @return void
  115. */
  116. public function testClearInvalidConnection() {
  117. $this->shell->params['connection'] = 'derpy-derp';
  118. $this->shell->clear('articles');
  119. }
  120. /**
  121. * Test clear() with no args.
  122. *
  123. * @return void
  124. */
  125. public function testClearNoArgs() {
  126. $this->cache->expects($this->at(2))
  127. ->method('delete')
  128. ->with('test_articles');
  129. $this->shell->params['connection'] = 'test';
  130. $this->shell->clear();
  131. }
  132. /**
  133. * Test clear() with a model name.
  134. *
  135. * @return void
  136. */
  137. public function testClearNamedModel() {
  138. $this->cache->expects($this->never())
  139. ->method('write');
  140. $this->cache->expects($this->once())
  141. ->method('delete')
  142. ->with('test_articles');
  143. $this->shell->params['connection'] = 'test';
  144. $this->shell->clear('articles');
  145. }
  146. }