SchemaCacheCommandsTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 3.6.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Command;
  17. use Cake\Cache\Cache;
  18. use Cake\Cache\Engine\NullEngine;
  19. use Cake\Datasource\ConnectionManager;
  20. use Cake\TestSuite\ConsoleIntegrationTestTrait;
  21. use Cake\TestSuite\TestCase;
  22. /**
  23. * SchemaCacheCommands test.
  24. */
  25. class SchemaCacheCommandsTest extends TestCase
  26. {
  27. use ConsoleIntegrationTestTrait;
  28. /**
  29. * Fixtures.
  30. *
  31. * @var array
  32. */
  33. protected array $fixtures = ['core.Articles', 'core.Tags'];
  34. /**
  35. * @var \Cake\Datasource\ConnectionInterface
  36. */
  37. protected $connection;
  38. /**
  39. * @var \Cake\Cache\Engine\NullEngine|\PHPUnit\Framework\MockObject\MockObject
  40. */
  41. protected $cache;
  42. /**
  43. * setup method
  44. */
  45. public function setUp(): void
  46. {
  47. parent::setUp();
  48. $this->setAppNamespace();
  49. $this->cache = $this->getMockBuilder(NullEngine::class)
  50. ->onlyMethods(['set', 'get', 'delete'])
  51. ->getMock();
  52. Cache::setConfig('orm_cache', $this->cache);
  53. $this->connection = ConnectionManager::get('test');
  54. $this->connection->cacheMetadata('orm_cache');
  55. }
  56. /**
  57. * Teardown
  58. */
  59. public function tearDown(): void
  60. {
  61. $this->connection->cacheMetadata(false);
  62. parent::tearDown();
  63. unset($this->connection);
  64. Cache::drop('orm_cache');
  65. }
  66. /**
  67. * Test that clear enables the cache if it was disabled.
  68. */
  69. public function testClearEnablesMetadataCache(): void
  70. {
  71. $this->connection->cacheMetadata(false);
  72. $this->exec('schema_cache clear --connection test');
  73. $this->assertExitSuccess();
  74. $this->assertInstanceOf('Cake\Database\Schema\CachedCollection', $this->connection->getSchemaCollection());
  75. }
  76. /**
  77. * Test that build enables the cache if it was disabled.
  78. */
  79. public function testBuildEnablesMetadataCache(): void
  80. {
  81. $this->connection->cacheMetadata(false);
  82. $this->exec('schema_cache build --connection test');
  83. $this->assertExitSuccess();
  84. $this->assertInstanceOf('Cake\Database\Schema\CachedCollection', $this->connection->getSchemaCollection());
  85. }
  86. /**
  87. * Test build() with no args.
  88. */
  89. public function testBuildNoArgs(): void
  90. {
  91. $this->cache->expects($this->atLeastOnce())
  92. ->method('set')
  93. ->withConsecutive(['test_articles'])
  94. ->will($this->returnValue(true));
  95. $this->exec('schema_cache build --connection test');
  96. $this->assertExitSuccess();
  97. }
  98. /**
  99. * Test build() with one arg.
  100. */
  101. public function testBuildNamedModel(): void
  102. {
  103. $this->cache->expects($this->once())
  104. ->method('set')
  105. ->with('test_articles')
  106. ->will($this->returnValue(true));
  107. $this->cache->expects($this->never())
  108. ->method('delete')
  109. ->will($this->returnValue(false));
  110. $this->exec('schema_cache build --connection test articles');
  111. $this->assertExitSuccess();
  112. }
  113. /**
  114. * Test build() overwrites cached data.
  115. */
  116. public function testBuildOverwritesExistingData(): void
  117. {
  118. $this->cache->expects($this->once())
  119. ->method('set')
  120. ->with('test_articles')
  121. ->will($this->returnValue(true));
  122. $this->cache->expects($this->never())
  123. ->method('get');
  124. $this->cache->expects($this->never())
  125. ->method('delete')
  126. ->will($this->returnValue(false));
  127. $this->exec('schema_cache build --connection test articles');
  128. $this->assertExitSuccess();
  129. }
  130. /**
  131. * Test build() with a nonexistent connection name.
  132. */
  133. public function testBuildInvalidConnection(): void
  134. {
  135. $this->exec('schema_cache build --connection derpy-derp articles');
  136. $this->assertExitError();
  137. }
  138. /**
  139. * Test clear() with an invalid connection name.
  140. */
  141. public function testClearInvalidConnection(): void
  142. {
  143. $this->exec('schema_cache clear --connection derpy-derp articles');
  144. $this->assertExitError();
  145. }
  146. /**
  147. * Test clear() with no args.
  148. */
  149. public function testClearNoArgs(): void
  150. {
  151. $this->cache->expects($this->atLeastOnce())
  152. ->method('delete')
  153. ->withConsecutive(['test_articles'])
  154. ->will($this->returnValue(true));
  155. $this->exec('schema_cache clear --connection test');
  156. $this->assertExitSuccess();
  157. }
  158. /**
  159. * Test clear() with a model name.
  160. */
  161. public function testClearNamedModel(): void
  162. {
  163. $this->cache->expects($this->never())
  164. ->method('set')
  165. ->will($this->returnValue(true));
  166. $this->cache->expects($this->once())
  167. ->method('delete')
  168. ->with('test_articles')
  169. ->will($this->returnValue(false));
  170. $this->exec('schema_cache clear --connection test articles');
  171. $this->assertExitSuccess();
  172. }
  173. }