root/trunk/trax/test/RouterTest.php

Revision 192, 6.0 KB (checked in by haas, 6 years ago)

working Pear install w/ tests

  • Property svn:executable set to *
  • Property svn:keywords set to Id
Line 
1<?php
2/**
3 *  File for the RouterTest class
4 *
5 * (PHP 5)
6 *
7 * @package PHPonTraxTest
8 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
9 * @copyright (c) Walter O. Haas 2006
10 * @version $Id$
11 * @author Walt Haas <haas@xmission.com>
12 */
13
14echo "testing Router\n";
15require_once 'testenv.php';
16
17// Call RouterTest::main() if this source file is executed directly.
18if (!defined("PHPUnit2_MAIN_METHOD")) {
19    define("PHPUnit2_MAIN_METHOD", "RouterTest::main");
20}
21
22require_once "PHPUnit2/Framework/TestCase.php";
23require_once "PHPUnit2/Framework/TestSuite.php";
24
25// You may remove the following line when all tests have been implemented.
26require_once "PHPUnit2/Framework/IncompleteTestError.php";
27
28require_once "router.php";
29
30/**
31 * Test class for Router.
32 * Generated by PHPUnit2_Util_Skeleton on 2006-03-01 at 15:31:34.
33 */
34class RouterTest extends PHPUnit2_Framework_TestCase {
35    /**
36     * Runs the test methods of this class.
37     *
38     * @access public
39     * @static
40     */
41    public static function main() {
42        require_once "PHPUnit2/TextUI/TestRunner.php";
43
44        $suite  = new PHPUnit2_Framework_TestSuite("RouterTest");
45        $result = PHPUnit2_TextUI_TestRunner::run($suite);
46    }
47
48    /**
49     * Sets up the fixture, for example, open a network connection.
50     * This method is called before a test is executed.
51     *
52     * @access protected
53     */
54    protected function setUp() {
55    }
56
57    /**
58     * Tears down the fixture, for example, close a network connection.
59     * This method is called after a test is executed.
60     *
61     * @access protected
62     */
63    protected function tearDown() {
64    }
65
66    /**
67     *  Test build_route_regexp().
68     */
69    public function testBuild_route_regexp() {
70        $r = new Router;
71        // Two abstract route components
72        $regexp = $r->build_route_regexp(':foo/:bar');
73        $this->assertEquals($regexp,
74                            '^[a-z0-9_\-]+\/[a-z0-9_\-]+$');
75        // Three abstract route components
76        $regexp = $r->build_route_regexp(':foo/:bar/:mumble');
77        $this->assertEquals($regexp,
78                            '^[a-z0-9_\-]+\/[a-z0-9_\-]+\/[a-z0-9_\-]+$');
79        // Abstract, concrete, abstract route components
80        $regexp = $r->build_route_regexp(':foo/bar/:mumble');
81        $this->assertEquals($regexp,
82                            '^[a-z0-9_\-]+\/bar\/[a-z0-9_\-]+$');
83        // Two concrete route components
84        $regexp = $r->build_route_regexp('foo/bar');
85        $this->assertEquals($regexp,
86                            '^foo\/bar$');
87    }
88
89    /**
90     * Test default route table
91     */
92    public function testDefault_route() {
93        $r = new Router;
94        // Should find default route
95        $route = $r->find_route('a/b/mumble');
96        $this->assertEquals(':controller/:action/:id', $route['path']);
97        $this->assertNull($route['params']);
98    }
99
100    /**
101     * Test route table with one simple entry besides default
102     */
103    public function testSimple_route() {
104        $r = new Router;
105        //  Build route table
106        $r->connect(':foo/:bar/mumble', array('mumble route'));
107        //  Params not an array ignored, null is stored
108        $r->connect(':controller/:action/:id', 'not-an-array');
109
110        //  Match first route
111        $route = $r->find_route('a/b/mumble');
112        $this->assertEquals(':foo/:bar/mumble', $route['path']);
113        $this->assertEquals(array('mumble route'), $route['params']);
114        $selected = $r->get_selected_route();
115        $this->assertEquals(':foo/:bar/mumble', $selected['path']);
116        $this->assertEquals(array('mumble route'), $selected['params']);
117
118        //  Match second route
119        $route = $r->find_route('a/b/c');
120        $this->assertEquals(':controller/:action/:id', $route['path']);
121        $this->assertNull($route['params']);
122        $selected = $r->get_selected_route();
123        $this->assertEquals(':controller/:action/:id', $selected['path']);
124        $this->assertNull($selected['params']);
125    }
126
127    /**
128     * Test route table with one regexp entry besides default
129     */
130    public function testRegexp_route() {
131        $r = new Router;
132        //  Build route table
133        $r->connect(':foo/:bar/\?(catalog|part)number=.*',
134                    array('number route'));
135        $r->connect(':controller/:action/:id', array('default route'));
136
137        //  Match first route
138        $route = $r->find_route('a/b/?catalognumber=17');
139        $this->assertEquals(':foo/:bar/\?(catalog|part)number=.*',
140                            $route['path']);
141        $this->assertEquals(array('number route'), $route['params']);
142        $route = $r->find_route('a/b/?partnumber=123-456');
143        $this->assertEquals(':foo/:bar/\?(catalog|part)number=.*',
144                            $route['path']);
145        $this->assertEquals(array('number route'), $route['params']);
146        $route = $r->find_route('a/b/?personnumber=156');
147        $this->assertEquals(':controller/:action/:id', $route['path']);
148        $this->assertEquals(array('default route'), $route['params']);
149    }
150
151    /**
152     * Test route table with route with empty path
153     */
154    public function testEmpty_route() {
155        $r = new Router;
156        //  Build route table with only an empty path
157        $r->connect('', array('empty route'));
158        $route = $r->find_route('');
159        $this->assertEquals('', $route['path']);
160        $this->assertEquals(array('empty route'), $route['params']);
161        //  This route shouldn't match anything
162        $route = $r->find_route('mumble/foo');
163        $this->assertNull($route);
164    }
165}
166
167// Call RouterTest::main() if this source file is executed directly.
168if (PHPUnit2_MAIN_METHOD == "RouterTest::main") {
169    RouterTest::main();
170}
171
172// -- set Emacs parameters --
173// Local variables:
174// tab-width: 4
175// c-basic-offset: 4
176// c-hanging-comment-ender-p: nil
177// indent-tabs-mode: nil
178// End:
179?>
Note: See TracBrowser for help on using the browser.