FixtureHelperTest.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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\Database\Connection;
  19. use Cake\Datasource\ConnectionInterface;
  20. use Cake\Datasource\ConnectionManager;
  21. use Cake\Test\Fixture\ArticlesFixture;
  22. use Cake\TestSuite\Fixture\FixtureHelper;
  23. use Cake\TestSuite\Fixture\TestFixture;
  24. use Cake\TestSuite\TestCase;
  25. use Company\TestPluginThree\Test\Fixture\ArticlesFixture as CompanyArticlesFixture;
  26. use PDOException;
  27. use TestApp\Test\Fixture\ArticlesFixture as AppArticlesFixture;
  28. use TestPlugin\Test\Fixture\ArticlesFixture as PluginArticlesFixture;
  29. use TestPlugin\Test\Fixture\Blog\CommentsFixture as PluginCommentsFixture;
  30. use UnexpectedValueException;
  31. class FixtureHelperTest extends TestCase
  32. {
  33. protected array $fixtures = ['core.Articles'];
  34. /**
  35. * Clean up after test.
  36. */
  37. protected function tearDown(): void
  38. {
  39. parent::tearDown();
  40. $this->clearPlugins();
  41. ConnectionManager::dropAlias('test1');
  42. ConnectionManager::dropAlias('test2');
  43. }
  44. /**
  45. * Tests loading fixtures.
  46. */
  47. public function testLoadFixtures(): void
  48. {
  49. $this->setAppNamespace('TestApp');
  50. $this->loadPlugins(['TestPlugin']);
  51. $fixtures = (new FixtureHelper())->loadFixtures([
  52. 'core.Articles',
  53. 'plugin.TestPlugin.Articles',
  54. 'plugin.TestPlugin.Blog/Comments',
  55. 'plugin.Company/TestPluginThree.Articles',
  56. 'app.Articles',
  57. ]);
  58. $this->assertNotEmpty($fixtures);
  59. $this->assertInstanceOf(ArticlesFixture::class, $fixtures[ArticlesFixture::class]);
  60. $this->assertInstanceOf(PluginArticlesFixture::class, $fixtures[PluginArticlesFixture::class]);
  61. $this->assertInstanceOf(PluginCommentsFixture::class, $fixtures[PluginCommentsFixture::class]);
  62. $this->assertInstanceOf(CompanyArticlesFixture::class, $fixtures[CompanyArticlesFixture::class]);
  63. $this->assertInstanceOf(AppArticlesFixture::class, $fixtures[AppArticlesFixture::class]);
  64. }
  65. /**
  66. * Tests that possible table instances used in the fixture loading mechanism
  67. * do not remain in the table locator.
  68. */
  69. public function testLoadFixturesDoesNotPolluteTheTableLocator(): void
  70. {
  71. (new FixtureHelper())->loadFixtures([
  72. 'core.Articles',
  73. 'plugin.TestPlugin.Blog/Comments',
  74. ]);
  75. $this->assertFalse($this->getTableLocator()->exists('Articles'));
  76. $this->assertFalse($this->getTableLocator()->exists('Comments'));
  77. }
  78. /**
  79. * Tests loading missing fixtures.
  80. */
  81. public function testLoadMissingFixtures(): void
  82. {
  83. $this->expectException(UnexpectedValueException::class);
  84. $this->expectExceptionMessage('Could not find fixture `core.ThisIsMissing`');
  85. (new FixtureHelper())->loadFixtures(['core.ThisIsMissing']);
  86. }
  87. /**
  88. * Tests loading duplicate fixtures.
  89. */
  90. public function testLoadDulicateFixtures(): void
  91. {
  92. $this->expectException(UnexpectedValueException::class);
  93. $this->expectExceptionMessage('Found duplicate fixture `core.Articles`');
  94. (new FixtureHelper())->loadFixtures(['core.Articles','core.Articles']);
  95. }
  96. /**
  97. * Tests running callback per connection
  98. */
  99. public function testPerConnection(): void
  100. {
  101. $fixture1 = new class extends TestFixture {
  102. public function connection(): string
  103. {
  104. return 'test1';
  105. }
  106. protected function _schemaFromReflection(): void
  107. {
  108. }
  109. };
  110. $fixture2 = new class extends TestFixture {
  111. public function connection(): string
  112. {
  113. return 'test2';
  114. }
  115. protected function _schemaFromReflection(): void
  116. {
  117. }
  118. };
  119. ConnectionManager::alias('test', 'test1');
  120. ConnectionManager::alias('test', 'test2');
  121. $numCalls = 0;
  122. (new FixtureHelper())->runPerConnection(function () use (&$numCalls): void {
  123. ++$numCalls;
  124. }, [$fixture1, $fixture2]);
  125. $this->assertSame(2, $numCalls);
  126. }
  127. /**
  128. * Tests inserting fixtures.
  129. */
  130. public function testInsertFixtures(): void
  131. {
  132. /**
  133. * @var \Cake\Database\Connection $connection
  134. */
  135. $connection = ConnectionManager::get('test');
  136. $connection->deleteQuery()->delete('articles')->execute()->closeCursor();
  137. $rows = $connection->selectQuery()->select('*')->from('articles')->execute();
  138. $this->assertEmpty($rows->fetchAll());
  139. $rows->closeCursor();
  140. $helper = new FixtureHelper();
  141. $helper->insert($helper->loadFixtures(['core.Articles']));
  142. $rows = $connection->selectQuery()->select('*')->from('articles')->execute();
  143. $this->assertNotEmpty($rows->fetchAll());
  144. $rows->closeCursor();
  145. }
  146. /**
  147. * Tests handling PDO errors when inserting rows.
  148. */
  149. public function testInsertFixturesException(): void
  150. {
  151. $fixture = new class extends TestFixture {
  152. public function connection(): string
  153. {
  154. return 'test';
  155. }
  156. protected function _schemaFromReflection(): void
  157. {
  158. }
  159. public function insert(ConnectionInterface $connection): bool
  160. {
  161. throw new PDOException('Missing key');
  162. }
  163. };
  164. $helper = new class extends FixtureHelper {
  165. public function sortByConstraint(Connection $connection, array $fixtures): array
  166. {
  167. return [new class extends TestFixture {
  168. public function connection(): string
  169. {
  170. return 'test';
  171. }
  172. protected function _schemaFromReflection(): void
  173. {
  174. }
  175. public function insert(ConnectionInterface $connection): bool
  176. {
  177. throw new PDOException('Missing key');
  178. }
  179. }];
  180. }
  181. };
  182. $this->expectException(CakeException::class);
  183. $this->expectExceptionMessage('Unable to insert rows for table `');
  184. $helper->insert([$fixture]);
  185. }
  186. /**
  187. * Tests truncating fixtures.
  188. */
  189. public function testTruncateFixtures(): void
  190. {
  191. /**
  192. * @var \Cake\Database\Connection $connection
  193. */
  194. $connection = ConnectionManager::get('test');
  195. $rows = $connection->selectQuery()->select('*')->from('articles')->execute();
  196. $this->assertNotEmpty($rows->fetchAll());
  197. $rows->closeCursor();
  198. $helper = new FixtureHelper();
  199. $helper->truncate($helper->loadFixtures(['core.Articles']));
  200. $rows = $connection->selectQuery()->select('*')->from('articles')->execute();
  201. $this->assertEmpty($rows->fetchAll());
  202. $rows->closeCursor();
  203. }
  204. /**
  205. * Tests handling PDO errors when trucating rows.
  206. */
  207. public function testTruncateFixturesException(): void
  208. {
  209. $fixture = new class extends TestFixture {
  210. public function connection(): string
  211. {
  212. return 'test';
  213. }
  214. protected function _schemaFromReflection(): void
  215. {
  216. }
  217. public function truncate(ConnectionInterface $connection): bool
  218. {
  219. throw new PDOException('Missing key');
  220. }
  221. };
  222. $helper = new class extends FixtureHelper {
  223. public function sortByConstraint(Connection $connection, array $fixtures): array
  224. {
  225. return [new class extends TestFixture {
  226. public function connection(): string
  227. {
  228. return 'test';
  229. }
  230. protected function _schemaFromReflection(): void
  231. {
  232. }
  233. public function truncate(ConnectionInterface $connection): bool
  234. {
  235. throw new PDOException('Missing key');
  236. }
  237. }];
  238. }
  239. };
  240. $this->expectException(CakeException::class);
  241. $this->expectExceptionMessage('Unable to truncate table `');
  242. $helper->truncate([$fixture]);
  243. }
  244. }