Skip to content
This repository has been archived by the owner on Nov 8, 2020. It is now read-only.

Commit

Permalink
Removed INI dependency:
Browse files Browse the repository at this point in the history
  • Loading branch information
klapuch committed Jul 18, 2017
1 parent 7bc6386 commit 35cffc5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 39 deletions.
12 changes: 5 additions & 7 deletions Core/HttpRoutes.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,21 @@
declare(strict_types = 1);
namespace Klapuch\Routing;

use Klapuch\Ini;
use Klapuch\Uri;

/**
* Routes suitable for HTTP protocol
*/
final class HttpRoutes implements Routes {
private $ini;
private $choices;

public function __construct(Ini\Source $ini) {
$this->ini = $ini;
public function __construct(array $choices) {
$this->choices = $choices;
}

public function match(Uri\Uri $uri): Route {
$choices = $this->ini->read();
$matches = array_filter(
$this->patterns($choices),
$this->patterns($this->choices),
function(string $source) use ($uri): bool {
return (bool) preg_match(
sprintf('~^%s$~iu', $source),
Expand All @@ -29,7 +27,7 @@ function(string $source) use ($uri): bool {
if ($matches) {
return new HttpRoute(
(string) key($matches),
$choices[key($matches)],
$this->choices[key($matches)],
$uri
);
}
Expand Down
48 changes: 17 additions & 31 deletions Tests/Unit/HttpRoutes.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ declare(strict_types = 1);
*/
namespace Klapuch\Routing\Unit;

use Klapuch\Ini;
use Klapuch\Routing;
use Klapuch\Uri;
use Tester;
Expand All @@ -17,7 +16,7 @@ require __DIR__ . '/../bootstrap.php';
final class HttpRoutes extends Tester\TestCase {
public function testExactMatch() {
[$destination, $source] = ['Foo/default', '/foo'];
$routes = new Routing\HttpRoutes(new Ini\FakeSource([$source => $destination]));
$routes = new Routing\HttpRoutes([$source => $destination]);
$uri = new Uri\FakeUri(null, $source);
Assert::equal(
new Routing\HttpRoute($source, $destination, $uri),
Expand All @@ -29,13 +28,12 @@ final class HttpRoutes extends Tester\TestCase {
* @throws \UnexpectedValueException HTTP route for "/foo" does not exist
*/
public function testStrictTypeMatching() {
$list = new Ini\FakeSource(['Foo/default' => true]);
(new Routing\HttpRoutes($list))->match(new Uri\FakeUri(null, '/foo'));
(new Routing\HttpRoutes(['Foo/default' => true]))->match(new Uri\FakeUri(null, '/foo'));
}

public function testStringMatchOnNumber() {
[$destination, $source] = ['5', '/foo'];
$routes = new Routing\HttpRoutes(new Ini\FakeSource([$source => $destination]));
$routes = new Routing\HttpRoutes([$source => $destination]);
$uri = new Uri\FakeUri(null, $source);
Assert::equal(
new Routing\HttpRoute($source, $destination, $uri),
Expand All @@ -45,7 +43,7 @@ final class HttpRoutes extends Tester\TestCase {

public function testAllowingFalseyMatch() {
[$destination, $source] = ['0', '/foo'];
$routes = new Routing\HttpRoutes(new Ini\FakeSource([$source => $destination]));
$routes = new Routing\HttpRoutes([$source => $destination]);
$uri = new Uri\FakeUri(null, $source);
Assert::equal(
new Routing\HttpRoute($source, $destination, $uri),
Expand All @@ -57,12 +55,11 @@ final class HttpRoutes extends Tester\TestCase {
* @throws \UnexpectedValueException HTTP route for "" does not exist
*/
public function testThrowingOnNoMatch() {
(new Routing\HttpRoutes(new Ini\FakeSource([])))->match(new Uri\FakeUri(null, ''));
(new Routing\HttpRoutes([]))->match(new Uri\FakeUri(null, ''));
}

public function testCaseInsensitiveMatch() {
$ini = new Ini\FakeSource(['/foo' => 'Foo/default', '/BaR' => 'Bar/default']);
$routes = new Routing\HttpRoutes($ini);
$routes = new Routing\HttpRoutes(['/foo' => 'Foo/default', '/BaR' => 'Bar/default']);
$fooUri = new Uri\FakeUri(null, '/foo');
$barUri = new Uri\FakeUri(null, '/BaR');
Assert::equal(
Expand All @@ -76,8 +73,7 @@ final class HttpRoutes extends Tester\TestCase {
}

public function testMultibyteCaseInsensitiveMatch() {
$ini = new Ini\FakeSource(['/foó' => 'Foo/default', '/BaŘ' => 'Bar/default']);
$routes = new Routing\HttpRoutes($ini);
$routes = new Routing\HttpRoutes(['/foó' => 'Foo/default', '/BaŘ' => 'Bar/default']);
$fooUri = new Uri\FakeUri(null, '/foó');
$barUri = new Uri\FakeUri(null, '/BaŘ');
Assert::equal(
Expand All @@ -92,8 +88,7 @@ final class HttpRoutes extends Tester\TestCase {

public function testMultiplePossibilitiesWithLastMatch() {
[$destination, $source] = ['Bar/default', '/foo'];
$ini = new Ini\FakeSource([$source => 'Foo/default', $source => $destination]);
$routes = new Routing\HttpRoutes($ini);
$routes = new Routing\HttpRoutes([$source => 'Foo/default', $source => $destination]);
$uri = new Uri\FakeUri(null, $source);
Assert::equal(
new Routing\HttpRoute($source, $destination, $uri),
Expand All @@ -103,8 +98,7 @@ final class HttpRoutes extends Tester\TestCase {

public function testMatchWithSinglePlaceholder() {
[$destination, $source] = ['Foo/default', '/books/{id}'];
$ini = new Ini\FakeSource([$source => $destination]);
$routes = new Routing\HttpRoutes($ini);
$routes = new Routing\HttpRoutes([$source => $destination]);
$uri = new Uri\FakeUri(null, '/books/1');
Assert::equal(
new Routing\HttpRoute($source, $destination, $uri),
Expand All @@ -117,8 +111,7 @@ final class HttpRoutes extends Tester\TestCase {
*/
public function testThrowingOnPlaceholderAsNestedParameter() {
[$destination, $source] = ['Foo/default', '/books/{id}'];
$ini = new Ini\FakeSource([$source => $destination]);
$routes = new Routing\HttpRoutes($ini);
$routes = new Routing\HttpRoutes([$source => $destination]);
$routes->match(new Uri\FakeUri(null, '/books/foo/bar'));
}

Expand All @@ -127,8 +120,7 @@ final class HttpRoutes extends Tester\TestCase {
*/
public function testThrowingOnSomePlaceholderMatch() {
[$destination, $source] = ['Foo/default', '/books/{id}'];
$ini = new Ini\FakeSource([$source => $destination]);
$routes = new Routing\HttpRoutes($ini);
$routes = new Routing\HttpRoutes([$source => $destination]);
$routes->match(new Uri\FakeUri(null, 'blabla/books/foo'));
}

Expand All @@ -137,15 +129,13 @@ final class HttpRoutes extends Tester\TestCase {
*/
public function testThrowingOnDirectPlaceholderParameter() {
[$destination, $source] = ['Foo/default', '/books/{id}'];
$ini = new Ini\FakeSource([$source => $destination]);
$routes = new Routing\HttpRoutes($ini);
$routes = new Routing\HttpRoutes([$source => $destination]);
$routes->match(new Uri\FakeUri(null, $source));
}

public function testMatchMultipleDifferentPlaceholders() {
[$destination, $source] = ['Foo/default', '/books/{id}/foo/{key}'];
$ini = new Ini\FakeSource([$source => $destination]);
$routes = new Routing\HttpRoutes($ini);
$routes = new Routing\HttpRoutes([$source => $destination]);
$uri = new Uri\FakeUri(null, '/books/1/foo/nwm');
Assert::equal(
new Routing\HttpRoute($source, $destination, $uri),
Expand All @@ -155,8 +145,7 @@ final class HttpRoutes extends Tester\TestCase {

public function testMatchMultipleDifferentPlaceholdersInRow() {
[$destination, $source] = ['Foo/default', '/books/{id}/{key}'];
$ini = new Ini\FakeSource([$source => $destination]);
$routes = new Routing\HttpRoutes($ini);
$routes = new Routing\HttpRoutes([$source => $destination]);
$uri = new Uri\FakeUri(null, '/books/1/nwm');
Assert::equal(
new Routing\HttpRoute($source, $destination, $uri),
Expand All @@ -166,8 +155,7 @@ final class HttpRoutes extends Tester\TestCase {

public function testMatchMultipleSamePlaceholdersInRow() {
[$destination, $source] = ['Foo/default', '/books/{id}/{id}'];
$ini = new Ini\FakeSource([$source => $destination]);
$routes = new Routing\HttpRoutes($ini);
$routes = new Routing\HttpRoutes([$source => $destination]);
$uri = new Uri\FakeUri(null, '/books/1/5');
Assert::equal(
new Routing\HttpRoute($source, $destination, $uri),
Expand All @@ -177,8 +165,7 @@ final class HttpRoutes extends Tester\TestCase {

public function testMatchWithRegex() {
[$destination, $source] = ['Foo/default', '/books/{id \d}/{key \w+}'];
$ini = new Ini\FakeSource([$source => $destination]);
$routes = new Routing\HttpRoutes($ini);
$routes = new Routing\HttpRoutes([$source => $destination]);
$uri = new Uri\FakeUri(null, '/books/1/bar');
Assert::equal(
new Routing\HttpRoute($source, $destination, $uri),
Expand All @@ -191,8 +178,7 @@ final class HttpRoutes extends Tester\TestCase {
*/
public function testThrowingOnNoRegexMatch() {
[$destination, $source] = ['Foo/default', '/books/{id \d}'];
$ini = new Ini\FakeSource([$source => $destination]);
$routes = new Routing\HttpRoutes($ini);
$routes = new Routing\HttpRoutes([$source => $destination]);
$routes->match(new Uri\FakeUri(null, '/books/10'));
}
}
Expand Down
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
],
"require": {
"php": ">=7.1",
"klapuch/ini": "^2.0",
"klapuch/uri": "^2.0"
},
"require-dev": {
Expand Down

0 comments on commit 35cffc5

Please sign in to comment.