Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2d8a257

Browse files
committed
[Spec] added new test to work with long api routes
1 parent 0eb6d8c commit 2d8a257

4 files changed

Lines changed: 14 additions & 8 deletions

File tree

config/packages/fos_rest.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ fos_rest:
77
empty_content: 204
88
format_listener:
99
rules:
10-
- { path: '^/api/v2/.*', priorities: ['json', 'xml'], fallback_format: json, prefer_extension: true }
10+
- { path: '^/api/v1/.*', priorities: ['json', 'xml'], fallback_format: json, prefer_extension: true }
1111
- { path: '^/', stop: true }

config/packages/security.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
parameters:
22
sylius.security.admin_regex: "^/%sylius_admin.path_name%"
33
sylius.security.api_regex: "^/api/v1"
4-
sylius.security.shop_regex: "^/(?!%sylius_admin.path_name%|api/v2/.*|api$|media/.*)[^/]++"
4+
sylius.security.shop_regex: "^/(?!%sylius_admin.path_name%|api/.*|api$|media/.*)[^/]++"
55
sylius.security.new_api_route: "/api/v2"
66
sylius.security.new_api_regex: "^%sylius.security.new_api_route%"
77
sylius.security.new_api_admin_route: "%sylius.security.new_api_route%/admin"

src/Sylius/Bundle/ApiBundle/Provider/PathPrefixProvider.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,17 @@ public function __construct(UserContextInterface $userContext, string $apiRoute)
3535

3636
public function getPathPrefix(string $path): ?string
3737
{
38-
$pathElements = array_values(array_filter(explode('/', $path)));
39-
40-
if ('/' . $pathElements[0] !== $this->apiRoute) {
38+
if (!str_contains($path, $this->apiRoute)) {
4139
return null;
4240
}
4341

44-
if ($pathElements[1] === PathPrefixes::SHOP_PREFIX) {
42+
$pathElements = array_values(array_filter(explode('/', str_replace($this->apiRoute, '', $path))));
43+
44+
if ($pathElements[0] === PathPrefixes::SHOP_PREFIX) {
4545
return PathPrefixes::SHOP_PREFIX;
4646
}
4747

48-
if ($pathElements[1] === PathPrefixes::ADMIN_PREFIX) {
48+
if ($pathElements[0] === PathPrefixes::ADMIN_PREFIX) {
4949
return PathPrefixes::ADMIN_PREFIX;
5050
}
5151

src/Sylius/Bundle/ApiBundle/spec/Provider/PathPrefixProviderSpec.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ final class PathPrefixProviderSpec extends ObjectBehavior
2323
{
2424
function let(UserContextInterface $userContext): void
2525
{
26-
$this->beConstructedWith($userContext, '/api/v2/');
26+
$this->beConstructedWith($userContext, '/api/v2');
2727
}
2828

2929
function it_implements_a_path_prefix_provider_interface(): void
@@ -51,6 +51,12 @@ function it_returns_admin_prefix_based_on_the_given_path(): void
5151
$this->getPathPrefix('/api/v2/admin/certain-route')->shouldReturn('admin');
5252
}
5353

54+
function it_returns_prefix_from_api_route_with_slashes(UserContextInterface $userContext): void
55+
{
56+
$this->beConstructedWith($userContext, '/api/long/route/name');
57+
$this->getPathPrefix('/api/long/route/name/admin/certain-route')->shouldReturn('admin');
58+
}
59+
5460
function it_returns_admin_prefix_if_currently_logged_in_is_admin_user(
5561
UserContextInterface $userContext,
5662
AdminUserInterface $user

0 commit comments

Comments
 (0)