Changeset 161 for trunk/trax/test

Show
Ignore:
Timestamp:
03/02/06 17:20:34 (6 years ago)
Author:
haas
Message:

Finish Router class doc, regression test

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/trax/test/RouterTest.php

    r159 r161  
    6565 
    6666    /** 
    67      * @todo Implement testGet_selected_route(). 
     67     *  Test build_route_regexp(). 
    6868     */ 
    69     public function testGet_selected_route() { 
    70         // Remove the following line when you implement this test. 
    71         throw new PHPUnit2_Framework_IncompleteTestError; 
     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$'); 
    7287    } 
    7388 
    7489    /** 
    75      * @todo Implement testConnect(). 
     90     * Test default route table 
    7691     */ 
    77     public function testConnect() { 
    78         // Remove the following line when you implement this test. 
    79         throw new PHPUnit2_Framework_IncompleteTestError; 
     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']); 
    8098    } 
    8199 
    82100    /** 
    83      * @todo Implement testFind_route(). 
     101     * Test route table with one simple entry besides default 
    84102     */ 
    85     public function testFind_route() { 
    86         // Remove the following line when you implement this test. 
    87         throw new PHPUnit2_Framework_IncompleteTestError; 
     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']); 
    88125    } 
    89126 
    90127    /** 
    91      * @todo Implement testBuild_route_regexp(). 
     128     * Test route table with one regexp entry besides default 
    92129     */ 
    93     public function testBuild_route_regexp() { 
    94         // Remove the following line when you implement this test. 
    95         throw new PHPUnit2_Framework_IncompleteTestError; 
     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); 
    96164    } 
    97165}