Skip to content

Commit

Permalink
Updated TestSuite folder
Browse files Browse the repository at this point in the history
  • Loading branch information
jrbasso committed May 25, 2012
1 parent 6accc54 commit 0e1f0e4
Show file tree
Hide file tree
Showing 17 changed files with 213 additions and 216 deletions.
76 changes: 37 additions & 39 deletions lib/Cake/TestSuite/ControllerTestCase.php
Expand Up @@ -16,14 +16,15 @@
* @since CakePHP(tm) v 2.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/

App::uses('Dispatcher', 'Routing');
App::uses('CakeTestCase', 'TestSuite');
App::uses('Router', 'Routing');
App::uses('CakeRequest', 'Network');
App::uses('CakeResponse', 'Network');
App::uses('Helper', 'View');
App::uses('CakeEvent', 'Event');
namespace Cake\TestSuite;
use Cake\Routing\Dispatcher,
Cake\Routing\Router,
Cake\View\Helper,
Cake\Event\Event,
Cake\Utility\Inflector,
Cake\Utility\ClassRegistry,
Cake\Core\App,
Cake\Error;

/**
* ControllerTestDispatcher class
Expand Down Expand Up @@ -55,7 +56,8 @@ protected function _getController($request, $response) {
if ($this->testController === null) {
$this->testController = parent::_getController($request, $response);
}
$this->testController->helpers = array_merge(array('InterceptContent'), $this->testController->helpers);
$default = array('InterceptContent' => array('className' => 'Cake\TestSuite\InterceptContentHelper'));
$this->testController->helpers = array_merge($default, $this->testController->helpers);
$this->testController->setRequest($request);
$this->testController->response = $this->response;
foreach ($this->testController->Components->attached() as $component) {
Expand Down Expand Up @@ -108,7 +110,7 @@ public function afterRender($viewFile) {
*
* @package Cake.TestSuite
*/
abstract class ControllerTestCase extends CakeTestCase {
abstract class ControllerTestCase extends TestCase {

/**
* The controller to test in testAction
Expand Down Expand Up @@ -230,7 +232,7 @@ protected function _testAction($url = '', $options = array()) {
$_GET = array();
}
}
$request = $this->getMock('CakeRequest', array('_readInput'), array($url));
$request = $this->getMock('Cake\Network\Request', array('_readInput'), array($url));

if (is_string($options['data'])) {
$request->expects($this->any())
Expand All @@ -241,11 +243,11 @@ protected function _testAction($url = '', $options = array()) {
$Dispatch = new ControllerTestDispatcher();
foreach (Router::$routes as $route) {
if ($route instanceof RedirectRoute) {
$route->response = $this->getMock('CakeResponse', array('send'));
$route->response = $this->getMock('Cake\Network\Response', array('send'));
}
}
$Dispatch->loadRoutes = $this->loadRoutes;
$Dispatch->parseParams(new CakeEvent('ControllerTestCase', $Dispatch, array('request' => $request)));
$Dispatch->parseParams(new Event('ControllerTestCase', $Dispatch, array('request' => $request)));
if (!isset($request->params['controller'])) {
$this->headers = Router::currentRoute()->response->header();
return;
Expand All @@ -265,7 +267,7 @@ protected function _testAction($url = '', $options = array()) {
$params['requested'] = 1;
}
$Dispatch->testController = $this->controller;
$Dispatch->response = $this->getMock('CakeResponse', array('send'));
$Dispatch->response = $this->getMock('Cake\Network\Response', array('send'));
$this->result = $Dispatch->dispatch($request, $Dispatch->response, $params);
$this->controller = $Dispatch->testController;
$this->vars = $this->controller->viewVars;
Expand Down Expand Up @@ -304,16 +306,12 @@ protected function _testAction($url = '', $options = array()) {
* @throws MissingComponentException When components could not be created.
*/
public function generate($controller, $mocks = array()) {
list($plugin, $controller) = pluginSplit($controller);
if ($plugin) {
App::uses($plugin . 'AppController', $plugin . '.Controller');
$plugin .= '.';
}
App::uses($controller . 'Controller', $plugin . 'Controller');
if (!class_exists($controller . 'Controller')) {
throw new MissingControllerException(array(
$classname = App::classname($controller, 'Controller', 'Controller');
if (!$classname) {
list($plugin, $controller) = pluginSplit($controller);
throw new Error\MissingControllerException(array(
'class' => $controller . 'Controller',
'plugin' => substr($plugin, 0, -1)
'plugin' => $plugin
));
}
ClassRegistry::flush();
Expand All @@ -324,11 +322,11 @@ public function generate($controller, $mocks = array()) {
'components' => array()
), (array)$mocks);

list($plugin, $name) = pluginSplit($controller);
$_controller = $this->getMock($name . 'Controller', $mocks['methods'], array(), '', false);
$_controller->name = $name;
$request = $this->getMock('CakeRequest');
$response = $this->getMock('CakeResponse', array('_sendHeader'));
$_controller = $this->getMock($classname, $mocks['methods'], array(), '', false);
list(, $controllerName) = namespaceSplit($classname);
$_controller->name = substr($controllerName, 0, -10);
$request = $this->getMock('Cake\Network\Request');
$response = $this->getMock('Cake\Network\Response', array('_sendHeader'));
$_controller->__construct($request, $response);

$config = ClassRegistry::config('Model');
Expand All @@ -340,12 +338,13 @@ public function generate($controller, $mocks = array()) {
if ($methods === true) {
$methods = array();
}
ClassRegistry::init($model);
list($plugin, $name) = pluginSplit($model);
;
$modelClass = get_class(ClassRegistry::init($model));
list(, $modelName) = namespaceSplit($modelClass);
$config = array_merge((array)$config, array('name' => $model));
$_model = $this->getMock($name, $methods, array($config));
ClassRegistry::removeObject($name);
ClassRegistry::addObject($name, $_model);
$_model = $this->getMock($modelClass, $methods, array($config));
ClassRegistry::removeObject($modelName);
ClassRegistry::addObject($modelName, $_model);
}

foreach ($mocks['components'] as $component => $methods) {
Expand All @@ -356,12 +355,11 @@ public function generate($controller, $mocks = array()) {
if ($methods === true) {
$methods = array();
}
list($plugin, $name) = pluginSplit($component, true);
$componentClass = $name . 'Component';
App::uses($componentClass, $plugin . 'Controller/Component');
if (!class_exists($componentClass)) {
throw new MissingComponentException(array(
'class' => $componentClass
$componentClass = App::classname($component, 'Controller/Component', 'Component');
list(, $name) = pluginSplit($component, true);
if (!$componentClass) {
throw new Error\MissingComponentException(array(
'class' => $name . 'Component'
));
}
$_component = $this->getMock($componentClass, $methods, array(), '', false);
Expand Down
12 changes: 8 additions & 4 deletions lib/Cake/TestSuite/Coverage/BaseCoverageReport.php
Expand Up @@ -17,6 +17,10 @@
* @since CakePHP(tm) v 2.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\TestSuite\Coverage;
use Cake\TestSuite\Reporter\BaseReporter,
Cake\Core\App,
Cake\Utility\Inflector;

/**
* Abstract class for common CoverageReport methods.
Expand Down Expand Up @@ -59,21 +63,21 @@ abstract class BaseCoverageReport {
* Constructor
*
* @param array $coverage Array of coverage data from PHPUnit_Test_Result
* @param CakeBaseReporter $reporter A reporter to use for the coverage report.
* @param Cake\TestSuite\Reporter\BaseReporter $reporter A reporter to use for the coverage report.
* @return void
*/
public function __construct($coverage, CakeBaseReporter $reporter) {
public function __construct($coverage, BaseReporter $reporter) {
$this->_rawCoverage = $coverage;
$this->_setParams($reporter);
}

/**
* Pulls params out of the reporter.
*
* @param CakeBaseReporter $reporter Reporter to suck params out of.
* @param Cake\TestSuite\Reporter\BaseReporter $reporter Reporter to suck params out of.
* @return void
*/
protected function _setParams(CakeBaseReporter $reporter) {
protected function setParams(BaseReporter $reporter) {
if ($reporter->params['app']) {
$this->appTest = true;
}
Expand Down
5 changes: 2 additions & 3 deletions lib/Cake/TestSuite/Coverage/HtmlCoverageReport.php
Expand Up @@ -16,8 +16,7 @@
* @since CakePHP(tm) v 2.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/

App::uses('BaseCoverageReport', 'TestSuite/Coverage');
namespace Cake\TestSuite\Coverage;

/**
* Generates code coverage reports in HTML from data obtained from PHPUnit
Expand Down Expand Up @@ -80,7 +79,7 @@ public function generateDiff($filename, $fileLines, $coverageData) {
$coveringTests = array();
foreach ($coverageData[$lineno] as $test) {
$class = (is_array($test) && isset($test['id'])) ? $test['id'] : $test;
$testReflection = new ReflectionClass(current(explode('::', $class)));
$testReflection = new \ReflectionClass(current(explode('::', $class)));
$this->_testNames[] = $this->_guessSubjectName($testReflection);
$coveringTests[] = $class;
}
Expand Down
3 changes: 1 addition & 2 deletions lib/Cake/TestSuite/Coverage/TextCoverageReport.php
Expand Up @@ -16,8 +16,7 @@
* @since CakePHP(tm) v 2.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/

App::uses('BaseCoverageReport', 'TestSuite/Coverage');
namespace Cake\TestSuite\Coverage;

/**
* Generates code coverage reports in Simple plain text from data obtained from PHPUnit
Expand Down
Expand Up @@ -16,16 +16,21 @@
* @since CakePHP(tm) v 2.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/

App::uses('ConnectionManager', 'Model');
App::uses('ClassRegistry', 'Utility');
namespace Cake\TestSuite\Fixture;
use Cake\Model\ConnectionManager,
Cake\Core\Plugin,
Cake\Core\Configure,
Cake\Core\App,
Cake\TestSuite\TestCase,
Cake\Utility\ClassRegistry,
Cake\Utility\Inflector;

/**
* A factory class to manage the life cycle of test fixtures
*
* @package Cake.TestSuite.Fixture
*/
class CakeFixtureManager {
class FixtureManager {

/**
* Was this class already initialized?
Expand Down Expand Up @@ -58,7 +63,7 @@ class CakeFixtureManager {
/**
* Inspects the test to look for unloaded fixtures and loads them
*
* @param CakeTestCase $test the test case to inspect
* @param Cake\TestSuite\TestCase $test the test case to inspect
* @return void
*/
public function fixturize($test) {
Expand Down Expand Up @@ -111,46 +116,31 @@ protected function _loadFixtures($fixtures) {
}

if (strpos($fixture, 'core.') === 0) {
$fixture = substr($fixture, strlen('core.'));
$fixturePaths[] = CAKE . 'Test' . DS . 'Fixture';
list($core, $base) = explode('.', $fixture, 2);
$baseNamespace = 'Cake';
} elseif (strpos($fixture, 'app.') === 0) {
$fixture = substr($fixture, strlen('app.'));
$fixturePaths = array(
TESTS . 'Fixture'
);
list($app, $base) = explode('.', $fixture, 2);
$baseNamespace = Configure::read('App.namespace');
} elseif (strpos($fixture, 'plugin.') === 0) {
$parts = explode('.', $fixture, 3);
$pluginName = $parts[1];
$fixture = $parts[2];
$fixturePaths = array(
CakePlugin::path(Inflector::camelize($pluginName)) . 'Test' . DS . 'Fixture',
TESTS . 'Fixture'
);
list($p, $plugin, $base) = explode('.', $fixture);
$baseNamespace = Plugin::getNamespace($plugin);
} else {
$fixturePaths = array(
TESTS . 'Fixture',
CAKE . 'Test' . DS . 'Fixture'
);
$base = $fixture;
}
$base = Inflector::camelize($base);
$className = implode('\\', array($baseNamespace, 'Test\Fixture', $base . 'Fixture'));

foreach ($fixturePaths as $path) {
$className = Inflector::camelize($fixture);
if (is_readable($path . DS . $className . 'Fixture.php')) {
$fixtureFile = $path . DS . $className . 'Fixture.php';
require_once $fixtureFile;
$fixtureClass = $className . 'Fixture';
$this->_loaded[$fixtureIndex] = new $fixtureClass();
$this->_fixtureMap[$fixtureClass] = $this->_loaded[$fixtureIndex];
break;
}
if ($className) {
$this->_loaded[$fixture] = new $className();
$this->_fixtureMap[$base] = $this->_loaded[$fixture];
}
}
}

/**
* Runs the drop and create commands on the fixtures if necessary.
*
* @param CakeTestFixture $fixture the fixture object to create
* @param Cake\TestSuite\Fixture\TestFixture $fixture the fixture object to create
* @param DataSource $db the datasource instance to use
* @param boolean $drop whether drop the fixture if it is already created or not
* @return void
Expand Down Expand Up @@ -181,10 +171,10 @@ protected function _setupTable($fixture, $db = null, $drop = true) {
/**
* Creates the fixtures tables and inserts data on them.
*
* @param CakeTestCase $test the test to inspect for fixture loading
* @param Cake\TestSuite\TestCase $test the test to inspect for fixture loading
* @return void
*/
public function load(CakeTestCase $test) {
public function load(TestCase $test) {
if (empty($test->fixtures)) {
return;
}
Expand All @@ -211,10 +201,10 @@ public function load(CakeTestCase $test) {
/**
* Truncates the fixtures tables
*
* @param CakeTestCase $test the test to inspect for fixture unloading
* @param Cake\TestSuite\TestCase $test the test to inspect for fixture unloading
* @return void
*/
public function unload(CakeTestCase $test) {
public function unload(TestCase $test) {
$fixtures = !empty($test->fixtures) ? $test->fixtures : array();
foreach (array_reverse($fixtures) as $f) {
if (isset($this->_loaded[$f])) {
Expand All @@ -238,7 +228,7 @@ public function unload(CakeTestCase $test) {
* @throws UnexpectedValueException if $name is not a previously loaded class
*/
public function loadSingle($name, $db = null) {
$name .= 'Fixture';
$name = Inflector::camelize($name);
if (isset($this->_fixtureMap[$name])) {
$fixture = $this->_fixtureMap[$name];
if (!$db) {
Expand All @@ -248,7 +238,7 @@ public function loadSingle($name, $db = null) {
$fixture->truncate($db);
$fixture->insert($db);
} else {
throw new UnexpectedValueException(__d('cake_dev', 'Referenced fixture class %s not found', $name));
throw new \UnexpectedValueException(__d('cake_dev', 'Referenced fixture class %s not found', $name));
}
}

Expand Down

0 comments on commit 0e1f0e4

Please sign in to comment.