Location: PHPKode > projects > PHP on Trax > johnpipi-trax-f599562/trax/test/RouterTest.php
<?php
/**
 *  File for the RouterTest class
 *
 * (PHP 5)
 *
 * @package PHPonTraxTest
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 * @copyright (c) Walter O. Haas 2006
 * @version $Id$
 * @author Walt Haas <hide@address.com>
 */

echo "testing Router\n";
require_once 'testenv.php';

// Call RouterTest::main() if this source file is executed directly.
if (!defined("PHPUnit2_MAIN_METHOD")) {
    define("PHPUnit2_MAIN_METHOD", "RouterTest::main");
}

require_once "PHPUnit2/Framework/TestCase.php";
require_once "PHPUnit2/Framework/TestSuite.php";

// You may remove the following line when all tests have been implemented.
require_once "PHPUnit2/Framework/IncompleteTestError.php";

require_once "router.php";

/**
 * Test class for Router.
 * Generated by PHPUnit2_Util_Skeleton on 2006-03-01 at 15:31:34.
 */
class RouterTest extends PHPUnit2_Framework_TestCase {
    /**
     * Runs the test methods of this class.
     *
     * @access public
     * @static
     */
    public static function main() {
        require_once "PHPUnit2/TextUI/TestRunner.php";

        $suite  = new PHPUnit2_Framework_TestSuite("RouterTest");
        $result = PHPUnit2_TextUI_TestRunner::run($suite);
    }

    /**
     * Sets up the fixture, for example, open a network connection.
     * This method is called before a test is executed.
     *
     * @access protected
     */
    protected function setUp() {
    }

    /**
     * Tears down the fixture, for example, close a network connection.
     * This method is called after a test is executed.
     *
     * @access protected
     */
    protected function tearDown() {
    }

    /**
     *  Test build_route_regexp().
     */
    public function testBuild_route_regexp() {
        $r = new Router;
        // Two abstract route components
        $regexp = $r->build_route_regexp(':foo/:bar');
        $this->assertEquals($regexp,
                            '^[a-z0-9_\-]+\/[a-z0-9_\-]+$');
        // Three abstract route components
        $regexp = $r->build_route_regexp(':foo/:bar/:mumble');
        $this->assertEquals($regexp,
                            '^[a-z0-9_\-]+\/[a-z0-9_\-]+\/[a-z0-9_\-]+$');
        // Abstract, concrete, abstract route components
        $regexp = $r->build_route_regexp(':foo/bar/:mumble');
        $this->assertEquals($regexp,
                            '^[a-z0-9_\-]+\/bar\/[a-z0-9_\-]+$');
        // Two concrete route components
        $regexp = $r->build_route_regexp('foo/bar');
        $this->assertEquals($regexp,
                            '^foo\/bar$');
    }

    /**
     * Test default route table
     */
    public function testDefault_route() {
        $r = new Router;
        // Should find default route
        $route = $r->find_route('a/b/mumble');
        $this->assertEquals(':controller/:action/:id', $route['path']);
        $this->assertNull($route['params']);
    }

    /**
     * Test route table with one simple entry besides default
     */
    public function testSimple_route() {
        $r = new Router;
        //  Build route table
        $r->connect(':foo/:bar/mumble', array('mumble route'));
        //  Params not an array ignored, null is stored
        $r->connect(':controller/:action/:id', 'not-an-array');

        //  Match first route
        $route = $r->find_route('a/b/mumble');
        $this->assertEquals(':foo/:bar/mumble', $route['path']);
        $this->assertEquals(array('mumble route'), $route['params']);
        $selected = $r->get_selected_route();
        $this->assertEquals(':foo/:bar/mumble', $selected['path']);
        $this->assertEquals(array('mumble route'), $selected['params']);

        //  Match second route
        $route = $r->find_route('a/b/c');
        $this->assertEquals(':controller/:action/:id', $route['path']);
        $this->assertNull($route['params']);
        $selected = $r->get_selected_route();
        $this->assertEquals(':controller/:action/:id', $selected['path']);
        $this->assertNull($selected['params']);
    }

    /**
     * Test route table with one regexp entry besides default
     */
    public function testRegexp_route() {
        $r = new Router;
        //  Build route table
        $r->connect(':foo/:bar/\?(catalog|part)number=.*',
                    array('number route'));
        $r->connect(':controller/:action/:id', array('default route'));

        //  Match first route
        $route = $r->find_route('a/b/?catalognumber=17');
        $this->assertEquals(':foo/:bar/\?(catalog|part)number=.*',
                            $route['path']);
        $this->assertEquals(array('number route'), $route['params']);
        $route = $r->find_route('a/b/?partnumber=123-456');
        $this->assertEquals(':foo/:bar/\?(catalog|part)number=.*',
                            $route['path']);
        $this->assertEquals(array('number route'), $route['params']);
        $route = $r->find_route('a/b/?personnumber=156');
        $this->assertEquals(':controller/:action/:id', $route['path']);
        $this->assertEquals(array('default route'), $route['params']);
    }

    /**
     * Test route table with route with empty path
     */
    public function testEmpty_route() {
        $r = new Router;
        //  Build route table with only an empty path
        $r->connect('', array('empty route'));
        $route = $r->find_route('');
        $this->assertEquals('', $route['path']);
        $this->assertEquals(array('empty route'), $route['params']);
        //  This route shouldn't match anything
        $route = $r->find_route('mumble/foo');
        $this->assertNull($route);
    }
}

// Call RouterTest::main() if this source file is executed directly.
if (PHPUnit2_MAIN_METHOD == "RouterTest::main") {
    RouterTest::main();
}

// -- set Emacs parameters --
// Local variables:
// tab-width: 4
// c-basic-offset: 4
// c-hanging-comment-ender-p: nil
// indent-tabs-mode: nil
// End:
?>
Return current item: PHP on Trax