44use PhpRouter \Route ;
55use PHPUnit_Framework_TestCase ;
66
7+ require 'Test.php ' ;
8+
79/**
810 * Class RouteTest
911 */
@@ -16,94 +18,87 @@ public function shouldCreateCorrectRouteObject()
1618 {
1719 $ route = new Route ('GET /test/page.html ' , 'A->index ' );
1820
21+ $ this ->assertEquals ('|^/test/page.html$| ' , $ route ->getPattern ());
1922 $ this ->assertEquals (['GET ' ], $ route ->getMethods ());
20- $ this ->assertEquals ('/test/page.html ' , $ route ->getUrl ());
2123 $ this ->assertEquals ('sync ' , $ route ->getType ());
24+ $ this ->assertEquals ('/test/page.html ' , $ route ->getUrl ());
2225 }
2326
2427 /**
2528 * @test
2629 */
27- public function shouldCreateRouteWithDefiniedNamedParam ()
30+ public function shouldCreateRouteWithNamedParam ()
2831 {
29- $ route = new Route ('GET|POST /some_page/@id ' , [ ' id ' => ' \d{2}\-\w{4} ' ], function ( $ param ){
30- return $ param [ ' id ' ] ;
31- });
32+ $ route = new Route ('GET|POST /some_page/@id ' , ' A->index ' );
33+ $ this -> assertEquals ( ' |^/some_page/([\w-]+)$| ' , $ route -> getPattern ()) ;
34+ }
3235
33- $ route ->parseParams ('/some_page/12-zaqw ' );
34- $ this ->assertEquals ('12-zaqw ' , $ route ->dispatch ());
36+ /**
37+ * @test
38+ */
39+ public function shouldCreateRouteWithNamedParamMatchToRegex ()
40+ {
41+ $ route = new Route ('GET|POST /some_page/@id ' , ['id ' => '[a-z]{2}\-[a-z]{4} ' ], 'A->index ' );
42+ $ this ->assertEquals ('|^/some_page/([a-z]{2}\-[a-z]{4})$| ' , $ route ->getPattern ());
3543 }
3644
3745 /**
3846 * @test
3947 */
40- public function shouldThrowWrongCallbackException ()
48+ public function shouldCallAndExecuteAnonymousFunction ()
4149 {
42- $ this ->setExpectedException ('Exception ' );
43- $ route = new Route ('GET /test.html ' , 'nothingSpecial ' );
44- $ route ->dispatch ();
50+ $ route = new Route ('GET /test/@data.html [ajax] ' , ['data ' => '[a-z]{2}:[a-z]{3} ' ], function ($ params ){
51+ return $ params ['data ' ];
52+ });
53+
54+ $ route ->parseParams ('/test/xx:www.html ' );
55+ $ this ->assertEquals ('xx:www ' , $ route ->dispatch ());
4556 }
4657
4758 /**
4859 * @test
4960 */
50- public function shouldThrowInvalidRouteException ()
61+ public function shouldCallSpecifiedCallback ()
5162 {
52- $ this ->setExpectedException ('Exception ' );
53- new Route ('/test.html ' , 'A->index ' );
63+ $ route = new Route ('GET /test/page/@number.html ' , '\PhpRouter\Test\Test->page ' );
64+
65+ $ route ->parseParams ('/test/page/42.html ' );
66+ $ this ->assertEquals (42 , $ route ->dispatch ());
5467 }
5568
5669 /**
5770 * @test
5871 */
59- public function shouldThrowInvalidTypeException ()
72+ public function shouldThrowWrongCallbackException ()
6073 {
6174 $ this ->setExpectedException ('Exception ' );
62- new Route ('GET /test.html [not_exists] ' , 'A->index ' );
75+ ( new Route ('GET /test.html ' , 'nothingSpecial ' ))-> dispatch ( );
6376 }
6477
6578 /**
6679 * @test
6780 */
68- public function shouldCallAndExecuteAnonymousFunction ()
81+ public function shouldThrowInvalidRouteException ()
6982 {
70- $ route = new Route ('GET /test.html [ajax] ' , function (){
71- echo 'XXX ' ;
72- });
73-
74- ob_start ();
75- $ route ->dispatch ();
76- $ result = ob_get_clean ();
77-
78- $ this ->assertEquals ('XXX ' , $ result );
83+ $ this ->setExpectedException ('Exception ' );
84+ new Route ('/test.html ' , 'A->index ' );
7985 }
8086
8187 /**
8288 * @test
8389 */
84- public function shouldThrowClasOrMethodNotFoundException ()
90+ public function shouldThrowInvalidTypeException ()
8591 {
8692 $ this ->setExpectedException ('Exception ' );
87- $ route = new Route ('GET /test/page.html ' , 'A->test ' );
88- $ route ->dispatch ();
93+ new Route ('GET /test.html [not_exists] ' , 'A->index ' );
8994 }
9095
9196 /**
9297 * @test
9398 */
94- public function shouldCallSpecifiedCallback ()
99+ public function shouldThrowClassOrMethodNotFoundException ()
95100 {
96- // $class = $this->getMockBuilder('Test')->setMethods(['login'])->getMock();
97- //
98- // $class->expects($this->any())
99- // ->method('login');
100- //
101- // $route = new Route('GET /test/page.html', 'Test->login');
102- //
103- // ob_start();
104- // $route->dispatch(['someData']);
105- // $result = ob_get_clean();
106- //
107- // $this->assertEquals('logged!', $result);
101+ $ this ->setExpectedException ('Exception ' );
102+ (new Route ('GET /test/page.html ' , 'A->test ' ))->dispatch ();
108103 }
109104}
0 commit comments