Skip to content

Commit

Permalink
Added route builder, extra tests, auto formatting.
Browse files Browse the repository at this point in the history
  • Loading branch information
briedis committed Mar 15, 2017
1 parent bf21adb commit 6ab2980
Show file tree
Hide file tree
Showing 12 changed files with 187 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/tests export-ignore
/phpunit.xml export-ignore
/.gitattributes export-ignore
8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
],
"require": {
"php": ">=5.4.0",
"illuminate/support": "~4.0|~5.0"
"illuminate/support": "~4.0|~5.0",
"mockery/mockery": "^0.9.9"
},
"require-dev": {
"phpunit/phpunit": "~4.0"
Expand All @@ -19,5 +20,10 @@
"Briedis\\ApiBuilder": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Briedis\\ApiBuilder\\Tests\\": "tests/"
}
},
"minimum-stability": "stable"
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Briedis\ApiBuilder\Exceptions;

use Briedis\ApiBuilder\Items\BaseItem;
Expand Down
1 change: 1 addition & 0 deletions src/Briedis/ApiBuilder/Presenter.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Briedis\ApiBuilder;


Expand Down
50 changes: 50 additions & 0 deletions src/Briedis/ApiBuilder/RouteBuilder.php
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;
}
}
2 changes: 2 additions & 0 deletions tests/MultiDepthValidatorTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<?php

namespace Briedis\ApiBuilder\Tests;

use Briedis\ApiBuilder\Exceptions\InvalidStructureException;
use Briedis\ApiBuilder\Items\DecimalItem;
use Briedis\ApiBuilder\StructureBuilder as SB;
use Briedis\ApiBuilder\StructureValidator;
use PHPUnit_Framework_TestCase;

class MultiDepthValidatorTest extends PHPUnit_Framework_TestCase
{
Expand Down
57 changes: 57 additions & 0 deletions tests/RouteBuilderTest.php
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');
}
}
2 changes: 2 additions & 0 deletions tests/SingleDepthValidatorTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?php

namespace Briedis\ApiBuilder\Tests;

use Briedis\ApiBuilder\Exceptions\InvalidStructureException;
use Briedis\ApiBuilder\Items\DecimalItem;
use Briedis\ApiBuilder\Items\IntegerItem;
use Briedis\ApiBuilder\StructureBuilder;
use Briedis\ApiBuilder\StructureValidator;
use PHPUnit_Framework_TestCase;

class SingleDepthValidatorTest extends PHPUnit_Framework_TestCase
{
Expand Down
20 changes: 20 additions & 0 deletions tests/Stubs/GetMethodStub.php
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()
{
}
}
20 changes: 20 additions & 0 deletions tests/Stubs/InvalidMethodStub.php
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()
{
}
}
20 changes: 20 additions & 0 deletions tests/Stubs/PostMethodStub.php
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()
{
}
}
4 changes: 4 additions & 0 deletions tests/TypeTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
<?php

namespace Briedis\ApiBuilder\Tests;

use Briedis\ApiBuilder\Items\BaseItem;
use Briedis\ApiBuilder\Items\BooleanItem;
use Briedis\ApiBuilder\Items\DecimalItem;
use Briedis\ApiBuilder\Items\IntegerItem;
use Briedis\ApiBuilder\Items\MixedItem;
use Briedis\ApiBuilder\Items\StringItem;
use PHPUnit_Framework_TestCase;
use stdClass;

class TypeTest extends PHPUnit_Framework_TestCase
{
Expand Down

0 comments on commit 6ab2980

Please sign in to comment.