Skip to content

Commit

Permalink
Fix fixture manager not correctly loading plugin fixtures.
Browse files Browse the repository at this point in the history
Add test cases for FixtureManager as they were absent.

Refs #3318
  • Loading branch information
markstory committed Apr 16, 2014
1 parent 3838e15 commit aff936b
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 11 deletions.
33 changes: 22 additions & 11 deletions src/TestSuite/Fixture/FixtureManager.php
@@ -1,7 +1,5 @@
<?php
/**
* A factory class to manage the life cycle of test fixtures
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
Expand Down Expand Up @@ -71,6 +69,15 @@ public function fixturize($test) {
$this->_processed[get_class($test)] = true;
}

/**
* Get the loaded fixtures.
*
* @return array
*/
public function loaded() {
return $this->_loaded;
}

/**
* Add aliaes for all non test prefixed connections.
*
Expand Down Expand Up @@ -130,33 +137,37 @@ protected function _loadFixtures($test) {
continue;
}

list($type, $name) = explode('.', $fixture, 2);
$path = explode('/', $name);
$base = array_pop($path);
list($type, $pathName) = explode('.', $fixture, 2);
$path = explode('/', $pathName);
$name = array_pop($path);
$additionalPath = implode('\\', $path);

if ($type === 'core') {
$baseNamespace = 'Cake';
} elseif ($type === 'app') {
$baseNamespace = Configure::read('App.namespace');
} elseif ($type === 'plugin') {
list($plugin, $additionalPath) = explode('.', $additionalPath);
$baseNamespace = Plugin::getNamespace($plugin);
if (strlen($additionalPath)) {
list($plugin, $additionalPath) = explode('.', $additionalPath);
} else {
list($plugin, $name) = explode('.', $name);
}
$baseNamespace = Plugin::getNamespace(Inflector::camelize($plugin));
} else {
$base = $fixture;
$name = $fixture;
}
$base = Inflector::camelize($base);
$name = Inflector::camelize($name);
$nameSegments = [
$baseNamespace,
'Test\Fixture',
$additionalPath,
$base . 'Fixture'
$name . 'Fixture'
];
$className = implode('\\', array_filter($nameSegments));

if (class_exists($className)) {
$this->_loaded[$fixture] = new $className();
$this->_fixtureMap[$base] = $this->_loaded[$fixture];
$this->_fixtureMap[$name] = $this->_loaded[$fixture];
} else {
$msg = sprintf(
'Referenced fixture class "%s" not found. Fixture "%s" was referenced in test case "%s".',
Expand Down
72 changes: 72 additions & 0 deletions tests/TestCase/TestSuite/FixtureManagerTest.php
@@ -0,0 +1,72 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Test\TestSuite;

use Cake\Core\Plugin;
use Cake\Database\ConnectionManager;
use Cake\TestSuite\Fixture\FixtureManager;
use Cake\TestSuite\TestCase;

/**
* Fixture manager test case.
*/
class FixtureManagerTest extends TestCase {

/**
* Setup method
*
* @return void
*/
public function setUp() {
parent::setUp();
$this->manager = new FixtureManager();
}

/**
* Test loading core fixtures.
*
* @return void
*/
public function testFixturizeCore() {
$test = $this->getMock('Cake\TestSuite\TestCase');
$test->fixtures = ['core.article'];
$this->manager->fixturize($test);
$fixtures = $this->manager->loaded();
$this->assertCount(1, $fixtures);
$this->assertArrayHasKey('core.article', $fixtures);
$this->assertInstanceOf('Cake\Test\Fixture\ArticleFixture', $fixtures['core.article']);
}

/**
* Test loading app fixtures.
*
* @return void
*/
public function testFixturizePlugin() {
Plugin::load('TestPlugin');

$test = $this->getMock('Cake\TestSuite\TestCase');
$test->fixtures = ['plugin.test_plugin.article'];
$this->manager->fixturize($test);
$fixtures = $this->manager->loaded();
$this->assertCount(1, $fixtures);
$this->assertArrayHasKey('plugin.test_plugin.article', $fixtures);
$this->assertInstanceOf(
'TestPlugin\Test\Fixture\ArticleFixture',
$fixtures['plugin.test_plugin.article']
);
}

}
45 changes: 45 additions & 0 deletions tests/test_app/Plugin/TestPlugin/Test/Fixture/ArticleFixture.php
@@ -0,0 +1,45 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 2.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace TestPlugin\Test\Fixture;

use Cake\TestSuite\Fixture\TestFixture;

/**
* Plugin article fixture.
*/
class ArticleFixture extends TestFixture {
/**
* fields property
*
* @var array
*/
public $fields = array(
'id' => ['type' => 'integer'],
'author_id' => ['type' => 'integer', 'null' => true],
'title' => ['type' => 'string', 'null' => true],
'body' => 'text',
'_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
);

/**
* records property
*
* @var array
*/
public $records = [
['author_id' => 1, 'title' => 'Plugin Article', 'body' => 'Plugin Article Body'],
];

}

0 comments on commit aff936b

Please sign in to comment.