123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564 |
- <?php
- /**
- * ControllerTestCaseTest file
- *
- * Test Case for ControllerTestCase class
- *
- * PHP version 5
- *
- * CakePHP : Rapid Development Framework (http://cakephp.org)
- * Copyright 2005-2012, Cake Software Foundation, Inc.
- *
- * Licensed under The MIT License
- * Redistributions of files must retain the above copyright notice.
- *
- * @copyright Copyright 2005-2012, Cake Software Foundation, Inc.
- * @link http://cakephp.org CakePHP Project
- * @package Cake.Test.Case.TestSuite
- * @since CakePHP v 2.0
- * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
- */
- App::uses('Controller', 'Controller');
- App::uses('Model', 'Model');
- App::uses('AppModel', 'Model');
- App::uses('CakeHtmlReporter', 'TestSuite/Reporter');
- require_once dirname(dirname(__FILE__)) . DS . 'Model' . DS . 'models.php';
- /**
- * AppController class
- *
- * @package Cake.Test.Case.TestSuite
- */
- if (!class_exists('AppController', false)) {
- /**
- * AppController class
- *
- * @package Cake.Test.Case.TestSuite
- */
- class AppController extends Controller {
- /**
- * helpers property
- *
- * @var array
- * @access public
- */
- public $helpers = array('Html');
- /**
- * uses property
- *
- * @var array
- * @access public
- */
- public $uses = array('ControllerPost');
- /**
- * components property
- *
- * @var array
- * @access public
- */
- public $components = array('Cookie');
- }
- } elseif (!defined('APP_CONTROLLER_EXISTS')) {
- define('APP_CONTROLLER_EXISTS', true);
- }
- /**
- * PostsController class
- */
- if (!class_exists('PostsController')) {
- class PostsController extends AppController {
- /**
- * Components array
- *
- * @var array
- */
- public $components = array(
- 'RequestHandler',
- 'Email',
- 'Auth'
- );
- }
- }
- /**
- * ControllerTestCaseTest controller
- */
- class ControllerTestCaseTestController extends AppController {
- /**
- * Uses array
- *
- * @param array
- */
- public $uses = array('TestPlugin.TestPluginComment');
- }
- /**
- * ControllerTestCaseTest
- *
- * @package Cake.Test.Case.TestSuite
- */
- class ControllerTestCaseTest extends CakeTestCase {
- /**
- * fixtures property
- *
- * @var array
- */
- public $fixtures = array('core.post', 'core.author', 'core.test_plugin_comment');
- /**
- * reset environment.
- *
- * @return void
- */
- public function setUp() {
- parent::setUp();
- App::build(array(
- 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
- 'Controller' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Controller' . DS),
- 'Model' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Model' . DS),
- 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
- ), App::RESET);
- CakePlugin::load(array('TestPlugin', 'TestPluginTwo'));
- $this->Case = $this->getMockForAbstractClass('ControllerTestCase');
- Router::reload();
- }
- /**
- * tearDown
- *
- * @return void
- */
- public function tearDown() {
- parent::tearDown();
- CakePlugin::unload();
- $this->Case->controller = null;
- }
- /**
- * Test that ControllerTestCase::generate() creates mock objects correctly
- */
- public function testGenerate() {
- if (defined('APP_CONTROLLER_EXISTS')) {
- $this->markTestSkipped('AppController exists, cannot run.');
- }
- $Posts = $this->Case->generate('Posts');
- $this->assertEquals('Posts', $Posts->name);
- $this->assertEquals('Post', $Posts->modelClass);
- $this->assertNull($Posts->response->send());
- $Posts = $this->Case->generate('Posts', array(
- 'methods' => array(
- 'render'
- )
- ));
- $this->assertNull($Posts->render('index'));
- $Posts = $this->Case->generate('Posts', array(
- 'models' => array('Post'),
- 'components' => array('RequestHandler')
- ));
- $this->assertInstanceOf('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());
- $Posts = $this->Case->generate('Posts', array(
- 'models' => array(
- 'Post' => true
- )
- ));
- $this->assertNull($Posts->Post->save(array()));
- $this->assertNull($Posts->Post->find('all'));
- $Posts = $this->Case->generate('Posts', array(
- 'models' => array(
- 'Post' => array('save'),
- )
- ));
- $this->assertNull($Posts->Post->save(array()));
- $this->assertInternalType('array', $Posts->Post->find('all'));
- $Posts = $this->Case->generate('Posts', array(
- 'models' => array('Post'),
- 'components' => array(
- 'RequestHandler' => array('isPut'),
- 'Email' => array('send'),
- 'Session'
- )
- ));
- $Posts->RequestHandler->expects($this->once())
- ->method('isPut')
- ->will($this->returnValue(true));
- $this->assertTrue($Posts->RequestHandler->isPut());
- $Posts->Auth->Session->expects($this->any())
- ->method('write')
- ->will($this->returnValue('written!'));
- $this->assertEquals('written!', $Posts->Auth->Session->write('something'));
- }
- /**
- * Tests ControllerTestCase::generate() using classes from plugins
- */
- public function testGenerateWithPlugin() {
- $Tests = $this->Case->generate('TestPlugin.Tests', array(
- 'models' => array(
- 'TestPlugin.TestPluginComment'
- ),
- 'components' => array(
- 'TestPlugin.Plugins'
- )
- ));
- $this->assertEquals('Tests', $Tests->name);
- $this->assertInstanceOf('PluginsComponent', $Tests->Plugins);
- $result = ClassRegistry::init('TestPlugin.TestPluginComment');
- $this->assertInstanceOf('TestPluginComment', $result);
- $Tests = $this->Case->generate('ControllerTestCaseTest', array(
- 'models' => array(
- 'TestPlugin.TestPluginComment' => array('save')
- )
- ));
- $this->assertInstanceOf('TestPluginComment', $Tests->TestPluginComment);
- $Tests->TestPluginComment->expects($this->at(0))
- ->method('save')
- ->will($this->returnValue(true));
- $Tests->TestPluginComment->expects($this->at(1))
- ->method('save')
- ->will($this->returnValue(false));
- $this->assertTrue($Tests->TestPluginComment->save(array()));
- $this->assertFalse($Tests->TestPluginComment->save(array()));
- }
- /**
- * Tests testAction
- */
- public function testTestAction() {
- $Controller = $this->Case->generate('TestsApps');
- $this->Case->testAction('/tests_apps/index');
- $this->assertInternalType('array', $this->Case->controller->viewVars);
- $this->Case->testAction('/tests_apps/set_action');
- $results = $this->Case->controller->viewVars;
- $expected = array(
- 'var' => 'string'
- );
- $this->assertEquals($expected, $results);
- $result = $this->Case->controller->response->body();
- $this->assertRegExp('/This is the TestsAppsController index view/', $result);
- $Controller = $this->Case->generate('TestsApps');
- $this->Case->testAction('/tests_apps/redirect_to');
- $results = $this->Case->headers;
- $expected = array(
- 'Location' => 'http://cakephp.org'
- );
- $this->assertEquals($expected, $results);
- }
- /**
- * Make sure testAction() can hit plugin controllers.
- *
- * @return void
- */
- public function testTestActionWithPlugin() {
- $this->Case->generate('TestPlugin.Tests');
- $this->Case->testAction('/test_plugin/tests/index');
- $this->assertEquals('It is a variable', $this->Case->controller->viewVars['test_value']);
- }
- /**
- * Tests using loaded routes during tests
- *
- * @return void
- */
- public function testUseRoutes() {
- Router::connect('/:controller/:action/*');
- include CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS . 'routes.php';
- $controller = $this->Case->generate('TestsApps');
- $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' . DS . 'test_app' . DS . 'Config' . DS . 'routes.php';
- $result = $this->Case->testAction('/some_alias');
- $this->assertEquals(5, $result);
- }
- /**
- * Tests not using loaded routes during tests
- *
- * @expectedException MissingActionException
- */
- public function testSkipRoutes() {
- Router::connect('/:controller/:action/*');
- include CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS . 'routes.php';
- $this->Case->loadRoutes = false;
- $this->Case->testAction('/tests_apps/missing_action.json', array('return' => 'view'));
- }
- /**
- * Tests backwards compatibility with setting the return type
- */
- public function testBCSetReturn() {
- $this->Case->autoMock = true;
- $result = $this->Case->testAction('/tests_apps/some_method');
- $this->assertEquals(5, $result);
- $data = array('var' => 'set');
- $result = $this->Case->testAction('/tests_apps_posts/post_var', array(
- 'data' => $data,
- 'return' => 'vars'
- ));
- $this->assertEquals($data, $result['data']);
- $result = $this->Case->testAction('/tests_apps/set_action', array(
- 'return' => 'view'
- ));
- $this->assertEquals('This is the TestsAppsController index view string', $result);
- $result = $this->Case->testAction('/tests_apps/set_action', array(
- 'return' => 'contents'
- ));
- $this->assertRegExp('/<html/', $result);
- $this->assertRegExp('/This is the TestsAppsController index view/', $result);
- $this->assertRegExp('/<\/html>/', $result);
- }
- /**
- * Tests sending POST data to testAction
- */
- public function testTestActionPostData() {
- $this->Case->autoMock = true;
- $data = array(
- 'Post' => array(
- 'name' => 'Some Post'
- )
- );
- $this->Case->testAction('/tests_apps_posts/post_var', array(
- 'data' => $data
- ));
- $this->assertEquals($this->Case->controller->viewVars['data'], $data);
- $this->assertEquals($this->Case->controller->data, $data);
- $this->Case->testAction('/tests_apps_posts/post_var/named:param', array(
- 'data' => $data
- ));
- $expected = array(
- 'named' => 'param'
- );
- $this->assertEquals($expected, $this->Case->controller->request->named);
- $this->assertEquals($this->Case->controller->data, $data);
- $result = $this->Case->testAction('/tests_apps_posts/post_var', array(
- 'return' => 'vars',
- 'method' => 'post',
- 'data' => array(
- 'name' => 'is jonas',
- 'pork' => 'and beans',
- )
- ));
- $this->assertEquals(array('name', 'pork'), array_keys($result['data']));
- $result = $this->Case->testAction('/tests_apps_posts/add', array('return' => 'vars'));
- $this->assertTrue(array_key_exists('posts', $result));
- $this->assertEquals(4, count($result['posts']));
- $this->assertTrue($this->Case->controller->request->is('post'));
- }
- /**
- * Tests sending GET data to testAction
- */
- public function testTestActionGetData() {
- $this->Case->autoMock = true;
- $result = $this->Case->testAction('/tests_apps_posts/url_var', array(
- 'method' => 'get',
- 'data' => array(
- 'some' => 'var',
- 'lackof' => 'creativity'
- )
- ));
- $this->assertEquals('var', $this->Case->controller->request->query['some']);
- $this->assertEquals('creativity', $this->Case->controller->request->query['lackof']);
- $result = $this->Case->testAction('/tests_apps_posts/url_var/var1:value1/var2:val2', array(
- 'return' => 'vars',
- 'method' => 'get',
- ));
- $this->assertEquals(array('var1', 'var2'), array_keys($result['params']['named']));
- $result = $this->Case->testAction('/tests_apps_posts/url_var/gogo/val2', array(
- 'return' => 'vars',
- 'method' => 'get',
- ));
- $this->assertEquals(array('gogo', 'val2'), $result['params']['pass']);
- $result = $this->Case->testAction('/tests_apps_posts/url_var', array(
- 'return' => 'vars',
- 'method' => 'get',
- 'data' => array(
- 'red' => 'health',
- 'blue' => 'mana'
- )
- ));
- $query = $this->Case->controller->request->query;
- $this->assertTrue(isset($query['red']));
- $this->assertTrue(isset($query['blue']));
- }
- /**
- * Test that REST actions with XML/JSON input work.
- *
- * @return void
- */
- public function testTestActionJsonData() {
- $result = $this->Case->testAction('/tests_apps_posts/input_data', array(
- 'return' => 'vars',
- 'method' => 'post',
- 'data' => '{"key":"value","json":true}'
- ));
- $this->assertEquals('value', $result['data']['key']);
- $this->assertTrue($result['data']['json']);
- }
- /**
- * Tests autoMock ability
- */
- public function testAutoMock() {
- $this->Case->autoMock = true;
- $this->Case->testAction('/tests_apps/set_action');
- $results = $this->Case->controller->viewVars;
- $expected = array(
- 'var' => 'string'
- );
- $this->assertEquals($expected, $results);
- }
- /**
- * Test using testAction and not mocking
- */
- public function testNoMocking() {
- $result = $this->Case->testAction('/tests_apps/some_method');
- $this->Case->assertEquals(5, $result);
- $data = array('var' => 'set');
- $result = $this->Case->testAction('/tests_apps_posts/post_var', array(
- 'data' => $data,
- 'return' => 'vars'
- ));
- $this->assertEquals($data, $result['data']);
- $result = $this->Case->testAction('/tests_apps/set_action', array(
- 'return' => 'view'
- ));
- $this->assertEquals('This is the TestsAppsController index view string', $result);
- $result = $this->Case->testAction('/tests_apps/set_action', array(
- 'return' => 'contents'
- ));
- $this->assertRegExp('/<html/', $result);
- $this->assertRegExp('/This is the TestsAppsController index view/', $result);
- $this->assertRegExp('/<\/html>/', $result);
- }
- /**
- * Test that controllers don't get reused.
- *
- * @return void
- */
- public function testNoControllerReuse() {
- $this->Case->autoMock = true;
- $result = $this->Case->testAction('/tests_apps/index', array(
- 'data' => array('var' => 'first call'),
- 'method' => 'get',
- 'return' => 'contents',
- ));
- $this->assertContains('<html', $result);
- $this->assertContains('This is the TestsAppsController index view', $result);
- $this->assertContains('first call', $result);
- $this->assertContains('</html>', $result);
- $result = $this->Case->testAction('/tests_apps/index', array(
- 'data' => array('var' => 'second call'),
- 'method' => 'get',
- 'return' => 'contents'
- ));
- $this->assertContains('second call', $result);
- $result = $this->Case->testAction('/tests_apps/index', array(
- 'data' => array('var' => 'third call'),
- 'method' => 'get',
- 'return' => 'contents'
- ));
- $this->assertContains('third call', $result);
- }
- /**
- * Test that multiple calls to redirect in the same test method don't cause issues.
- *
- * @return void
- */
- public function testTestActionWithMultipleRedirect() {
- $this->Case->generate('TestsApps');
- $options = array('method' => 'get');
- $this->Case->testAction('/tests_apps/redirect_to', $options);
- $this->Case->testAction('/tests_apps/redirect_to', $options);
- }
- /**
- * Tests that Components storing response or request objects internally during construct
- * will always have a fresh reference to those object available
- *
- * @return void
- * @see http://cakephp.lighthouseapp.com/projects/42648-cakephp/tickets/2705-requesthandler-weird-behavior
- */
- public function testComponentsSameRequestAndResponse() {
- $this->Case->generate('TestsApps');
- $options = array('method' => 'get');
- $this->Case->testAction('/tests_apps/index', $options);
- $this->assertSame($this->Case->controller->response, $this->Case->controller->RequestHandler->response);
- $this->assertSame($this->Case->controller->request, $this->Case->controller->RequestHandler->request);
- }
- /**
- * Test that testAction() doesn't destroy data in GET & POST
- *
- * @return void
- */
- public function testRestoreGetPost() {
- $restored = array('new' => 'value');
- $_GET = $restored;
- $_POST = $restored;
- $this->Case->generate('TestsApps');
- $options = array('method' => 'get');
- $this->Case->testAction('/tests_apps/index', $options);
- $this->assertEquals($restored, $_GET);
- $this->assertEquals($restored, $_POST);
- }
- }
|