Skip to content

Commit

Permalink
v0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
atanvarno69 committed Mar 15, 2017
1 parent a0a7d2d commit 13e1ded
Show file tree
Hide file tree
Showing 11 changed files with 567 additions and 8 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ php:
- 7.0
- 7.1

before_install:
- echo "extension = apcu.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini
- echo "apc.enable_cli = 1" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini

before_script:
- travis_retry composer install --no-interaction --prefer-source

Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
"nikic/fast-route": "^1.2.0"
},
"require-dev": {
"phpunit/phpunit": "^6.0.8"
"phpunit/phpunit": "^6.0.8",
"http-interop/http-factory-diactoros": "0.2.0",
"atanvarno/cache-apcu": "^0.1.1"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion src/CachedRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class CachedRouter extends SimpleRouter

public function __construct(
CacheInterface $cache,
string $cacheKey,
string $cacheKey = 'routerData',
$driver = Router::GROUP_COUNT,
array $routes = [],
bool $cacheDisabled = false
Expand Down
4 changes: 2 additions & 2 deletions src/Exception/MethodNotAllowedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ class MethodNotAllowedException extends UnexpectedValueException
public function __construct(array $allowed, string $actual)
{
$this->allowed = $allowed;
$msg = sprintf('$s is not allowed for this route', $actual);
parent::__construct($msg, 405);
$msg = sprintf('%s is not allowed for this route', $actual);
parent::__construct($msg);
}

/**
Expand Down
7 changes: 5 additions & 2 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
namespace Atanvarno\Router;

/** SPL use block. */
use Atanvarno\Router\Exception\MethodNotAllowedException;
use Atanvarno\Router\Exception\NotFoundException;
use InvalidArgumentException;

/** PSR-7 use block */
Expand All @@ -19,6 +17,11 @@
/** HTTP Message Utilities use block. */
use Fig\Http\Message\RequestMethodInterface;

/** Package use block. */
use Atanvarno\Router\Exception\{
MethodNotAllowedException, NotFoundException
};

/**
* Atanvarno\Router\Router
*
Expand Down
4 changes: 2 additions & 2 deletions src/SimpleRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

namespace Atanvarno\Router;


/** PSR-7 use block */
use Psr\Http\Message\RequestInterface;

Expand Down Expand Up @@ -48,6 +47,7 @@ public function __construct(
throw new InvalidArgumentException($msg);
}
$this->driver = $driver;
$this->routes = [];

// Ensure routes array is numerically indexed.
$routes = array_values($routes);
Expand Down Expand Up @@ -105,7 +105,7 @@ public function dispatch(RequestInterface $request)
throw new MethodNotAllowedException($result[1], $method);
default: // No break
case Dispatcher::FOUND:
return $result;
return [$result[1], $result[2]];
}
}

Expand Down
126 changes: 126 additions & 0 deletions tests/CachedRouterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php
/**
* @package Atanvarno\Router
* @author atanvarno69 <https://github.com/atanvarno69>
* @copyright 2017 atanvarno.com
* @license https://opensource.org/licenses/MIT The MIT License
*/

namespace Atanvarno\Router\Test;

/** PSR-16 use block. */
use Psr\SimpleCache\CacheInterface;

/** PSR-17 use block. */
use Http\Factory\Diactoros\{
RequestFactory, UriFactory
};

/** HTTP Message Utilities use block. */
use Fig\Http\Message\RequestMethodInterface;

/** PHP Unit use block. */
use PHPUnit\Framework\TestCase;

/** Package use block. */
use Atanvarno\Router\{
Router,
CachedRouter
};

/** Dependency use block. */
use Atanvarno\Cache\{
Apcu\APCuDriver, Cache
};

class CachedRouterTest extends TestCase
{
/** @var CacheInterface $cache */
private $cache;

private $request;

/** @var Router $router */
private $router;

public function setUp()
{
$this->cache = new Cache(new APCuDriver());
$this->router = new CachedRouter($this->cache, 'routerData');
$uri = (new UriFactory())->createUri('http://atanvarno.com/test/uri/');
$this->request = (new RequestFactory())->createRequest(
RequestMethodInterface::METHOD_HEAD, $uri
);
}

public function tearDown()
{
$this->cache->clear();
}

public function testImplementsInterface()
{
$this->assertInstanceOf(Router::class, $this->router);
}

public function testFirstRunPopulatesCache()
{
$this->assertFalse($this->cache->has('routerData'));
$this->router->add(
RequestMethodInterface::METHOD_HEAD,
'/{name}/uri',
'handler'
);
$result = $this->router->dispatch($this->request);
$expected = ['handler', ['name' => 'test']];
$this->assertSame($expected, $result);
$this->assertTrue($this->cache->has('routerData'));
}

public function testCachedDataIsUsed()
{
$data = [
0 => [],
1 => [
'HEAD' => [
0 => [
'regex' => '~^(?|/([^/]+)/uri)$~',
'routeMap' => [
2 => [
0 => 'handler',
1 => [
'name' => 'name'
],
],
],
],
],
],
];
$this->cache->set('routerData', $data);
$result = $this->router->dispatch($this->request);
$expected = ['handler', ['name' => 'test']];
$this->assertSame($expected, $result);
}

public function testCacheDisabledUsesSimpleRouter()
{
$router = new CachedRouter(
$this->cache,
'routerData',
Router::GROUP_COUNT,
[
[
RequestMethodInterface::METHOD_HEAD,
'/{name}/uri',
'handler',
],
],
true
);
$result = $router->dispatch($this->request);
$expected = ['handler', ['name' => 'test']];
$this->assertSame($expected, $result);
$this->assertFalse($this->cache->has('routerData'));
}
}
27 changes: 27 additions & 0 deletions tests/InvalidArgumentExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* @package Atanvarno\Router
* @author atanvarno69 <https://github.com/atanvarno69>
* @copyright 2017 atanvarno.com
* @license https://opensource.org/licenses/MIT The MIT License
*/

namespace Atanvarno\Router\Test;

/** SPL use block. */
use InvalidArgumentException as SplInvalidArgumentException;

/** PHP Unit use block. */
use PHPUnit\Framework\TestCase;

/** Package use block. */
use Atanvarno\Router\Exception\InvalidArgumentException;

class InvalidArgumentExceptionTest extends TestCase
{
public function testExtendsSplInvalidArgumentException()
{
$exception = new InvalidArgumentException();
$this->assertInstanceOf(SplInvalidArgumentException::class, $exception);
}
}
61 changes: 61 additions & 0 deletions tests/MethodNotAllowedExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/**
* @package Atanvarno\Router
* @author atanvarno69 <https://github.com/atanvarno69>
* @copyright 2017 atanvarno.com
* @license https://opensource.org/licenses/MIT The MIT License
*/

namespace Atanvarno\Router\Test;

/** SPL use block. */
use UnexpectedValueException;

/** HTTP Message Utilities use block. */
use Fig\Http\Message\RequestMethodInterface;

/** PHP Unit use block. */
use PHPUnit\Framework\TestCase;

/** Package use block. */
use Atanvarno\Router\Exception\MethodNotAllowedException;

class MethodNotAllowedExceptionTest extends TestCase
{
private $exception;

public function setUp()
{
$this->exception = new MethodNotAllowedException(
[
RequestMethodInterface::METHOD_GET,
RequestMethodInterface::METHOD_HEAD,
],
RequestMethodInterface::METHOD_POST
);
}

public function testExtendsOutOfBoundsException()
{
$this->assertInstanceOf(
UnexpectedValueException::class,
$this->exception
);
}

public function testCaught()
{
try {
throw $this->exception;
} catch (MethodNotAllowedException $caught) {
$this->assertInstanceOf(UnexpectedValueException::class, $caught);
$expectedMessage = 'POST is not allowed for this route';
$this->assertSame($expectedMessage, $caught->getMessage());
$expectedAllowed = [
RequestMethodInterface::METHOD_GET,
RequestMethodInterface::METHOD_HEAD
];
$this->assertSame($expectedAllowed, $caught->getAllowed());
}
}
}
27 changes: 27 additions & 0 deletions tests/NotFoundExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* @package Atanvarno\Router
* @author atanvarno69 <https://github.com/atanvarno69>
* @copyright 2017 atanvarno.com
* @license https://opensource.org/licenses/MIT The MIT License
*/

namespace Atanvarno\Router\Test;

/** SPL use block. */
use OutOfBoundsException;

/** PHP Unit use block. */
use PHPUnit\Framework\TestCase;

/** Package use block. */
use Atanvarno\Router\Exception\NotFoundException;

class NotFoundExceptionTest extends TestCase
{
public function testExtendsOutOfBoundsException()
{
$exception = new NotFoundException();
$this->assertInstanceOf(OutOfBoundsException::class, $exception);
}
}
Loading

0 comments on commit 13e1ded

Please sign in to comment.