Browse Source

Loading all fixtures if no args are passed to loadFixtures

Jeremy Harris 8 years ago
parent
commit
214fc0eec1

+ 7 - 0
src/TestSuite/TestCase.php

@@ -141,6 +141,13 @@ abstract class TestCase extends BaseTestCase
         foreach ($args as $class) {
             $this->fixtureManager->loadSingle($class, null, $this->dropTables);
         }
+
+        if (empty($args)) {
+            $autoFixtures = $this->autoFixtures;
+            $this->autoFixtures = true;
+            $this->fixtureManager->load($this);
+            $this->autoFixtures = $autoFixtures;
+        }
     }
 
     /**

+ 16 - 1
tests/Fixture/FixturizedTestCase.php

@@ -2,6 +2,7 @@
 namespace Cake\Test\Fixture;
 
 use Cake\TestSuite\TestCase;
+use Cake\ORM\TableRegistry;
 use Exception;
 
 /**
@@ -14,7 +15,7 @@ class FixturizedTestCase extends TestCase
      * Fixtures to use in this test
      * @var array
      */
-    public $fixtures = ['core.categories'];
+    public $fixtures = ['core.categories', 'core.articles'];
 
     /**
      * test that the shared fixture is correctly set
@@ -37,6 +38,20 @@ class FixturizedTestCase extends TestCase
     }
 
     /**
+     * test that calling loadFixtures without args loads all fixtures
+     *
+     * @return void
+     */
+    public function testLoadAllFixtures()
+    {
+        $this->loadFixtures();
+        $article = TableRegistry::get('Articles')->get(1);
+        $this->assertSame(1, $article->id);
+        $category = TableRegistry::get('Categories')->get(1);
+        $this->assertSame(1, $category->id);
+    }
+
+    /**
      * test that a test is marked as skipped using skipIf and its first parameter evaluates to true
      *
      * @return void

+ 21 - 0
tests/TestCase/TestSuite/TestCaseTest.php

@@ -22,6 +22,7 @@ use Cake\Event\EventManager;
 use Cake\ORM\Entity;
 use Cake\ORM\Table;
 use Cake\ORM\TableRegistry;
+use Cake\TestSuite\Fixture\FixtureManager;
 use Cake\TestSuite\TestCase;
 use Cake\Test\Fixture\FixturizedTestCase;
 
@@ -139,6 +140,26 @@ class TestCaseTest extends TestCase
     }
 
     /**
+     * tests loadFixtures loads all fixtures on the test
+     *
+     * @return void
+     */
+    public function testLoadAllFixtures()
+    {
+        $test = new FixturizedTestCase('testLoadAllFixtures');
+        $test->autoFixtures = false;
+        $manager = new FixtureManager();
+        $manager->fixturize($test);
+        $test->fixtureManager = $manager;
+
+        $result = $test->run();
+
+        $this->assertEquals(0, $result->errorCount());
+        $this->assertCount(1, $result->passed());
+        $this->assertFalse($test->autoFixtures);
+    }
+
+    /**
      * testSkipIf
      *
      * @return void