SchemaCacheTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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\Database;
  17. use Cake\Cache\Cache;
  18. use Cake\Database\Schema\CachedCollection;
  19. use Cake\Database\SchemaCache;
  20. use Cake\Datasource\ConnectionManager;
  21. use Cake\TestSuite\TestCase;
  22. /**
  23. * SchemaCache test.
  24. */
  25. class SchemaCacheTest extends TestCase
  26. {
  27. /**
  28. * Fixtures.
  29. *
  30. * @var array<string>
  31. */
  32. protected array $fixtures = ['core.Articles', 'core.Tags'];
  33. /**
  34. * Cache Engine Mock
  35. *
  36. * @var \Cake\Cache\CacheEngine
  37. */
  38. protected $cache;
  39. /**
  40. * @var \Cake\Datasource\ConnectionInterface
  41. */
  42. protected $connection;
  43. /**
  44. * setup method
  45. */
  46. public function setUp(): void
  47. {
  48. parent::setUp();
  49. Cache::setConfig('orm_cache', ['className' => 'Array']);
  50. $this->cache = Cache::pool('orm_cache');
  51. $this->connection = ConnectionManager::get('test');
  52. $this->connection->cacheMetadata('orm_cache');
  53. }
  54. /**
  55. * Teardown
  56. */
  57. public function tearDown(): void
  58. {
  59. $this->connection->cacheMetadata(false);
  60. parent::tearDown();
  61. unset($this->connection);
  62. Cache::drop('orm_cache');
  63. }
  64. /**
  65. * Test that clear enables the cache if it was disabled.
  66. */
  67. public function testClearEnablesMetadataCache(): void
  68. {
  69. $this->connection->cacheMetadata(false);
  70. $ormCache = new SchemaCache($this->connection);
  71. $ormCache->clear();
  72. $this->assertInstanceOf(CachedCollection::class, $this->connection->getSchemaCollection());
  73. }
  74. /**
  75. * Test that build enables the cache if it was disabled.
  76. */
  77. public function testBuildEnablesMetadataCache(): void
  78. {
  79. $this->connection->cacheMetadata(false);
  80. $ormCache = new SchemaCache($this->connection);
  81. $ormCache->build();
  82. $this->assertInstanceOf(CachedCollection::class, $this->connection->getSchemaCollection());
  83. }
  84. /**
  85. * Test build() with no args.
  86. */
  87. public function testBuildNoArgs(): void
  88. {
  89. $ormCache = new SchemaCache($this->connection);
  90. $ormCache->build();
  91. $this->assertNotEmpty($this->cache->get('test_articles'));
  92. }
  93. /**
  94. * Test build() with one arg.
  95. */
  96. public function testBuildNamedModel(): void
  97. {
  98. $ormCache = new SchemaCache($this->connection);
  99. $ormCache->build('articles');
  100. $this->assertNotEmpty($this->cache->get('test_articles'));
  101. }
  102. /**
  103. * Test build() overwrites cached data.
  104. */
  105. public function testBuildOverwritesExistingData(): void
  106. {
  107. $this->cache->set('test_articles', 'dummy data');
  108. $ormCache = new SchemaCache($this->connection);
  109. $ormCache->build('articles');
  110. $this->assertNotSame('dummy data', $this->cache->get('test_articles'));
  111. }
  112. /**
  113. * Test clear() with no args.
  114. */
  115. public function testClearNoArgs(): void
  116. {
  117. $this->cache->set('test_articles', 'dummy data');
  118. $ormCache = new SchemaCache($this->connection);
  119. $ormCache->clear();
  120. $this->assertFalse($this->cache->has('test_articles'));
  121. }
  122. /**
  123. * Test clear() with a model name.
  124. */
  125. public function testClearNamedModel(): void
  126. {
  127. $this->cache->set('test_articles', 'dummy data');
  128. $ormCache = new SchemaCache($this->connection);
  129. $ormCache->clear('articles');
  130. $this->assertFalse($this->cache->has('test_articles'));
  131. }
  132. /**
  133. * Tests getting a schema config from a connection instance
  134. */
  135. public function testGetSchemaWithConnectionInstance(): void
  136. {
  137. $ormCache = new SchemaCache($this->connection);
  138. $result = $ormCache->getSchema($this->connection);
  139. $this->assertInstanceOf(CachedCollection::class, $result);
  140. }
  141. }