CollectionTest.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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\Exception\DatabaseException;
  19. use Cake\Database\Schema\Collection;
  20. use Cake\Datasource\ConnectionManager;
  21. use Cake\TestSuite\TestCase;
  22. /**
  23. * Test case for Collection
  24. */
  25. class CollectionTest extends TestCase
  26. {
  27. /**
  28. * @var \Cake\Database\Connection
  29. */
  30. protected $connection;
  31. /**
  32. * @var array
  33. */
  34. protected array $fixtures = [
  35. 'core.Users',
  36. ];
  37. /**
  38. * Setup function
  39. */
  40. public function setUp(): void
  41. {
  42. parent::setUp();
  43. $this->connection = ConnectionManager::get('test');
  44. Cache::clear('_cake_model_');
  45. Cache::enable();
  46. }
  47. /**
  48. * Teardown function
  49. */
  50. public function tearDown(): void
  51. {
  52. $this->connection->cacheMetadata(false);
  53. parent::tearDown();
  54. unset($this->connection);
  55. }
  56. /**
  57. * Test that describing nonexistent tables fails.
  58. *
  59. * Tests for positive describe() calls are in each platformSchema
  60. * test case.
  61. */
  62. public function testDescribeIncorrectTable(): void
  63. {
  64. $this->expectException(DatabaseException::class);
  65. $schema = new Collection($this->connection);
  66. $this->assertNull($schema->describe('derp'));
  67. }
  68. /**
  69. * Tests that schema metadata is cached
  70. */
  71. public function testDescribeCache(): void
  72. {
  73. $this->connection->cacheMetadata('_cake_model_');
  74. $schema = $this->connection->getSchemaCollection();
  75. $table = $schema->describe('users');
  76. Cache::delete('test_users', '_cake_model_');
  77. $this->connection->cacheMetadata(true);
  78. $schema = $this->connection->getSchemaCollection();
  79. $result = $schema->describe('users');
  80. $this->assertEquals($table, $result);
  81. $result = Cache::read('test_users', '_cake_model_');
  82. $this->assertEquals($table, $result);
  83. }
  84. }