Skip to content

Commit

Permalink
Add resource route
Browse files Browse the repository at this point in the history
Fix Arrays::firstOrNull()
  • Loading branch information
piotrooo committed Sep 26, 2013
1 parent fa087bb commit fe5d73b
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 2 deletions.
48 changes: 47 additions & 1 deletion lib/Ouzo/Routing/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,56 @@ public static function any($uri, $action)
self::_addRoute($methods, $uri, $action);
}

public static function resource($action)
{
self::_addRoute('GET',
self::_createRouteUri($action),
self::_createRouteAction($action, 'index')
);
self::_addRoute('GET',
self::_createRouteUri($action, '/new'),
self::_createRouteAction($action, 'new')
);
self::_addRoute('POST',
self::_createRouteUri($action),
self::_createRouteAction($action, 'create')
);
self::_addRoute('GET',
self::_createRouteUri($action, '/:id'),
self::_createRouteAction($action, 'show')
);
self::_addRoute('GET',
self::_createRouteUri($action, '/:id/edit'),
self::_createRouteAction($action, 'edit')
);
self::_addRoute('PUT',
self::_createRouteUri($action, '/:id'),
self::_createRouteAction($action, 'update')
);
self::_addRoute('PATCH',
self::_createRouteUri($action, '/:id'),
self::_createRouteAction($action, 'update')
);
self::_addRoute('DELETE',
self::_createRouteUri($action, '/:id'),
self::_createRouteAction($action, 'delete')
);
}

private static function _createRouteUri($action, $suffix = '')
{
return '/' . $action . $suffix;
}

private static function _createRouteAction($controller, $action)
{
return $controller . '#' . $action;
}

private static function _addRoute($method, $uri, $action)
{
if (self::_existRouteRule($method, $uri)) {
throw new InvalidArgumentException('Route roule for method ' . $method . ' and URI "' . $uri . '" already exists');
throw new InvalidArgumentException('Route rule for method ' . $method . ' and URI "' . $uri . '" already exists');
}
self::$routes[] = new RouteRule($method, $uri, $action);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Ouzo/Utilities/Arrays.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public static function last($elements)

public static function firstOrNull($object)
{
return empty($object) ? null : $object[0];
return empty($object) ? null : self::first($object);
}

public static function getValue($array, $key, $default = null)
Expand Down
16 changes: 16 additions & 0 deletions test/lib/Ouzo/Routing/RouteTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Ouzo\Routing\Route;
use Ouzo\Tests\Assert;
use Ouzo\Utilities\Arrays;

class RouteTest extends PHPUnit_Framework_TestCase
Expand Down Expand Up @@ -147,4 +148,19 @@ public function shouldDefineMultipleRulesWithDifferentTypes()
//then
$this->assertCount(2, $routes);
}

/**
* @test
*/
public function shouldCreateRouteForResource()
{
//given
Route::resource('users');

//when
$routes = Route::getRoutes();

//then
Assert::thatArray($routes)->hasSize(8);
}
}
32 changes: 32 additions & 0 deletions test/lib/Ouzo/Routing/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,24 @@ public function shouldFindRouteAnyForController($method)
$this->assertNull($rule->getAction());
}

/**
* @test
* @dataProvider requestRestMethods
*/
public function shouldFindRouteResource($method, $uri)
{
//given
Route::resource('albums');
$router = $this->_createRouter($method, $uri);

//when
$rule = $router->findRoute();

//then
$this->assertEquals($method, $rule->getMethod());
$this->assertEquals('albums', $rule->getController());
}

public function requestMethods()
{
return array(
Expand All @@ -187,6 +205,20 @@ public function requestMethods()
);
}

public function requestRestMethods()
{
return array(
array('GET', '/albums'),
array('GET', '/albums/new'),
array('POST', '/albums'),
array('GET', '/albums/12'),
array('GET', '/albums/12/edit'),
array('PUT', '/albums/12'),
array('PATCH', '/albums/12'),
array('DELETE', '/albums/12')
);
}

private function _createRouter($method, $uri)
{
$_SERVER['REQUEST_METHOD'] = $method;
Expand Down

0 comments on commit fe5d73b

Please sign in to comment.