TestFixtureTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 1.2.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\TestSuite;
  17. use Cake\Core\Exception\CakeException;
  18. use Cake\Database\Connection;
  19. use Cake\Database\Query\InsertQuery;
  20. use Cake\Database\Schema\TableSchema;
  21. use Cake\Database\StatementInterface;
  22. use Cake\Datasource\ConnectionManager;
  23. use Cake\Log\Log;
  24. use Cake\Test\Fixture\ArticlesFixture;
  25. use Cake\Test\Fixture\PostsFixture;
  26. use Cake\TestSuite\TestCase;
  27. use TestApp\Test\Fixture\FeaturedTagsFixture;
  28. use TestApp\Test\Fixture\LettersFixture;
  29. /**
  30. * Test case for TestFixture
  31. */
  32. class TestFixtureTest extends TestCase
  33. {
  34. /**
  35. * Fixtures for this test.
  36. *
  37. * @var list<string>
  38. */
  39. protected array $fixtures = ['core.Articles', 'core.Posts'];
  40. /**
  41. * Set up
  42. */
  43. public function setUp(): void
  44. {
  45. parent::setUp();
  46. Log::reset();
  47. }
  48. /**
  49. * Tear down
  50. */
  51. public function tearDown(): void
  52. {
  53. parent::tearDown();
  54. Log::reset();
  55. ConnectionManager::get('test')->execute('DROP TABLE IF EXISTS letters');
  56. }
  57. /**
  58. * test initializing a static fixture
  59. */
  60. public function testInitStaticFixture(): void
  61. {
  62. $Fixture = new ArticlesFixture();
  63. $this->assertSame('articles', $Fixture->table);
  64. $Fixture = new ArticlesFixture();
  65. $Fixture->table = '';
  66. $Fixture->init();
  67. $this->assertSame('articles', $Fixture->table);
  68. $schema = $Fixture->getTableSchema();
  69. $this->assertInstanceOf(TableSchema::class, $schema);
  70. }
  71. /**
  72. * Tests that trying to reflect with a table that doesn't exist throws an exception.
  73. */
  74. public function testReflectionMissingTable(): void
  75. {
  76. $this->expectException(CakeException::class);
  77. $this->expectExceptionMessage(
  78. sprintf(
  79. 'Cannot describe schema for table `letters` for fixture `%s`. The table does not exist.',
  80. LettersFixture::class
  81. ),
  82. );
  83. new LettersFixture();
  84. }
  85. /**
  86. * Tests schema reflection.
  87. */
  88. public function testReflection(): void
  89. {
  90. $db = ConnectionManager::get('test');
  91. $table = new TableSchema('letters', [
  92. 'id' => ['type' => 'integer'],
  93. 'letter' => ['type' => 'string', 'length' => 1],
  94. ]);
  95. $table->addConstraint('primary', ['type' => 'primary', 'columns' => ['id']]);
  96. $sql = $table->createSql($db);
  97. foreach ($sql as $stmt) {
  98. $db->execute($stmt);
  99. }
  100. $fixture = new LettersFixture();
  101. $this->assertSame(['id', 'letter'], $fixture->getTableSchema()->columns());
  102. }
  103. /**
  104. * Tests that schema reflection picks up dynamically configured column types.
  105. */
  106. public function testReflectionWithDynamicTypes(): void
  107. {
  108. $db = ConnectionManager::get('test');
  109. $table = new TableSchema('letters', [
  110. 'id' => ['type' => 'integer'],
  111. 'letter' => ['type' => 'string', 'length' => 1],
  112. 'complex_field' => ['type' => 'text'],
  113. ]);
  114. $table->addConstraint('primary', ['type' => 'primary', 'columns' => ['id']]);
  115. $sql = $table->createSql($db);
  116. foreach ($sql as $stmt) {
  117. $db->execute($stmt);
  118. }
  119. $table = $this->fetchTable('Letters', ['connection' => $db]);
  120. $table->getSchema()->setColumnType('complex_field', 'json');
  121. $fixture = new LettersFixture();
  122. $fixtureSchema = $fixture->getTableSchema();
  123. $this->assertSame(['id', 'letter', 'complex_field'], $fixtureSchema->columns());
  124. $this->assertSame('json', $fixtureSchema->getColumnType('complex_field'));
  125. }
  126. /**
  127. * test init with other tables used in initialize()
  128. *
  129. * The FeaturedTagsTable uses PostsTable, then when PostsFixture
  130. * reflects schema it should not raise an error.
  131. */
  132. public function testInitInitializeUsesRegistry(): void
  133. {
  134. $this->setAppNamespace();
  135. $fixture = new FeaturedTagsFixture();
  136. $posts = new PostsFixture();
  137. $posts->init();
  138. $expected = ['tag_id', 'priority'];
  139. $this->assertSame($expected, $fixture->getTableSchema()->columns());
  140. }
  141. /**
  142. * test the insert method
  143. */
  144. public function testInsert(): void
  145. {
  146. $fixture = new ArticlesFixture();
  147. $db = $this->getMockBuilder(Connection::class)
  148. ->disableOriginalConstructor()
  149. ->getMock();
  150. $query = $this->getMockBuilder(InsertQuery::class)
  151. ->setConstructorArgs([$db])
  152. ->getMock();
  153. $db->expects($this->once())
  154. ->method('insertQuery')
  155. ->willReturn($query);
  156. $query->expects($this->once())
  157. ->method('insert')
  158. ->with(['author_id', 'title', 'body', 'published'], ['author_id' => 'integer', 'title' => 'string', 'body' => 'text', 'published' => 'string'])
  159. ->willReturnSelf();
  160. $query->expects($this->once())
  161. ->method('into')
  162. ->with('articles')
  163. ->willReturnSelf();
  164. $expected = [
  165. ['author_id' => 1, 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y'],
  166. ['author_id' => 3, 'title' => 'Second Article', 'body' => 'Second Article Body', 'published' => 'Y'],
  167. ['author_id' => 1, 'title' => 'Third Article', 'body' => 'Third Article Body', 'published' => 'Y'],
  168. ];
  169. $query->expects($this->exactly(3))
  170. ->method('values')
  171. ->with(
  172. ...self::withConsecutive(
  173. [$expected[0]],
  174. [$expected[1]],
  175. [$expected[2]]
  176. )
  177. )
  178. ->willReturnSelf();
  179. $statement = $this->createMock(StatementInterface::class);
  180. $query->expects($this->once())
  181. ->method('execute')
  182. ->willReturn($statement);
  183. $this->assertSame(true, $fixture->insert($db));
  184. }
  185. /**
  186. * Test the truncate method.
  187. */
  188. public function testTruncate(): void
  189. {
  190. $fixture = new ArticlesFixture();
  191. $this->assertTrue($fixture->truncate(ConnectionManager::get('test')));
  192. $rows = ConnectionManager::get('test')->selectQuery()->select('*')->from('articles')->execute();
  193. $this->assertEmpty($rows->fetchAll());
  194. }
  195. }