TestFixtureTest.php 6.8 KB

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