CollectionTest.php 2.4 KB

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