-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
class Routing { | ||
public $controllerClassName; | ||
public $methodName; | ||
public $parameter; | ||
|
||
/** | ||
* Processes a URL for MVC routing | ||
* | ||
* Parses a URL into a controller part, a view part, and a parameter part. | ||
* All parts are optional. The controller and view parts have the default | ||
* values of Home and Index. | ||
* | ||
* @param string $url The path part of a URL - after the initial / | ||
* @return object | ||
*/ | ||
public function handle($url) { | ||
$parts = explode('/', $url); | ||
|
||
$controllerName = @$parts[0]; | ||
$methodName = @$parts[1]; | ||
$parameter = @$parts[2]; | ||
|
||
if (!$controllerName) $controllerName = 'Home'; | ||
if (!$methodName) $methodName = 'Index'; | ||
|
||
return (object) [ | ||
'controllerClassName' => $controllerName . 'Controller', | ||
'methodName' => $methodName, | ||
'parameter' => $parameter | ||
]; | ||
} | ||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
class RoutingTests { | ||
/** | ||
* @test | ||
*/ | ||
public function CheckAllDefaults() { | ||
// Arrange | ||
$routing = new Routing(); | ||
|
||
// Act | ||
$route = $routing->handle(''); | ||
|
||
// Assert | ||
The($route->controllerClassName)->shouldEqual('HomeController'); | ||
The($route->methodName)->shouldEqual('Index'); | ||
The($route->parameter)->shouldNotBeSet(); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function CheckDefaultMethodNameAndParameter() { | ||
// Arrange | ||
$routing = new Routing(); | ||
|
||
// Act | ||
$route = $routing->handle('Articles'); | ||
|
||
// Assert | ||
The($route->controllerClassName)->shouldEqual('ArticlesController'); | ||
The($route->methodName)->shouldEqual('Index'); | ||
The($route->parameter)->shouldNotBeSet(); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function CheckDefaultParameter() { | ||
// Arrange | ||
$routing = new Routing(); | ||
|
||
// Act | ||
$route = $routing->handle('Categories/List'); | ||
|
||
// Assert | ||
The($route->controllerClassName)->shouldEqual('CategoriesController'); | ||
The($route->methodName)->shouldEqual('List'); | ||
The($route->parameter)->shouldNotBeSet(); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function CheckNoDefaults() { | ||
// Arrange | ||
$routing = new Routing(); | ||
|
||
// Act | ||
$route = $routing->handle('Products/Item/123x'); | ||
|
||
// Assert | ||
The($route->controllerClassName)->shouldEqual('ProductsController'); | ||
The($route->methodName)->shouldEqual('Item'); | ||
The($route->parameter)->shouldEqual('123x'); | ||
} | ||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- Komodo Project File - DO NOT EDIT --> | ||
<project id="6859751f-864b-4e0e-9c36-697664ad1b5e" kpf_version="5" name="mvc.komodoproject"> | ||
<preference-set idref="6859751f-864b-4e0e-9c36-697664ad1b5e" id="project" preftype="project"> | ||
<long id="prefs_version">1</long> | ||
</preference-set> | ||
</project> |