Skip to content

Commit

Permalink
Optional attributes are empty value instead of not returned
Browse files Browse the repository at this point in the history
  • Loading branch information
ElGigi committed Dec 1, 2022
1 parent ac7a0fd commit b580fbe
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 7 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. This projec
to [Semantic Versioning] (http://semver.org/). For change log format,
use [Keep a Changelog] (http://keepachangelog.com/).

## [2.0.2] - 2022-12-01

### Fixed

- Optional attributes are empty value instead of not returned

## [2.0.1] - 2021-12-15

### Fixed
Expand Down
8 changes: 7 additions & 1 deletion src/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,12 @@ public function test(ServerRequestInterface $request, array &$attributes = []):
}

$matches = [];
if (preg_match('~^' . $this->getPathRegex() . '$~i', $request->getUri()->getPath(), $matches) !== 1) {
if (preg_match(
'~^' . $this->getPathRegex() . '$~i',
$request->getUri()->getPath(),
$matches,
PREG_UNMATCHED_AS_NULL
) !== 1) {
return false;
}

Expand All @@ -379,6 +384,7 @@ public function test(ServerRequestInterface $request, array &$attributes = []):
}

$attributes = array_filter($matches, 'is_string', ARRAY_FILTER_USE_KEY);
$attributes = array_filter($attributes, fn($value) => null !== $value);

return true;
}
Expand Down
59 changes: 53 additions & 6 deletions tests/RouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,18 +276,62 @@ public function testTestWithOptionalPartWith2Attributes()
{
$route = new Route('/my-path/{foo}[/{bar}/{baz}]');

$this->assertTrue($route->test($this->getServerRequest('/my-path/value1')));
$attributes = [];
$this->assertTrue($route->test($this->getServerRequest('/my-path/value1'), $attributes));
$this->assertEquals(
[
'foo' => 'value1',
],
$attributes
);

$this->assertFalse($route->test($this->getServerRequest('/my-path/value1/value2')));
$this->assertTrue($route->test($this->getServerRequest('/my-path/value1/value2/value3')));

$attributes = [];
$this->assertTrue($route->test($this->getServerRequest('/my-path/value1/value2/value3'), $attributes));
$this->assertEquals(
[
'foo' => 'value1',
'bar' => 'value2',
'baz' => 'value3',
],
$attributes
);
}

public function testTestWithNestedOptionalPart()
{
$route = new Route('/my-path/{foo}[[/{bar}]/sub-path/{baz}]');

$this->assertTrue($route->test($this->getServerRequest('/my-path/value1')));
$this->assertTrue($route->test($this->getServerRequest('/my-path/value1/sub-path/value3')));
$this->assertTrue($route->test($this->getServerRequest('/my-path/value1/value2/sub-path/value3')));
$attributes = [];
$this->assertTrue($route->test($this->getServerRequest('/my-path/value1'), $attributes));
$this->assertEquals(
[
'foo' => 'value1',
],
$attributes
);

$attributes = [];
$this->assertTrue($route->test($this->getServerRequest('/my-path/value1/sub-path/value3'), $attributes));
$this->assertEquals(
[
'foo' => 'value1',
'baz' => 'value3',
],
$attributes
);

$attributes = [];
$this->assertTrue($route->test($this->getServerRequest('/my-path/value1/value2/sub-path/value3'), $attributes));
$this->assertEquals(
[
'foo' => 'value1',
'bar' => 'value2',
'baz' => 'value3',
],
$attributes
);
}

public function testTestForParentRoute()
Expand Down Expand Up @@ -407,7 +451,10 @@ public function testGenerateWithOptionalPartWith2Attributes()

$this->assertEquals('/my-path/value1', $route->generate(['foo' => 'value1']));
$this->assertEquals('/my-path/value1', $route->generate(['foo' => 'value1', 'bar' => 'value2']));
$this->assertEquals('/my-path/value1/value2/value3', $route->generate(['foo' => 'value1', 'bar' => 'value2', 'baz' => 'value3']));
$this->assertEquals(
'/my-path/value1/value2/value3',
$route->generate(['foo' => 'value1', 'bar' => 'value2', 'baz' => 'value3'])
);
}

public function testGenerateWithNestedOptionalPart()
Expand Down

0 comments on commit b580fbe

Please sign in to comment.