-
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.
- Loading branch information
1 parent
a0a7d2d
commit 13e1ded
Showing
11 changed files
with
567 additions
and
8 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
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
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
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,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')); | ||
} | ||
} |
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,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); | ||
} | ||
} |
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,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()); | ||
} | ||
} | ||
} |
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,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); | ||
} | ||
} |
Oops, something went wrong.