Testing A Controller

Testing a controller

This entry is the result of a forum thread. “The section Testing an HTTP GET on the Controller” doesn't work yet. It is on the front burner until the procedure and code does work. Watch the thread “functional tests (http://forum.akelos.org/discussion/123/)” for progress.

General Setup

Definitions/variables:

  • project - The name at the root of your project
  • root-folder - The whole path up to your project, such as
    1. Linux: /home/alan/develop/php
  • docroot - The place where Apache has direct access, such as
    1. Linux: /var/www/html
  • server - The base of the URL to access your web server, such as http://localhost
  • name - The name of the controller that you are writing the test for.

——————————-

  1. Make project/test/fixtures/public folder visible to your web server:
    1. Linux:
      1. With a link:
        ln -s project/test/fixtures/public docroot/project_test
      2. With a virtual server.
        This approach requires that you be able to create a virtual server by editing httpd.conf. Make the docroot of your server point to project/test/fixtures/public.
  2. In project/config/config.php, add a line to tell Akelos where the testing URL is:
    1. Linux:
      1. With a link:
        defined('AK_TESTING_URL') ? null : define('AK_TESTING_URL', 'server/project_test');
      2. With a virtual server:
        defined('AK_TESTING_URL') ? null : define('AK_TESTING_URL', 'server);
  3. With your editor, open a simple test web test case in project/test/functional/controllers/name_controller.php. Please note that “name” and “Name” in this illustration is the name of your controller. It will look something like this:
    <?php
    require_once(dirname(__FILE__).'/../../fixtures/config/config.php');
    require_once(AK_APP_DIR.DS.'application_controller.php');
    require_once(AK_CONTROLLERS_DIR.DS.'name_controller.php');
     
    class NameControllerTest extends AkWebTestCase
    {
        function test_Name()
        {
            $this->assertTrue(false, 'NameController has not been tested');
        }
    }
     
    ak_test('NameControllerTest',true);
     
    ?>

To run the whole suite of tests in the above file, open a terminal window and enter:
cd root-folder/project
./script/test test/functional/controllers/name_controller.php

Testing an HTTP GET on the Controller

Add the following method to the above test:

        function test_should_get_index()
        {
            $this->setMaximumRedirects(0);
            $this->get(AK_TESTING_URL.'/name/');
            $this->assertResponse(200);
        }

This test is presently giving a response of 500 instead of the expected 200. This author, alake, doesn't know how to adjust this code for the desired result.

 
testing-guide/testing-a-controller_cn.txt · Last modified: 2008/08/27 11:28 by liyh
 

The Akelos Framework was created by Bermi Ferrer and other contributors.
Potions of the code and documentation have been ported from Ruby on Rails.

The Akelos Framework is released under the LGPL license.

"Akelos", "Akelos Framework", and the Akelos logo are trademarks of Bermi Labs All rights reserved.

Wiki driven by DokuWiki