-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added route builder, extra tests, auto formatting.
- Loading branch information
Showing
12 changed files
with
187 additions
and
1 deletion.
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,3 @@ | ||
/tests export-ignore | ||
/phpunit.xml export-ignore | ||
/.gitattributes export-ignore |
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
1 change: 1 addition & 0 deletions
1
src/Briedis/ApiBuilder/Exceptions/InvalidParameterTypeException.php
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
<?php | ||
|
||
namespace Briedis\ApiBuilder\Exceptions; | ||
|
||
use Briedis\ApiBuilder\Items\BaseItem; | ||
|
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
<?php | ||
|
||
namespace Briedis\ApiBuilder; | ||
|
||
|
||
|
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,50 @@ | ||
<?php | ||
|
||
namespace Briedis\ApiBuilder; | ||
|
||
use Closure; | ||
use Illuminate\Contracts\Routing\Registrar; | ||
|
||
/** | ||
* Class to help build routes | ||
*/ | ||
class RouteBuilder | ||
{ | ||
/** @var Registrar */ | ||
private $registrar; | ||
|
||
/** | ||
* @param Registrar $registrar | ||
*/ | ||
public function __construct(Registrar $registrar) | ||
{ | ||
$this->registrar = $registrar; | ||
} | ||
|
||
/** | ||
* Generate a route for a given method | ||
* @param Method $method | ||
* @param array|Closure|null|string $routeAction | ||
* @return self | ||
*/ | ||
public function add(Method $method, $routeAction) | ||
{ | ||
$mapping = [ | ||
'GET' => 'get', | ||
'POST' => 'post', | ||
'PUT' => 'put', | ||
'DELETE' => 'delete', | ||
'PATCH' => 'patch', | ||
]; | ||
|
||
if (!isset($mapping[$method::METHOD])) { | ||
throw new \InvalidArgumentException('Unknown method type: ' . $method::METHOD); | ||
} | ||
|
||
$routeMethod = $mapping[$method::METHOD]; | ||
|
||
call_user_func([$this->registrar, $routeMethod], $method::URI, $routeAction); | ||
|
||
return $this; | ||
} | ||
} |
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
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,57 @@ | ||
<?php | ||
|
||
namespace Briedis\ApiBuilder\Tests; | ||
|
||
use Briedis\ApiBuilder\RouteBuilder; | ||
use Briedis\ApiBuilder\Tests\Stubs\GetMethodStub; | ||
use Briedis\ApiBuilder\Tests\Stubs\InvalidMethodStub; | ||
use Briedis\ApiBuilder\Tests\Stubs\PostMethodStub; | ||
use Illuminate\Contracts\Routing\Registrar; | ||
use Mockery; | ||
use Mockery\Mock; | ||
use Mockery\MockInterface; | ||
use PHPUnit_Framework_TestCase; | ||
|
||
class RouteBuilderTest extends PHPUnit_Framework_TestCase | ||
{ | ||
/** @var Registrar|MockInterface|Mock */ | ||
private $mock; | ||
|
||
/** @var RouteBuilder */ | ||
private $builder; | ||
|
||
|
||
public static function tearDownAfterClass() | ||
{ | ||
Mockery::close(); | ||
parent::tearDownAfterClass(); | ||
} | ||
|
||
protected function setUp() | ||
{ | ||
parent::setUp(); | ||
$this->mock = Mockery::mock(Registrar::class); | ||
$this->builder = new RouteBuilder($this->mock); | ||
} | ||
|
||
public function testGetMethod() | ||
{ | ||
$method = new GetMethodStub; | ||
$this->mock->shouldReceive('get')->with($method::URI, 'action')->once(); | ||
$this->builder->add($method, 'action'); | ||
} | ||
|
||
public function testPostMethod() | ||
{ | ||
$method = new PostMethodStub; | ||
$this->mock->shouldReceive('post')->with($method::URI, 'controller@method')->once(); | ||
$this->builder->add($method, 'controller@method'); | ||
} | ||
|
||
public function testInvalidMethod() | ||
{ | ||
$method = new InvalidMethodStub; | ||
self::setExpectedException(\InvalidArgumentException::class); | ||
$this->builder->add($method, 'controller@method'); | ||
} | ||
} |
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
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,20 @@ | ||
<?php | ||
|
||
namespace Briedis\ApiBuilder\Tests\Stubs; | ||
|
||
use Briedis\ApiBuilder\Method; | ||
|
||
class GetMethodStub extends Method | ||
{ | ||
const METHOD = 'GET'; | ||
|
||
const URI = 'get-uri'; | ||
|
||
public function getRequest() | ||
{ | ||
} | ||
|
||
public function getResponse() | ||
{ | ||
} | ||
} |
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,20 @@ | ||
<?php | ||
|
||
namespace Briedis\ApiBuilder\Tests\Stubs; | ||
|
||
use Briedis\ApiBuilder\Method; | ||
|
||
class InvalidMethodStub extends Method | ||
{ | ||
const METHOD = 'UNKNOWN-METHOD'; | ||
|
||
const URI = 'doesnt-matter'; | ||
|
||
public function getRequest() | ||
{ | ||
} | ||
|
||
public function getResponse() | ||
{ | ||
} | ||
} |
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,20 @@ | ||
<?php | ||
|
||
namespace Briedis\ApiBuilder\Tests\Stubs; | ||
|
||
use Briedis\ApiBuilder\Method; | ||
|
||
class PostMethodStub extends Method | ||
{ | ||
const METHOD = 'POST'; | ||
|
||
const URI = 'post-uri'; | ||
|
||
public function getRequest() | ||
{ | ||
} | ||
|
||
public function getResponse() | ||
{ | ||
} | ||
} |
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