FixtureManagerTest.php 7.6 KB

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