08.11 php saved
Vizjerai
Tags add more
controller test case and test suite  
Note
Sample code to get redirects to work and be able to test for the redirected url and status code for CakePHP v1.2 RC2.
  1. // cake/tests/lib/cake_test_case.php
  2. // In CakeTestDispatcher class. Copied code from cake/dispatcher.php
  3. // Note: looks for the <Controller Name>TestController to use the test controller class.
  4.   function __loadController($params) {
  5.     $pluginName = $pluginPath = $controller = null;
  6.  
  7.     if (!empty($params['plugin'])) {
  8.       $this->plugin = $params['plugin'];
  9.       $pluginName = Inflector::camelize($params['plugin']);
  10.       $pluginPath = $pluginName . '.';
  11.       $this->params['controller'] = $this->plugin;
  12.       $controller = $pluginName;
  13.     }
  14.  
  15.     if (!empty($params['controller'])) {
  16.       $controller = Inflector::camelize($params['controller']);
  17.     }
  18.  
  19.     if ($pluginPath . $controller) {
  20.       if (App::import('Controller', $pluginPath . $controller)) {
  21.         if(class_exists($controller.'TestController')) {
  22.           return $controller.'Test'; //Added for test suite.
  23.         } else {
  24.           return $controller;
  25.         }
  26.       }
  27.     }
  28.     return false;
  29.   }
  30.  
  31.  
  32. // app/tests/cases/controllers/reports_controler.test.php
  33. App::import('Controller','Reports');
  34.  
  35. class ReportsTestController extends ReportsController {
  36.   function redirect($url = null, $status = null) {
  37.     return array('url' => $url, 'status' => $status);
  38.   }
  39. }
  40.  
  41. class TestReports extends ReportsTestController {
  42.   var $autoRender = false;
  43.  
  44. }
  45.  
  46. class ReportsControlllerTest extends CakeTestCase {
  47.   var $Reports = null;
  48.  
  49.   function setUp() {
  50.     $this->Reports = new TestReports();
  51.   }
  52.  
  53.   function testReportsView() {
  54.     $result = $this->testAction('/reports/view');
  55.     $expect = array(
  56.       'url' => array(
  57.         'controller' => 'reports',
  58.         'action' => 'index'
  59.       ),
  60.       'status' => 301
  61.     );
  62.  
  63.     $this->assertEqual($result, $expect);
  64.   }
  65.  
  66.   function tearDown() {
  67.     unset($this->Reports);
  68.   }
  69.  
  70. }
  71.  
  72.  
  73. // app/controllers/reports_controller.php
  74. class ReportsController extends AppController {
  75.   var $name = 'Reports';
  76.   var $helpers = array('Html', 'Form');
  77.  
  78.   function view() {
  79.     return $this->redirect(
  80.       array(
  81.         'controller' => 'reports',
  82.         'action' => 'index'
  83.       ),
  84.       301
  85.     );
  86.   }
  87.  
  88. }
  89.  
Parsed in 0.121 seconds, using GeSHi 1.0.7.14

Modify this Paste