FixtureManagerTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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\ConnectionManager;
  18. use Cake\ORM\TableRegistry;
  19. use Cake\TestSuite\Fixture\FixtureManager;
  20. use Cake\TestSuite\TestCase;
  21. /**
  22. * Fixture manager test case.
  23. */
  24. class FixtureManagerTest extends TestCase
  25. {
  26. /**
  27. * Setup method
  28. *
  29. * @return void
  30. */
  31. public function setUp()
  32. {
  33. parent::setUp();
  34. $this->manager = new FixtureManager();
  35. }
  36. /**
  37. * Test loading core fixtures.
  38. *
  39. * @return void
  40. */
  41. public function testFixturizeCore()
  42. {
  43. $test = $this->getMock('Cake\TestSuite\TestCase');
  44. $test->fixtures = ['core.articles'];
  45. $this->manager->fixturize($test);
  46. $fixtures = $this->manager->loaded();
  47. $this->assertCount(1, $fixtures);
  48. $this->assertArrayHasKey('core.articles', $fixtures);
  49. $this->assertInstanceOf('Cake\Test\Fixture\ArticlesFixture', $fixtures['core.articles']);
  50. }
  51. /**
  52. * Test loading fixtures with constraints.
  53. *
  54. * @return void
  55. */
  56. public function testFixturizeCoreConstraint()
  57. {
  58. $test = $this->getMock('Cake\TestSuite\TestCase');
  59. $test->fixtures = ['core.articles', 'core.articles_tags', 'core.tags'];
  60. $this->manager->fixturize($test);
  61. $this->manager->load($test);
  62. $table = TableRegistry::get('ArticlesTags');
  63. $schema = $table->schema();
  64. $this->assertEquals(['primary', 'tag_id_fk'], $schema->constraints());
  65. $expectedConstraint = [
  66. 'type' => 'foreign',
  67. 'columns' => [
  68. 'tag_id'
  69. ],
  70. 'references' => [
  71. 'tags',
  72. 'id'
  73. ],
  74. 'update' => 'cascade',
  75. 'delete' => 'cascade',
  76. 'length' => []
  77. ];
  78. $this->assertEquals($expectedConstraint, $schema->constraint('tag_id_fk'));
  79. $this->manager->unload($test);
  80. $this->manager->load($test);
  81. $table = TableRegistry::get('ArticlesTags');
  82. $schema = $table->schema();
  83. $this->assertEquals(['primary', 'tag_id_fk'], $schema->constraints());
  84. $expectedConstraint = [
  85. 'type' => 'foreign',
  86. 'columns' => [
  87. 'tag_id'
  88. ],
  89. 'references' => [
  90. 'tags',
  91. 'id'
  92. ],
  93. 'update' => 'cascade',
  94. 'delete' => 'cascade',
  95. 'length' => []
  96. ];
  97. $this->assertEquals($expectedConstraint, $schema->constraint('tag_id_fk'));
  98. $this->manager->unload($test);
  99. }
  100. /**
  101. * Test loading app fixtures.
  102. *
  103. * @return void
  104. */
  105. public function testFixturizePlugin()
  106. {
  107. Plugin::load('TestPlugin');
  108. $test = $this->getMock('Cake\TestSuite\TestCase');
  109. $test->fixtures = ['plugin.test_plugin.articles'];
  110. $this->manager->fixturize($test);
  111. $fixtures = $this->manager->loaded();
  112. $this->assertCount(1, $fixtures);
  113. $this->assertArrayHasKey('plugin.test_plugin.articles', $fixtures);
  114. $this->assertInstanceOf(
  115. 'TestPlugin\Test\Fixture\ArticlesFixture',
  116. $fixtures['plugin.test_plugin.articles']
  117. );
  118. }
  119. /**
  120. * Test loading app fixtures.
  121. *
  122. * @return void
  123. */
  124. public function testFixturizeCustom()
  125. {
  126. $test = $this->getMock('Cake\TestSuite\TestCase');
  127. $test->fixtures = ['plugin.Company/TestPluginThree.articles'];
  128. $this->manager->fixturize($test);
  129. $fixtures = $this->manager->loaded();
  130. $this->assertCount(1, $fixtures);
  131. $this->assertArrayHasKey('plugin.Company/TestPluginThree.articles', $fixtures);
  132. $this->assertInstanceOf(
  133. 'Company\TestPluginThree\Test\Fixture\ArticlesFixture',
  134. $fixtures['plugin.Company/TestPluginThree.articles']
  135. );
  136. }
  137. /**
  138. * Test that unknown types are handled gracefully.
  139. *
  140. * @expectedException \UnexpectedValueException
  141. * @expectedExceptionMessage Referenced fixture class "Test\Fixture\Derp.derpFixture" not found. Fixture "derp.derp" was referenced
  142. */
  143. public function testFixturizeInvalidType()
  144. {
  145. $test = $this->getMock('Cake\TestSuite\TestCase');
  146. $test->fixtures = ['derp.derp'];
  147. $this->manager->fixturize($test);
  148. }
  149. }