Browse Source

Merge pull request #3329 from cakephp/3.0-fixture-manager

Fix fixture manager not correctly loading plugin fixtures.
José Lorenzo Rodríguez 12 years ago
parent
commit
3bb0b7f7f6

+ 22 - 11
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)
  *
@@ -72,6 +70,15 @@ class FixtureManager {
 	}
 
 /**
+ * Get the loaded fixtures.
+ *
+ * @return array
+ */
+	public function loaded() {
+		return $this->_loaded;
+	}
+
+/**
  * Add aliaes for all non test prefixed connections.
  *
  * This allows models to use the test connections without
@@ -130,9 +137,9 @@ class FixtureManager {
 				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') {
@@ -140,23 +147,27 @@ class FixtureManager {
 			} 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".',

+ 72 - 0
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']
+		);
+	}
+
+}

+ 46 - 0
tests/test_app/Plugin/TestPlugin/Test/Fixture/ArticleFixture.php

@@ -0,0 +1,46 @@
+<?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 TestPlugin\Test\Fixture;
+
+use Cake\TestSuite\Fixture\TestFixture;
+
+/**
+ * Plugin article fixture.
+ */
+class ArticleFixture extends TestFixture {
+
+/**
+ * fields property
+ *
+ * @var array
+ */
+	public $fields = [
+		'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'],
+	];
+
+}