FixtureHelperTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 4.3.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\Datasource\ConnectionManager;
  19. use Cake\Test\Fixture\ArticlesFixture;
  20. use Cake\TestSuite\Fixture\FixtureHelper;
  21. use Cake\TestSuite\Fixture\TestFixture;
  22. use Cake\TestSuite\TestCase;
  23. use Company\TestPluginThree\Test\Fixture\ArticlesFixture as CompanyArticlesFixture;
  24. use PDOException;
  25. use TestApp\Test\Fixture\ArticlesFixture as AppArticlesFixture;
  26. use TestPlugin\Test\Fixture\ArticlesFixture as PluginArticlesFixture;
  27. use TestPlugin\Test\Fixture\Blog\CommentsFixture as PluginCommentsFixture;
  28. use UnexpectedValueException;
  29. class FixtureHelperTest extends TestCase
  30. {
  31. protected $fixtures = ['core.Articles'];
  32. /**
  33. * Clean up after test.
  34. */
  35. protected function tearDown(): void
  36. {
  37. parent::tearDown();
  38. $this->clearPlugins();
  39. ConnectionManager::dropAlias('test1');
  40. ConnectionManager::dropAlias('test2');
  41. }
  42. /**
  43. * Tests loading fixtures.
  44. */
  45. public function testLoadFixtures(): void
  46. {
  47. $this->setAppNamespace('TestApp');
  48. $this->loadPlugins(['TestPlugin']);
  49. $fixtures = (new FixtureHelper())->loadFixtures([
  50. 'core.Articles',
  51. 'plugin.TestPlugin.Articles',
  52. 'plugin.TestPlugin.Blog/Comments',
  53. 'plugin.Company/TestPluginThree.Articles',
  54. 'app.Articles',
  55. ]);
  56. $this->assertNotEmpty($fixtures);
  57. $this->assertInstanceOf(ArticlesFixture::class, $fixtures[ArticlesFixture::class]);
  58. $this->assertInstanceOf(PluginArticlesFixture::class, $fixtures[PluginArticlesFixture::class]);
  59. $this->assertInstanceOf(PluginCommentsFixture::class, $fixtures[PluginCommentsFixture::class]);
  60. $this->assertInstanceOf(CompanyArticlesFixture::class, $fixtures[CompanyArticlesFixture::class]);
  61. $this->assertInstanceOf(AppArticlesFixture::class, $fixtures[AppArticlesFixture::class]);
  62. }
  63. /**
  64. * Tests loading missing fixtures.
  65. */
  66. public function testLoadMissingFixtures(): void
  67. {
  68. $this->expectException(UnexpectedValueException::class);
  69. $this->expectExceptionMessage('Could not find fixture `core.ThisIsMissing`');
  70. (new FixtureHelper())->loadFixtures(['core.ThisIsMissing']);
  71. }
  72. /**
  73. * Tests loading duplicate fixtures.
  74. */
  75. public function testLoadDulicateFixtures(): void
  76. {
  77. $this->expectException(UnexpectedValueException::class);
  78. $this->expectExceptionMessage('Found duplicate fixture `core.Articles`');
  79. (new FixtureHelper())->loadFixtures(['core.Articles','core.Articles']);
  80. }
  81. /**
  82. * Tests running callback per connection
  83. */
  84. public function testPerConnection(): void
  85. {
  86. $fixture1 = $this->createMock(TestFixture::class);
  87. $fixture1->expects($this->once())
  88. ->method('connection')
  89. ->will($this->returnValue('test1'));
  90. $fixture2 = $this->createMock(TestFixture::class);
  91. $fixture2->expects($this->once())
  92. ->method('connection')
  93. ->will($this->returnValue('test2'));
  94. ConnectionManager::alias('test', 'test1');
  95. ConnectionManager::alias('test', 'test2');
  96. $numCalls = 0;
  97. (new FixtureHelper())->runPerConnection(function () use (&$numCalls) {
  98. ++$numCalls;
  99. }, [$fixture1, $fixture2]);
  100. $this->assertSame(2, $numCalls);
  101. }
  102. /**
  103. * Tests inserting fixtures.
  104. */
  105. public function testInsertFixtures(): void
  106. {
  107. /**
  108. * @var \Cake\Database\Connection $connection
  109. */
  110. $connection = ConnectionManager::get('test');
  111. $connection->deleteQuery('articles')->execute()->closeCursor();
  112. $rows = $connection->selectQuery('*')->from('articles')->execute();
  113. $this->assertEmpty($rows->fetchAll());
  114. $rows->closeCursor();
  115. $helper = new FixtureHelper();
  116. $helper->insert($helper->loadFixtures(['core.Articles']));
  117. $rows = $connection->selectQuery('*')->from('articles')->execute();
  118. $this->assertNotEmpty($rows->fetchAll());
  119. $rows->closeCursor();
  120. }
  121. /**
  122. * Tests handling PDO errors when inserting rows.
  123. */
  124. public function testInsertFixturesException(): void
  125. {
  126. $fixture = $this->getMockBuilder(TestFixture::class)->getMock();
  127. $fixture->expects($this->once())
  128. ->method('connection')
  129. ->will($this->returnValue('test'));
  130. $fixture->expects($this->once())
  131. ->method('insert')
  132. ->will($this->throwException(new PDOException('Missing key')));
  133. $helper = $this->getMockBuilder(FixtureHelper::class)
  134. ->onlyMethods(['sortByConstraint'])
  135. ->getMock();
  136. $helper->expects($this->any())
  137. ->method('sortByConstraint')
  138. ->will($this->returnValue([$fixture]));
  139. $this->expectException(CakeException::class);
  140. $this->expectExceptionMessage('Unable to insert rows for table ``');
  141. $helper->insert([$fixture]);
  142. }
  143. /**
  144. * Tests truncating fixtures.
  145. */
  146. public function testTruncateFixtures(): void
  147. {
  148. /**
  149. * @var \Cake\Database\Connection $connection
  150. */
  151. $connection = ConnectionManager::get('test');
  152. $rows = $connection->selectQuery('*')->from('articles')->execute();
  153. $this->assertNotEmpty($rows->fetchAll());
  154. $rows->closeCursor();
  155. $helper = new FixtureHelper();
  156. $helper->truncate($helper->loadFixtures(['core.Articles']));
  157. $rows = $connection->selectQuery('*')->from('articles')->execute();
  158. $this->assertEmpty($rows->fetchAll());
  159. $rows->closeCursor();
  160. }
  161. /**
  162. * Tests handling PDO errors when trucating rows.
  163. */
  164. public function testTruncateFixturesException(): void
  165. {
  166. $fixture = $this->getMockBuilder(TestFixture::class)->getMock();
  167. $fixture->expects($this->once())
  168. ->method('connection')
  169. ->will($this->returnValue('test'));
  170. $fixture->expects($this->once())
  171. ->method('truncate')
  172. ->will($this->throwException(new PDOException('Missing key')));
  173. $helper = $this->getMockBuilder(FixtureHelper::class)
  174. ->onlyMethods(['sortByConstraint'])
  175. ->getMock();
  176. $helper->expects($this->any())
  177. ->method('sortByConstraint')
  178. ->will($this->returnValue([$fixture]));
  179. $this->expectException(CakeException::class);
  180. $this->expectExceptionMessage('Unable to truncate table ``');
  181. $helper->truncate([$fixture]);
  182. }
  183. }