FixtureManagerTest.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestSuite;
  16. use Cake\Core\Plugin;
  17. use Cake\Database\Schema\Table;
  18. use Cake\Datasource\ConnectionManager;
  19. use Cake\Log\Log;
  20. use Cake\ORM\TableRegistry;
  21. use Cake\TestSuite\Fixture\FixtureManager;
  22. use Cake\TestSuite\Stub\ConsoleOutput;
  23. use Cake\TestSuite\TestCase;
  24. /**
  25. * Fixture manager test case.
  26. */
  27. class FixtureManagerTest extends TestCase
  28. {
  29. /**
  30. * Setup method
  31. *
  32. * @return void
  33. */
  34. public function setUp()
  35. {
  36. parent::setUp();
  37. $this->manager = new FixtureManager();
  38. }
  39. public function tearDown()
  40. {
  41. parent::tearDown();
  42. Log::reset();
  43. }
  44. /**
  45. * Test loading core fixtures.
  46. *
  47. * @return void
  48. */
  49. public function testFixturizeCore()
  50. {
  51. $test = $this->getMock('Cake\TestSuite\TestCase');
  52. $test->fixtures = ['core.articles'];
  53. $this->manager->fixturize($test);
  54. $fixtures = $this->manager->loaded();
  55. $this->assertCount(1, $fixtures);
  56. $this->assertArrayHasKey('core.articles', $fixtures);
  57. $this->assertInstanceOf('Cake\Test\Fixture\ArticlesFixture', $fixtures['core.articles']);
  58. }
  59. /**
  60. * Test logging depends on fixture manager debug.
  61. *
  62. * @return void
  63. */
  64. public function testLogSchemaWithDebug()
  65. {
  66. $db = ConnectionManager::get('test');
  67. $restore = $db->logQueries();
  68. $db->logQueries(true);
  69. $this->manager->setDebug(true);
  70. $buffer = new ConsoleOutput();
  71. Log::config('testQueryLogger', [
  72. 'className' => 'Console',
  73. 'stream' => $buffer
  74. ]);
  75. $test = $this->getMock('Cake\TestSuite\TestCase');
  76. $test->fixtures = ['core.articles'];
  77. $this->manager->fixturize($test);
  78. // Need to load/shutdown twice to ensure fixture is created.
  79. $this->manager->load($test);
  80. $this->manager->shutdown();
  81. $this->manager->load($test);
  82. $this->manager->shutdown();
  83. $db->logQueries($restore);
  84. $this->assertContains('CREATE TABLE', implode('', $buffer->messages()));
  85. }
  86. /**
  87. * Test that if a table already exists in the test database, it will dropped
  88. * before being recreated
  89. *
  90. * @return void
  91. */
  92. public function testResetDbIfTableExists()
  93. {
  94. $db = ConnectionManager::get('test');
  95. $restore = $db->logQueries();
  96. $db->logQueries(true);
  97. $this->manager->setDebug(true);
  98. $buffer = new ConsoleOutput();
  99. Log::config('testQueryLogger', [
  100. 'className' => 'Console',
  101. 'stream' => $buffer
  102. ]);
  103. $table = new Table('articles', [
  104. 'id' => ['type' => 'integer', 'unsigned' => true],
  105. 'title' => ['type' => 'string', 'length' => 255],
  106. ]);
  107. $table->addConstraint('primary', ['type' => 'primary', 'columns' => ['id']]);
  108. $sql = $table->createSql($db);
  109. foreach ($sql as $stmt) {
  110. $db->execute($stmt);
  111. }
  112. $test = $this->getMock('Cake\TestSuite\TestCase');
  113. $test->fixtures = ['core.articles'];
  114. $this->manager->fixturize($test);
  115. $this->manager->load($test);
  116. $db->logQueries($restore);
  117. $this->assertContains('DROP TABLE', implode('', $buffer->messages()));
  118. }
  119. /**
  120. * Test loading fixtures with constraints.
  121. *
  122. * @return void
  123. */
  124. public function testFixturizeCoreConstraint()
  125. {
  126. $test = $this->getMock('Cake\TestSuite\TestCase');
  127. $test->fixtures = ['core.articles', 'core.articles_tags', 'core.tags'];
  128. $this->manager->fixturize($test);
  129. $this->manager->load($test);
  130. $table = TableRegistry::get('ArticlesTags');
  131. $schema = $table->schema();
  132. $expectedConstraint = [
  133. 'type' => 'foreign',
  134. 'columns' => [
  135. 'tag_id'
  136. ],
  137. 'references' => [
  138. 'tags',
  139. 'id'
  140. ],
  141. 'update' => 'cascade',
  142. 'delete' => 'cascade',
  143. 'length' => []
  144. ];
  145. $this->assertEquals($expectedConstraint, $schema->constraint('tag_id_fk'));
  146. $this->manager->unload($test);
  147. $this->manager->load($test);
  148. $table = TableRegistry::get('ArticlesTags');
  149. $schema = $table->schema();
  150. $expectedConstraint = [
  151. 'type' => 'foreign',
  152. 'columns' => [
  153. 'tag_id'
  154. ],
  155. 'references' => [
  156. 'tags',
  157. 'id'
  158. ],
  159. 'update' => 'cascade',
  160. 'delete' => 'cascade',
  161. 'length' => []
  162. ];
  163. $this->assertEquals($expectedConstraint, $schema->constraint('tag_id_fk'));
  164. $this->manager->unload($test);
  165. }
  166. /**
  167. * Test loading app fixtures.
  168. *
  169. * @return void
  170. */
  171. public function testFixturizePlugin()
  172. {
  173. Plugin::load('TestPlugin');
  174. $test = $this->getMock('Cake\TestSuite\TestCase');
  175. $test->fixtures = ['plugin.test_plugin.articles'];
  176. $this->manager->fixturize($test);
  177. $fixtures = $this->manager->loaded();
  178. $this->assertCount(1, $fixtures);
  179. $this->assertArrayHasKey('plugin.test_plugin.articles', $fixtures);
  180. $this->assertInstanceOf(
  181. 'TestPlugin\Test\Fixture\ArticlesFixture',
  182. $fixtures['plugin.test_plugin.articles']
  183. );
  184. }
  185. /**
  186. * Test loading app fixtures.
  187. *
  188. * @return void
  189. */
  190. public function testFixturizeCustom()
  191. {
  192. $test = $this->getMock('Cake\TestSuite\TestCase');
  193. $test->fixtures = ['plugin.Company/TestPluginThree.articles'];
  194. $this->manager->fixturize($test);
  195. $fixtures = $this->manager->loaded();
  196. $this->assertCount(1, $fixtures);
  197. $this->assertArrayHasKey('plugin.Company/TestPluginThree.articles', $fixtures);
  198. $this->assertInstanceOf(
  199. 'Company\TestPluginThree\Test\Fixture\ArticlesFixture',
  200. $fixtures['plugin.Company/TestPluginThree.articles']
  201. );
  202. }
  203. /**
  204. * Test that unknown types are handled gracefully.
  205. *
  206. * @expectedException \UnexpectedValueException
  207. * @expectedExceptionMessage Referenced fixture class "Test\Fixture\Derp.derpFixture" not found. Fixture "derp.derp" was referenced
  208. */
  209. public function testFixturizeInvalidType()
  210. {
  211. $test = $this->getMock('Cake\TestSuite\TestCase');
  212. $test->fixtures = ['derp.derp'];
  213. $this->manager->fixturize($test);
  214. }
  215. /**
  216. * Test loading fixtures using loadSingle()
  217. *
  218. * @return void
  219. */
  220. public function testLoadSingle()
  221. {
  222. $test = $this->getMock('Cake\TestSuite\TestCase');
  223. $test->autoFixtures = false;
  224. $test->fixtures = ['core.articles', 'core.articles_tags', 'core.tags'];
  225. $this->manager->fixturize($test);
  226. $this->manager->loadSingle('Articles');
  227. $this->manager->loadSingle('Tags');
  228. $this->manager->loadSingle('ArticlesTags');
  229. $table = TableRegistry::get('ArticlesTags');
  230. $results = $table->find('all')->toArray();
  231. $schema = $table->schema();
  232. $expectedConstraint = [
  233. 'type' => 'foreign',
  234. 'columns' => [
  235. 'tag_id'
  236. ],
  237. 'references' => [
  238. 'tags',
  239. 'id'
  240. ],
  241. 'update' => 'cascade',
  242. 'delete' => 'cascade',
  243. 'length' => []
  244. ];
  245. $this->assertEquals($expectedConstraint, $schema->constraint('tag_id_fk'));
  246. $this->assertCount(4, $results);
  247. $this->manager->unload($test);
  248. $this->manager->loadSingle('Articles');
  249. $this->manager->loadSingle('Tags');
  250. $this->manager->loadSingle('ArticlesTags');
  251. $table = TableRegistry::get('ArticlesTags');
  252. $results = $table->find('all')->toArray();
  253. $schema = $table->schema();
  254. $expectedConstraint = [
  255. 'type' => 'foreign',
  256. 'columns' => [
  257. 'tag_id'
  258. ],
  259. 'references' => [
  260. 'tags',
  261. 'id'
  262. ],
  263. 'update' => 'cascade',
  264. 'delete' => 'cascade',
  265. 'length' => []
  266. ];
  267. $this->assertEquals($expectedConstraint, $schema->constraint('tag_id_fk'));
  268. $this->assertCount(4, $results);
  269. $this->manager->unload($test);
  270. }
  271. }