Skip to content

Commit

Permalink
Loading all fixtures if no args are passed to loadFixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyharris committed Jul 27, 2017
1 parent e158802 commit 214fc0e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/TestSuite/TestCase.php
Expand Up @@ -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;
}
}

/**
Expand Down
17 changes: 16 additions & 1 deletion tests/Fixture/FixturizedTestCase.php
Expand Up @@ -2,6 +2,7 @@
namespace Cake\Test\Fixture;

use Cake\TestSuite\TestCase;
use Cake\ORM\TableRegistry;
use Exception;

/**
Expand All @@ -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
Expand All @@ -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
*
Expand Down
21 changes: 21 additions & 0 deletions tests/TestCase/TestSuite/TestCaseTest.php
Expand Up @@ -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;

Expand Down Expand Up @@ -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
*
Expand Down

3 comments on commit 214fc0e

@voycey
Copy link
Contributor

@voycey voycey commented on 214fc0e Aug 2, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@jeremyharris
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Contributor

@voycey voycey commented on 214fc0e Aug 2, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.