CollectionTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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.0.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Database\Schema;
  17. use Cake\Cache\Cache;
  18. use Cake\Database\Connection;
  19. use Cake\Database\Exception\DatabaseException;
  20. use Cake\Database\Schema\Collection;
  21. use Cake\Datasource\ConnectionManager;
  22. use Cake\TestSuite\TestCase;
  23. use PHPUnit\Framework\Attributes\DoesNotPerformAssertions;
  24. /**
  25. * Test case for Collection
  26. */
  27. class CollectionTest extends TestCase
  28. {
  29. /**
  30. * @var \Cake\Database\Connection
  31. */
  32. protected $connection;
  33. /**
  34. * @var list<string>
  35. */
  36. protected array $fixtures = [
  37. 'core.Users',
  38. ];
  39. /**
  40. * Setup function
  41. */
  42. public function setUp(): void
  43. {
  44. parent::setUp();
  45. $this->connection = ConnectionManager::get('test');
  46. Cache::clear('_cake_model_');
  47. Cache::enable();
  48. }
  49. /**
  50. * Teardown function
  51. */
  52. public function tearDown(): void
  53. {
  54. $this->connection->cacheMetadata(false);
  55. parent::tearDown();
  56. unset($this->connection);
  57. }
  58. /**
  59. * Test that describing nonexistent tables fails.
  60. *
  61. * Tests for positive describe() calls are in each platformSchema
  62. * test case.
  63. */
  64. public function testDescribeIncorrectTable(): void
  65. {
  66. $this->expectException(DatabaseException::class);
  67. $schema = new Collection($this->connection);
  68. $this->assertNull($schema->describe('derp'));
  69. }
  70. /**
  71. * Tests that schema metadata is cached
  72. */
  73. public function testDescribeCache(): void
  74. {
  75. $this->connection->cacheMetadata('_cake_model_');
  76. $schema = $this->connection->getSchemaCollection();
  77. $table = $schema->describe('users');
  78. Cache::delete('test_users', '_cake_model_');
  79. $this->connection->cacheMetadata(true);
  80. $schema = $this->connection->getSchemaCollection();
  81. $result = $schema->describe('users');
  82. $this->assertEquals($table, $result);
  83. $result = Cache::read('test_users', '_cake_model_');
  84. $this->assertEquals($table, $result);
  85. }
  86. #[DoesNotPerformAssertions]
  87. public function testListTables(): void
  88. {
  89. $config = $this->connection->config();
  90. $driver = new $config['driver']($config);
  91. $connection = new Connection([
  92. 'driver' => $driver,
  93. ]);
  94. $collection = new Collection($connection);
  95. $collection->listTables();
  96. }
  97. }