Browse Source

Get ControllerTestCase tests working again.

Update some of the controller stubs so they work with new internals and
framework code.

Add the $name parameter to Controller. This is necessary, as a number of
derived properties are set based on the detected name. Another option
is to set all the properties in the stubs.
mark_story 12 years ago
parent
commit
ce08116328

+ 8 - 4
src/Controller/Controller.php

@@ -233,11 +233,15 @@ class Controller extends Object implements EventListener {
  * @param \Cake\Network\Request $request Request object for this controller. Can be null for testing,
  *  but expect that features that use the request parameters will not work.
  * @param \Cake\Network\Response $response Response object for this controller.
+ * @param string $name Override the name useful in testing when using mocks.
  */
-	public function __construct($request = null, $response = null) {
-		if ($this->name === null) {
-			list(, $this->name) = namespaceSplit(get_class($this));
-			$this->name = substr($this->name, 0, -10);
+	public function __construct($request = null, $response = null, $name = null) {
+		if ($this->name === null && $name === null) {
+			list(, $name) = namespaceSplit(get_class($this));
+			$name = substr($name, 0, -10);
+		}
+		if ($name !== null) {
+			$this->name = $name;
 		}
 
 		if (!$this->viewPath) {

+ 11 - 12
src/TestSuite/ControllerTestCase.php

@@ -1,7 +1,5 @@
 <?php
 /**
- * ControllerTestCase file
- *
  * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  *
@@ -59,8 +57,9 @@ class ControllerTestDispatcher extends Dispatcher {
 		$this->testController->helpers = array_merge($default, $this->testController->helpers);
 		$this->testController->setRequest($request);
 		$this->testController->response = $this->response;
-		foreach ($this->testController->Components->loaded() as $component) {
-			$object = $this->testController->Components->{$component};
+		$registry = $this->testController->components();
+		foreach ($registry->loaded() as $component) {
+			$object = $registry->{$component};
 			if (isset($object->response)) {
 				$object->response = $response;
 			}
@@ -98,7 +97,7 @@ class InterceptContentHelper extends Helper {
  */
 	public function afterRender($viewFile) {
 		$this->_View->assign('__view_no_layout__', $this->_View->fetch('content'));
-		$this->_View->Helpers->unload('InterceptContent');
+		$this->_View->helpers()->unload('InterceptContent');
 	}
 
 }
@@ -324,17 +323,16 @@ abstract class ControllerTestCase extends TestCase {
 			'models' => array(),
 			'components' => array()
 		), (array)$mocks);
+		list(, $controllerName) = namespaceSplit($classname);
+		$name = substr($controllerName, 0, -10);
 
 		$request = $this->getMock('Cake\Network\Request');
 		$response = $this->getMock('Cake\Network\Response', array('_sendHeader'));
 		$controller = $this->getMock(
 			$classname,
 			$mocks['methods'],
-			array($request, $response)
+			array($request, $response, $name)
 		);
-		list(, $controllerName) = namespaceSplit($classname);
-		$controller->name = substr($controllerName, 0, -10);
-		$controller->Components->setController($controllerObj);
 
 		foreach ($mocks['models'] as $model => $methods) {
 			if (is_string($methods)) {
@@ -362,10 +360,11 @@ abstract class ControllerTestCase extends TestCase {
 					'class' => $name . 'Component'
 				));
 			}
+			$registry = $controller->components();
+
 			$config = isset($controller->components[$component]) ? $controller->components[$component] : array();
-			$component = $this->getMock($componentClass, $methods, array($controller->Components, $config));
-			$controller->Components->set($name, $component);
-			$controller->Components->enable($name);
+			$component = $this->getMock($componentClass, $methods, array($registry, $config));
+			$registry->set($name, $component);
 		}
 
 		$controller->constructClasses();

+ 8 - 2
tests/TestCase/ORM/BehaviorRegistryTest.php

@@ -180,7 +180,10 @@ class BehaviorRegistryTest extends TestCase {
  */
 	public function testCall() {
 		$this->Behaviors->load('Sluggable');
-		$mockedBehavior = $this->getMock('Behavior', ['slugify']);
+		$mockedBehavior = $this->getMockBuilder('Cake\ORM\Behavior')
+			->setMethods(['slugify'])
+			->disableOriginalConstructor()
+			->getMock();
 		$this->Behaviors->set('Sluggable', $mockedBehavior);
 
 		$mockedBehavior
@@ -213,7 +216,10 @@ class BehaviorRegistryTest extends TestCase {
  */
 	public function testCallFinder() {
 		$this->Behaviors->load('Sluggable');
-		$mockedBehavior = $this->getMock('Behavior', ['findNoSlug']);
+		$mockedBehavior = $this->getMockBuilder('Cake\ORM\Behavior')
+			->setMethods(['findNoSlug'])
+			->disableOriginalConstructor()
+			->getMock();
 		$this->Behaviors->set('Sluggable', $mockedBehavior);
 
 		$query = $this->getMock('Cake\ORM\Query', [], [null, null]);

+ 32 - 48
tests/TestCase/TestSuite/ControllerTestCaseTest.php

@@ -1,9 +1,5 @@
 <?php
 /**
- * ControllerTestCaseTest file
- *
- * Test Case for ControllerTestCase class
- *
  * CakePHP : Rapid Development Framework (http://cakephp.org)
  * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  *
@@ -23,10 +19,10 @@ use Cake\Core\App;
 use Cake\Core\Configure;
 use Cake\Core\Plugin;
 use Cake\ORM\Table;
+use Cake\ORM\TableRegistry;
 use Cake\Routing\Router;
 use Cake\TestSuite\Reporter\HtmlReporter;
 use Cake\TestSuite\TestCase;
-use Cake\Utility\ClassRegistry;
 
 /**
  * AppController class
@@ -42,13 +38,6 @@ class AppController extends Controller {
 	public $helpers = array('Html');
 
 /**
- * uses property
- *
- * @var array
- */
-	public $uses = array('ControllerPost');
-
-/**
  * components property
  *
  * @var array
@@ -63,13 +52,6 @@ class AppController extends Controller {
  */
 class ControllerTestCaseTestController extends AppController {
 
-/**
- * Uses array
- *
- * @param array
- */
-	public $uses = array('TestPlugin.TestPluginComment');
-
 }
 
 /**
@@ -92,12 +74,12 @@ class ControllerTestCaseTest extends TestCase {
  */
 	public function setUp() {
 		parent::setUp();
-		$this->markTestIncomplete('Need to revisit once models work again.');
 		Configure::write('App.namespace', 'TestApp');
 		Plugin::load(array('TestPlugin', 'TestPluginTwo'));
 
 		$this->Case = $this->getMockForAbstractClass('Cake\TestSuite\ControllerTestCase');
 		Router::reload();
+		TableRegistry::clear();
 	}
 
 /**
@@ -117,7 +99,8 @@ class ControllerTestCaseTest extends TestCase {
 	public function testGenerate() {
 		$Posts = $this->Case->generate('TestApp\Controller\PostsController');
 		$this->assertEquals('Posts', $Posts->name);
-		$this->assertEquals('Post', $Posts->modelClass);
+		$this->assertEquals('Posts', $Posts->modelClass);
+		$this->assertEquals('Posts', $Posts->viewPath);
 		$this->assertNull($Posts->response->send());
 
 		$Posts = $this->Case->generate('TestApp\Controller\PostsController', array(
@@ -128,34 +111,33 @@ class ControllerTestCaseTest extends TestCase {
 		$this->assertNull($Posts->render('index'));
 
 		$Posts = $this->Case->generate('TestApp\Controller\PostsController', array(
-			'models' => array('Post'),
+			'models' => array('Posts'),
 			'components' => array('RequestHandler')
 		));
 
-		$this->assertInstanceOf('TestApp\Model\Post', $Posts->Post);
-		$this->assertNull($Posts->Post->save(array()));
-		$this->assertNull($Posts->Post->find('all'));
-		$this->assertEquals('posts', $Posts->Post->useTable);
-		$this->assertNull($Posts->RequestHandler->isAjax());
+		$this->assertInstanceOf('TestApp\Model\Table\PostsTable', $Posts->Posts);
+		$this->assertNull($Posts->Posts->deleteAll(array()));
+		$this->assertNull($Posts->Posts->find('all'));
+		$this->assertNull($Posts->RequestHandler->isXml());
 
 		$Posts = $this->Case->generate('TestApp\Controller\PostsController', array(
 			'models' => array(
-				'Post' => true
+				'Posts' => true
 			)
 		));
-		$this->assertNull($Posts->Post->save(array()));
-		$this->assertNull($Posts->Post->find('all'));
+		$this->assertNull($Posts->Posts->deleteAll([]));
+		$this->assertNull($Posts->Posts->find('all'));
 
 		$Posts = $this->Case->generate('TestApp\Controller\PostsController', array(
 			'models' => array(
-				'Post' => array('save'),
+				'Posts' => array('deleteAll'),
 			)
 		));
-		$this->assertNull($Posts->Post->save(array()));
-		$this->assertInternalType('array', $Posts->Post->find('all'));
+		$this->assertNull($Posts->Posts->deleteAll([]));
+		$this->assertEquals('posts', $Posts->Posts->table());
 
 		$Posts = $this->Case->generate('TestApp\Controller\PostsController', array(
-			'models' => array('Post'),
+			'models' => array('Posts'),
 			'components' => array(
 				'RequestHandler' => array('isPut'),
 				'Session'
@@ -180,7 +162,7 @@ class ControllerTestCaseTest extends TestCase {
 		));
 
 		$expected = array('some' => 'config');
-		$settings = array_intersect_key($Tests->RequestHandler->settings, array('some' => 'foo'));
+		$settings = array_intersect_key($Tests->RequestHandler->config(), array('some' => 'foo'));
 		$this->assertSame($expected, $settings, 'A mocked component should have the same config as an unmocked component');
 
 		$Tests = $this->Case->generate('TestConfigs', array(
@@ -190,7 +172,7 @@ class ControllerTestCaseTest extends TestCase {
 		));
 
 		$expected = array('some' => 'config');
-		$settings = array_intersect_key($Tests->RequestHandler->settings, array('some' => 'foo'));
+		$settings = array_intersect_key($Tests->RequestHandler->config(), array('some' => 'foo'));
 		$this->assertSame($expected, $settings, 'A mocked component should have the same config as an unmocked component');
 	}
 
@@ -200,7 +182,7 @@ class ControllerTestCaseTest extends TestCase {
 	public function testGenerateWithPlugin() {
 		$Tests = $this->Case->generate('TestPlugin.Tests', array(
 			'models' => array(
-				'TestPlugin.TestPluginComment'
+				'TestPlugin.TestPluginComments'
 			),
 			'components' => array(
 				'TestPlugin.Plugins'
@@ -209,8 +191,8 @@ class ControllerTestCaseTest extends TestCase {
 		$this->assertEquals('Tests', $Tests->name);
 		$this->assertInstanceOf('TestPlugin\Controller\Component\PluginsComponent', $Tests->Plugins);
 
-		$result = ClassRegistry::init('TestPlugin.TestPluginComment');
-		$this->assertInstanceOf('TestPlugin\Model\TestPluginComment', $result);
+		$result = TableRegistry::get('TestPlugin.TestPluginComments');
+		$this->assertInstanceOf('TestPlugin\Model\Table\TestPluginCommentsTable', $result);
 	}
 
 /**
@@ -258,16 +240,16 @@ class ControllerTestCaseTest extends TestCase {
  */
 	public function testUseRoutes() {
 		Router::connect('/:controller/:action/*');
-		include CAKE . 'Test/TestApp/Config/routes.php';
+		include APP . '/Config/routes.php';
 
 		$controller = $this->Case->generate('TestsApps');
-		$controller->Components->load('RequestHandler');
+		$controller->components()->load('RequestHandler');
 		$result = $this->Case->testAction('/tests_apps/index.json', array('return' => 'contents'));
 		$result = json_decode($result, true);
 		$expected = array('cakephp' => 'cool');
 		$this->assertEquals($expected, $result);
 
-		include CAKE . 'Test/TestApp/Config/routes.php';
+		include APP . '/Config/routes.php';
 		$result = $this->Case->testAction('/some_alias');
 		$this->assertEquals(5, $result);
 	}
@@ -279,7 +261,7 @@ class ControllerTestCaseTest extends TestCase {
  */
 	public function testSkipRoutes() {
 		Router::connect('/:controller/:action/*');
-		include CAKE . 'Test/TestApp/Config/routes.php';
+		include APP . 'Config/routes.php';
 
 		$this->Case->loadRoutes = false;
 		$this->Case->testAction('/tests_apps/missing_action.json', array('return' => 'view'));
@@ -296,6 +278,7 @@ class ControllerTestCaseTest extends TestCase {
 
 		$data = array('var' => 'set');
 		$result = $this->Case->testAction('/tests_apps_posts/post_var', array(
+			'method' => 'post',
 			'data' => $data,
 			'return' => 'vars'
 		));
@@ -321,9 +304,7 @@ class ControllerTestCaseTest extends TestCase {
 		$this->Case->autoMock = true;
 
 		$data = array(
-			'Post' => array(
-				'name' => 'Some Post'
-			)
+			'name' => 'Some Post'
 		);
 		$this->Case->testAction('/tests_apps_posts/post_var', array(
 			'method' => 'post',
@@ -342,9 +323,12 @@ class ControllerTestCaseTest extends TestCase {
 		));
 		$this->assertEquals(array('name', 'pork'), array_keys($result['data']));
 
-		$result = $this->Case->testAction('/tests_apps_posts/add', array('return' => 'vars'));
+		$result = $this->Case->testAction('/tests_apps_posts/add', array(
+			'method' => 'post',
+			'return' => 'vars'
+		));
 		$this->assertTrue(array_key_exists('posts', $result));
-		$this->assertEquals(4, count($result['posts']));
+		$this->assertInstanceOf('Cake\ORM\Query', $result['posts']);
 		$this->assertTrue($this->Case->controller->request->is('post'));
 	}
 

+ 0 - 2
tests/test_app/Plugin/TestPlugin/Model/Table/TestPluginCommentsTable.php

@@ -1,7 +1,5 @@
 <?php
 /**
- * Test Plugin Comments Model
- *
  * CakePHP :  Rapid Development Framework (http://cakephp.org)
  * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  *

+ 8 - 13
tests/test_app/TestApp/Controller/TestsAppsPostsController.php

@@ -23,21 +23,15 @@ namespace TestApp\Controller;
 
 class TestsAppsPostsController extends AppController {
 
-	public $uses = array('Post');
-
 	public $viewPath = 'TestsApps';
 
 	public function add() {
-		$data = array(
-			'Post' => array(
-				'title' => 'Test article',
-				'body' => 'Body of article.',
-				'author_id' => 1
-			)
-		);
-		$this->Post->save($data);
-
-		$this->set('posts', $this->Post->find('all'));
+		$this->loadModel('Posts');
+		$entity = $this->Posts->newEntity($this->request->data);
+		if ($entity) {
+			$this->Posts->save($entity);
+		}
+		$this->set('posts', $this->Posts->find('all'));
 		$this->render('index');
 	}
 
@@ -69,7 +63,8 @@ class TestsAppsPostsController extends AppController {
  *
  */
 	public function fixtured() {
-		$this->set('posts', $this->Post->find('all'));
+		$this->loadModel('Posts');
+		$this->set('posts', $this->Posts->find('all'));
 		$this->render('index');
 	}