-
-
Notifications
You must be signed in to change notification settings - Fork 835
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix optional parameters in url generator (#2246)
* Fix route collection getting wrong path when optional parameters present, add unit tests
- Loading branch information
1 parent
db83003
commit 8a73cc5
Showing
2 changed files
with
96 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Flarum. | ||
* | ||
* For detailed copyright and license information, please view the | ||
* LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Flarum\Tests\unit\Foundation; | ||
|
||
use Flarum\Http\RouteCollection; | ||
use Flarum\Tests\unit\TestCase; | ||
use RuntimeException; | ||
|
||
class RouteCollectionTest extends TestCase | ||
{ | ||
/** @test */ | ||
public function it_errors_when_nonexistent_route_requested() | ||
{ | ||
$collection = new RouteCollection(); | ||
|
||
$this->expectException(RuntimeException::class); | ||
|
||
$collection->getPath('nonexistent'); | ||
} | ||
|
||
/** @test */ | ||
public function it_properly_processes_a_simple_route_with_no_parameters() | ||
{ | ||
$collection = new RouteCollection(); | ||
// We can use anything for the handler since we're only testing getPath | ||
$collection->addRoute('GET', '/custom/route', 'custom', ''); | ||
|
||
$this->assertEquals('/custom/route', $collection->getPath('custom')); | ||
} | ||
|
||
/** @test */ | ||
public function it_properly_processes_a_route_with_all_parameters_required() | ||
{ | ||
$collection = new RouteCollection(); | ||
// We can use anything for the handler since we're only testing getPath | ||
$collection->addRoute('GET', '/custom/{route}/{has}/{parameters}', 'custom', ''); | ||
|
||
$this->assertEquals('/custom/something/something_else/anything_else', $collection->getPath('custom', [ | ||
'route' => 'something', | ||
'has' => 'something_else', | ||
'parameters' => 'anything_else' | ||
])); | ||
} | ||
|
||
/** @test */ | ||
public function it_works_if_optional_parameters_are_missing() | ||
{ | ||
$collection = new RouteCollection(); | ||
// We can use anything for the handler since we're only testing getPath | ||
$collection->addRoute('GET', '/custom/{route}[/{has}]', 'custom', ''); | ||
|
||
$this->assertEquals('/custom/something', $collection->getPath('custom', [ | ||
'route' => 'something' | ||
])); | ||
} | ||
|
||
/** @test */ | ||
public function it_works_with_optional_parameters() | ||
{ | ||
$collection = new RouteCollection(); | ||
// We can use anything for the handler since we're only testing getPath | ||
$collection->addRoute('GET', '/custom/{route}[/{has}]', 'custom', ''); | ||
|
||
$this->assertEquals('/custom/something/something_else', $collection->getPath('custom', [ | ||
'route' => 'something', | ||
'has' => 'something_else' | ||
])); | ||
} | ||
} |