FixtureManagerTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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\Datasource\ConnectionManager;
  18. use Cake\Log\Log;
  19. use Cake\ORM\TableRegistry;
  20. use Cake\TestSuite\Fixture\FixtureManager;
  21. use Cake\TestSuite\Stub\ConsoleOutput;
  22. use Cake\TestSuite\TestCase;
  23. /**
  24. * Fixture manager test case.
  25. */
  26. class FixtureManagerTest extends TestCase
  27. {
  28. /**
  29. * Setup method
  30. *
  31. * @return void
  32. */
  33. public function setUp()
  34. {
  35. parent::setUp();
  36. $this->manager = new FixtureManager();
  37. }
  38. public function tearDown()
  39. {
  40. parent::tearDown();
  41. Log::reset();
  42. }
  43. /**
  44. * Test loading core fixtures.
  45. *
  46. * @return void
  47. */
  48. public function testFixturizeCore()
  49. {
  50. $test = $this->getMock('Cake\TestSuite\TestCase');
  51. $test->fixtures = ['core.articles'];
  52. $this->manager->fixturize($test);
  53. $fixtures = $this->manager->loaded();
  54. $this->assertCount(1, $fixtures);
  55. $this->assertArrayHasKey('core.articles', $fixtures);
  56. $this->assertInstanceOf('Cake\Test\Fixture\ArticlesFixture', $fixtures['core.articles']);
  57. }
  58. /**
  59. * Test logging depends on fixture manager debug.
  60. *
  61. * @return void
  62. */
  63. public function testLogSchemaWithDebug()
  64. {
  65. $db = ConnectionManager::get('test');
  66. $restore = $db->logQueries();
  67. $db->logQueries(true);
  68. $this->manager->setDebug(true);
  69. $buffer = new ConsoleOutput();
  70. Log::config('testQueryLogger', [
  71. 'className' => 'Console',
  72. 'stream' => $buffer
  73. ]);
  74. $test = $this->getMock('Cake\TestSuite\TestCase');
  75. $test->fixtures = ['core.articles'];
  76. $this->manager->fixturize($test);
  77. // Need to load/shutdown twice to ensure fixture is created.
  78. $this->manager->load($test);
  79. $this->manager->shutdown();
  80. $this->manager->load($test);
  81. $this->manager->shutdown();
  82. $db->logQueries($restore);
  83. $this->assertContains('CREATE TABLE', implode('', $buffer->messages()));
  84. }
  85. /**
  86. * Test loading fixtures with constraints.
  87. *
  88. * @return void
  89. */
  90. public function testFixturizeCoreConstraint()
  91. {
  92. $test = $this->getMock('Cake\TestSuite\TestCase');
  93. $test->fixtures = ['core.articles', 'core.articles_tags', 'core.tags'];
  94. $this->manager->fixturize($test);
  95. $this->manager->load($test);
  96. $table = TableRegistry::get('ArticlesTags');
  97. $schema = $table->schema();
  98. $expectedConstraint = [
  99. 'type' => 'foreign',
  100. 'columns' => [
  101. 'tag_id'
  102. ],
  103. 'references' => [
  104. 'tags',
  105. 'id'
  106. ],
  107. 'update' => 'cascade',
  108. 'delete' => 'cascade',
  109. 'length' => []
  110. ];
  111. $this->assertEquals($expectedConstraint, $schema->constraint('tag_id_fk'));
  112. $this->manager->unload($test);
  113. $this->manager->load($test);
  114. $table = TableRegistry::get('ArticlesTags');
  115. $schema = $table->schema();
  116. $expectedConstraint = [
  117. 'type' => 'foreign',
  118. 'columns' => [
  119. 'tag_id'
  120. ],
  121. 'references' => [
  122. 'tags',
  123. 'id'
  124. ],
  125. 'update' => 'cascade',
  126. 'delete' => 'cascade',
  127. 'length' => []
  128. ];
  129. $this->assertEquals($expectedConstraint, $schema->constraint('tag_id_fk'));
  130. $this->manager->unload($test);
  131. }
  132. /**
  133. * Test loading app fixtures.
  134. *
  135. * @return void
  136. */
  137. public function testFixturizePlugin()
  138. {
  139. Plugin::load('TestPlugin');
  140. $test = $this->getMock('Cake\TestSuite\TestCase');
  141. $test->fixtures = ['plugin.test_plugin.articles'];
  142. $this->manager->fixturize($test);
  143. $fixtures = $this->manager->loaded();
  144. $this->assertCount(1, $fixtures);
  145. $this->assertArrayHasKey('plugin.test_plugin.articles', $fixtures);
  146. $this->assertInstanceOf(
  147. 'TestPlugin\Test\Fixture\ArticlesFixture',
  148. $fixtures['plugin.test_plugin.articles']
  149. );
  150. }
  151. /**
  152. * Test loading app fixtures.
  153. *
  154. * @return void
  155. */
  156. public function testFixturizeCustom()
  157. {
  158. $test = $this->getMock('Cake\TestSuite\TestCase');
  159. $test->fixtures = ['plugin.Company/TestPluginThree.articles'];
  160. $this->manager->fixturize($test);
  161. $fixtures = $this->manager->loaded();
  162. $this->assertCount(1, $fixtures);
  163. $this->assertArrayHasKey('plugin.Company/TestPluginThree.articles', $fixtures);
  164. $this->assertInstanceOf(
  165. 'Company\TestPluginThree\Test\Fixture\ArticlesFixture',
  166. $fixtures['plugin.Company/TestPluginThree.articles']
  167. );
  168. }
  169. /**
  170. * Test that unknown types are handled gracefully.
  171. *
  172. * @expectedException \UnexpectedValueException
  173. * @expectedExceptionMessage Referenced fixture class "Test\Fixture\Derp.derpFixture" not found. Fixture "derp.derp" was referenced
  174. */
  175. public function testFixturizeInvalidType()
  176. {
  177. $test = $this->getMock('Cake\TestSuite\TestCase');
  178. $test->fixtures = ['derp.derp'];
  179. $this->manager->fixturize($test);
  180. }
  181. /**
  182. * Test loading fixtures using loadSingle()
  183. *
  184. * @return void
  185. */
  186. public function testLoadSingle()
  187. {
  188. $test = $this->getMock('Cake\TestSuite\TestCase');
  189. $test->autoFixtures = false;
  190. $test->fixtures = ['core.articles', 'core.articles_tags', 'core.tags'];
  191. $this->manager->fixturize($test);
  192. $this->manager->loadSingle('Articles');
  193. $this->manager->loadSingle('Tags');
  194. $this->manager->loadSingle('ArticlesTags');
  195. $table = TableRegistry::get('ArticlesTags');
  196. $results = $table->find('all')->toArray();
  197. $schema = $table->schema();
  198. $expectedConstraint = [
  199. 'type' => 'foreign',
  200. 'columns' => [
  201. 'tag_id'
  202. ],
  203. 'references' => [
  204. 'tags',
  205. 'id'
  206. ],
  207. 'update' => 'cascade',
  208. 'delete' => 'cascade',
  209. 'length' => []
  210. ];
  211. $this->assertEquals($expectedConstraint, $schema->constraint('tag_id_fk'));
  212. $this->assertCount(4, $results);
  213. $this->manager->unload($test);
  214. $this->manager->loadSingle('Articles');
  215. $this->manager->loadSingle('Tags');
  216. $this->manager->loadSingle('ArticlesTags');
  217. $table = TableRegistry::get('ArticlesTags');
  218. $results = $table->find('all')->toArray();
  219. $schema = $table->schema();
  220. $expectedConstraint = [
  221. 'type' => 'foreign',
  222. 'columns' => [
  223. 'tag_id'
  224. ],
  225. 'references' => [
  226. 'tags',
  227. 'id'
  228. ],
  229. 'update' => 'cascade',
  230. 'delete' => 'cascade',
  231. 'length' => []
  232. ];
  233. $this->assertEquals($expectedConstraint, $schema->constraint('tag_id_fk'));
  234. $this->assertCount(4, $results);
  235. $this->manager->unload($test);
  236. }
  237. }