Skip to content
Permalink
Browse files

Loading all fixtures if no args are passed to loadFixtures

  • Loading branch information...
jeremyharris committed Jul 27, 2017
1 parent e158802 commit 214fc0eec1b210e7c6658b63c61830bed66fb06c
Showing with 44 additions and 1 deletion.
  1. +7 −0 src/TestSuite/TestCase.php
  2. +16 −1 tests/Fixture/FixturizedTestCase.php
  3. +21 −0 tests/TestCase/TestSuite/TestCaseTest.php
@@ -141,6 +141,13 @@ public function loadFixtures()
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;
}
}
/**
@@ -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
@@ -36,6 +37,20 @@ public function testFixtureLoadOnDemand()
$this->loadFixtures('Categories');
}
/**
* 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
*
@@ -22,6 +22,7 @@
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;
@@ -138,6 +139,26 @@ public function testLoadFixturesOnDemand()
$this->assertEquals(0, $result->errorCount());
}
/**
* 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
*

3 comments on commit 214fc0e

@voycey

This comment has been minimized.

Copy link
Contributor

voycey replied Aug 2, 2017

So this will mean we dont have to specify each fixture in each test case? We can load them all at once?

@jeremyharris

This comment has been minimized.

Copy link
Member Author

jeremyharris replied Aug 2, 2017

You still need to specify all of the fixtures (usually create a base test case that extends Cake's to put them in). This change allows you to load all fixtures when autoFixtures is false, e.g., in a single test method. You can hit me up in slack if you have questions.

@voycey

This comment has been minimized.

Copy link
Contributor

voycey replied Aug 2, 2017

Yeah no worries, this is what I am currently doing, was just hoping to save a few lines of code and lots of class extensions :)

Please sign in to comment.
You can’t perform that action at this time.