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

Commit

Permalink
Without messing with method
Browse files Browse the repository at this point in the history
  • Loading branch information
klapuch committed Oct 19, 2017
1 parent a3bb020 commit 4616296
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 49 deletions.
8 changes: 1 addition & 7 deletions Core/DefaultRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,7 @@ private function path(string $source, Uri\Uri $uri): array {
}

private function query(string $source, Uri\Uri $uri): array {
parse_str(
(string) parse_url(
preg_replace('~\s+\[.+\]$~', '', $source),
PHP_URL_QUERY
),
$query
);
parse_str((string) parse_url($source, PHP_URL_QUERY), $query);
return $uri->query() + array_map(
function(string $part): string {
return substr($part, 1, -1);
Expand Down
21 changes: 19 additions & 2 deletions Core/HttpMethodRoutes.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,31 @@ public function __construct(Routes $origin, string $method) {
}

public function matches(): array {
return array_intersect_key(
$matches = array_intersect_key(
$this->origin->matches(),
array_flip(
preg_grep(
sprintf('~\s+\[%s\]$~i', $this->method),
$this->pattern($this->method),
array_keys($this->origin->matches())
)
)
);
return array_combine(
preg_replace(
$this->pattern($this->method),
'',
array_keys($matches)
),
$matches
);
}

/**
* Pattern matching method
* @param string $method
* @return string
*/
private function pattern(string $method): string {
return sprintf('~\s+\[%s\]$~i', $method);
}
}
5 changes: 2 additions & 3 deletions Core/PathRoutes.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ private function patterns(array $matches): array {
* @return string
*/
private function filling(string $match): string {
preg_match('~\s(\[\w+\])~', $match, $method);
return implode(
'/',
array_map(
Expand All @@ -70,10 +69,10 @@ private function filling(string $match): string {
function(string $part): string {
return parse_url($part, PHP_URL_PATH);
},
explode('/', str_replace(current($method), '', $match))
explode('/', $match)
)
)
) . current($method);
);
}

/**
Expand Down
5 changes: 1 addition & 4 deletions Core/QueryRoutes.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ public function matches(): array {
return array_filter(
$this->origin->matches(),
function(string $match): bool {
parse_str(
(string) parse_url(preg_replace('~\s(\[\w+\])~', '', $match), PHP_URL_QUERY),
$query
);
parse_str((string) parse_url($match, PHP_URL_QUERY), $query);
return $this->includes($this->uri, $query, $this->defaults($query));
},
ARRAY_FILTER_USE_KEY
Expand Down
2 changes: 1 addition & 1 deletion Tests/Unit/DefaultRoute.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ final class DefaultRoute extends Tester\TestCase {
Assert::same(
['page' => '1', 'name' => 'dom', 'position' => 'developer'],
(new Routing\DefaultRoute(
'/books/{name}/{position}?page=1 [GET]',
'/books/{name}/{position}?page=1',
'Foo/bar',
new Uri\FakeUri(null, '/books/dom/developer', [])
))->parameters()
Expand Down
6 changes: 3 additions & 3 deletions Tests/Unit/HttpMethodRoutes.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require __DIR__ . '/../bootstrap.php';
final class HttpMethodRoutes extends Tester\TestCase {
public function testFilteringByMethods() {
Assert::same(
['foo [GET]' => 'a'],
['foo' => 'a'],
(new Routing\HttpMethodRoutes(
new Routing\FakeRoutes(
[
Expand All @@ -30,14 +30,14 @@ final class HttpMethodRoutes extends Tester\TestCase {

public function testFilteringByCaseInsensitiveMethods() {
Assert::same(
['foo [post]' => 'a'],
['foo' => 'a'],
(new Routing\HttpMethodRoutes(
new Routing\FakeRoutes(['foo [post]' => 'a']),
'POST'
))->matches()
);
Assert::same(
['foo [POST]' => 'a'],
['foo' => 'a'],
(new Routing\HttpMethodRoutes(
new Routing\FakeRoutes(['foo [POST]' => 'a']),
'post'
Expand Down
28 changes: 14 additions & 14 deletions Tests/Unit/PathRoutes.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ final class PathRoutes extends Tester\TestCase {

public function testCaseInsensitiveMatch() {
Assert::same(
['foo/{name \d+} [GET]' => 'a'],
['foo/{name \d+}' => 'a'],
(new Routing\PathRoutes(
new Routing\FakeRoutes(['foo/{name \d+} [GET]' => 'a']),
new Routing\FakeRoutes(['foo/{name \d+}' => 'a']),
new Uri\FakeUri(null, 'FOO/123')
))->matches()
);
Assert::same(
['FOO/{name \d+} [GET]' => 'a'],
['FOO/{name \d+}' => 'a'],
(new Routing\PathRoutes(
new Routing\FakeRoutes(['FOO/{name \d+} [GET]' => 'a']),
new Routing\FakeRoutes(['FOO/{name \d+}' => 'a']),
new Uri\FakeUri(null, 'foo/123')
))->matches()
);
Expand All @@ -45,20 +45,20 @@ final class PathRoutes extends Tester\TestCase {
Assert::same(
[],
(new Routing\PathRoutes(
new Routing\FakeRoutes(['foo/{name \d+} [GET]' => 'a']),
new Routing\FakeRoutes(['foo/{name \d+}' => 'a']),
new Uri\FakeUri(null, 'foo/abc')
))->matches()
);
}

public function testFilteringOnlyMatched() {
Assert::same(
['foo/{name \d+} [GET]' => 'a'],
['foo/{name \d+}' => 'a'],
(new Routing\PathRoutes(
new Routing\FakeRoutes(
[
'foo/{name \d+} [GET]' => 'a',
'bar/{name \w+} [GET]' => 'b',
'foo/{name \d+}' => 'a',
'bar/{name \w+}' => 'b',
]
),
new Uri\FakeUri(null, 'foo/123')
Expand All @@ -68,19 +68,19 @@ final class PathRoutes extends Tester\TestCase {

public function testIgnoringQueryPart() {
Assert::same(
['foo/{name \d+}?page=(1) [GET]' => 'a'],
['foo/{name \d+}?page=(1)' => 'a'],
(new Routing\PathRoutes(
new Routing\FakeRoutes(['foo/{name \d+}?page=(1) [GET]' => 'a']),
new Routing\FakeRoutes(['foo/{name \d+}?page=(1)' => 'a']),
new Uri\FakeUri(null, 'foo/123')
))->matches()
);
}

public function testIgnoringMethodPart() {
Assert::same(
['foo/{name \d+} [GET]' => 'a'],
['foo/{name \d+}' => 'a'],
(new Routing\PathRoutes(
new Routing\FakeRoutes(['foo/{name \d+} [GET]' => 'a']),
new Routing\FakeRoutes(['foo/{name \d+}' => 'a']),
new Uri\FakeUri(null, 'foo/123')
))->matches()
);
Expand All @@ -98,9 +98,9 @@ final class PathRoutes extends Tester\TestCase {
))->matches()
);
Assert::same(
['foo/{name} [GET]' => 'a'],
['foo/{name}' => 'a'],
(new Routing\PathRoutes(
new Routing\FakeRoutes(['foo/{name} [GET]' => 'a']),
new Routing\FakeRoutes(['foo/{name}' => 'a']),
new Uri\FakeUri(null, $path)
))->matches()
);
Expand Down
30 changes: 15 additions & 15 deletions Tests/Unit/QueryRoutes.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ final class QueryRoutes extends Tester\TestCase {
public function testMatchingAnyWithMatchingQuery() {
Assert::same(
[
'{foo :string}?name=cool [GET]' => 'a',
'{foo :string}?name=cool' => 'a',
'{foo (\w\d+*[])}?name=cool' => 'b',
'bar?name=cool [GET]' => 'd',
'bar?name=cool' => 'd',
],
(new Routing\QueryRoutes(
new Routing\FakeRoutes(
[
'{foo :string}?name=cool [GET]' => 'a',
'{foo :string}?name=cool' => 'a',
'{foo (\w\d+*[])}?name=cool' => 'b',
'foo?name=not_cool [GET]' => 'c',
'bar?name=cool [GET]' => 'd',
'foo?name=not_cool' => 'c',
'bar?name=cool' => 'd',
]
),
new Uri\FakeUri(null, null, ['name' => 'cool'])
Expand All @@ -37,10 +37,10 @@ final class QueryRoutes extends Tester\TestCase {

public function testMatchingWithRandomOrder() {
Assert::same(
['foo?name=cool&position=developer [GET]' => 'a'],
['foo?name=cool&position=developer' => 'a'],
(new Routing\QueryRoutes(
new Routing\FakeRoutes(
['foo?name=cool&position=developer [GET]' => 'a']
['foo?name=cool&position=developer' => 'a']
),
new Uri\FakeUri(null, null, ['position' => 'developer', 'name' => 'cool'])
))->matches()
Expand All @@ -49,9 +49,9 @@ final class QueryRoutes extends Tester\TestCase {

public function testIgnoringOtherNotRelevantParameters() {
Assert::same(
['foo?name=cool [GET]' => 'a'],
['foo?name=cool' => 'a'],
(new Routing\QueryRoutes(
new Routing\FakeRoutes(['foo?name=cool [GET]' => 'a']),
new Routing\FakeRoutes(['foo?name=cool' => 'a']),
new Uri\FakeUri(null, null, ['position' => 'developer', 'name' => 'cool'])
))->matches()
);
Expand All @@ -61,7 +61,7 @@ final class QueryRoutes extends Tester\TestCase {
Assert::same(
[],
(new Routing\QueryRoutes(
new Routing\FakeRoutes(['foo?name=cool [GET]' => 'a']),
new Routing\FakeRoutes(['foo?name=cool' => 'a']),
new Uri\FakeUri(null, null, ['x' => 'cool'])
))->matches()
);
Expand All @@ -71,27 +71,27 @@ final class QueryRoutes extends Tester\TestCase {
Assert::same(
[],
(new Routing\QueryRoutes(
new Routing\FakeRoutes(['foo?name=cool [GET]' => 'a']),
new Routing\FakeRoutes(['foo?name=cool' => 'a']),
new Uri\FakeUri(null, 'bar', ['name' => 'x'])
))->matches()
);
}

public function testBracesForDefaultValue() {
Assert::same(
['foo?page=(1) [GET]' => 'a'],
['foo?page=(1)' => 'a'],
(new Routing\QueryRoutes(
new Routing\FakeRoutes(['foo?page=(1) [GET]' => 'a']),
new Routing\FakeRoutes(['foo?page=(1)' => 'a']),
new Uri\FakeUri(null, null, [])
))->matches()
);
}

public function testPassingWithoutQuery() {
Assert::same(
['foo [GET]' => 'a'],
['foo' => 'a'],
(new Routing\QueryRoutes(
new Routing\FakeRoutes(['foo [GET]' => 'a']),
new Routing\FakeRoutes(['foo' => 'a']),
new Uri\FakeUri(null, null, [])
))->matches()
);
Expand Down

0 comments on commit 4616296

Please sign in to comment.