-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* IBX-8176: Implemented routing language expression extension handling `Content-Type` header * IBX-8176: CS * IBX-8176: Added unit tests * IBX-8176: Enable `strict_types` * IBX-8176: Enable `strict_types` * IBX-8176: Applied review remarks * IBX-8176: Fixed type * IBX-8176: Refactored solution * IBX-8176: Removed 'future' implementation * IBX-8176: Applied review remarks * IBX-8176: Fix route definition * IBX-8176: Refactoring * IBX-8176: Rename the expression function * IBX-8176: Applied review remark * IBX-8176: Added generation of `options` route for newly added `condition`-dependable routes * IBX-8176: Fixed tests * IBX-8176: Created new OpenAPI compatible location moving endpoint * IBX-8176: Fixed functional tests * IBX-8176: Updated baseline for PHPStan * IBX-8176: Applied review remarks * IBX-8176: Fixed parser logic * IBX-8176: Removed deprecation * IBX-8176: Refactored routing to use the `is_content_type_compatible` function * IBX-8176: Used `Validator` to validate input data * IBX-8176: Applied review remarks * IBX-8176: Fix tests * IBX-8176: Fixed routing definition * IBX-8176: Added `options_route_suffix` * IBX-8176: Update `HttpOptionsTest` * IBX-8176: Updated routing * IBX-8176: Fixed routing * IBX-8176: CS
- Loading branch information
Showing
9 changed files
with
292 additions
and
6 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
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
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,84 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\Rest\Server\Input\Parser; | ||
|
||
use Ibexa\Contracts\Core\Repository\LocationService; | ||
use Ibexa\Contracts\Core\Repository\Values\Content\Location; | ||
use Ibexa\Contracts\Rest\Input\ParsingDispatcher; | ||
use Ibexa\Rest\Input\BaseParser; | ||
use Ibexa\Rest\Server\Exceptions\ValidationFailedException; | ||
use Ibexa\Rest\Server\Validation\Builder\Input\Parser\MoveLocationInputValidatorBuilder; | ||
use Symfony\Component\Validator\Validator\ValidatorInterface; | ||
|
||
final class MoveLocation extends BaseParser | ||
{ | ||
public const string DESTINATION_KEY = 'destination'; | ||
|
||
public function __construct( | ||
private readonly LocationService $locationService, | ||
private readonly ValidatorInterface $validator, | ||
) { | ||
} | ||
|
||
/** | ||
* @phpstan-param array{ | ||
* 'destination': string, | ||
* } $data | ||
* | ||
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException | ||
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException | ||
* @throws \Ibexa\Contracts\Rest\Exceptions\Parser | ||
*/ | ||
public function parse(array $data, ParsingDispatcher $parsingDispatcher): Location | ||
{ | ||
$this->validateInputData($data); | ||
|
||
return $this->getLocationByPath($data[self::DESTINATION_KEY]); | ||
} | ||
|
||
/** | ||
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException | ||
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException | ||
*/ | ||
private function getLocationByPath(string $path): Location | ||
{ | ||
return $this->locationService->loadLocation( | ||
$this->extractLocationIdFromPath($path) | ||
); | ||
} | ||
|
||
private function extractLocationIdFromPath(string $path): int | ||
{ | ||
$pathParts = explode('/', $path); | ||
|
||
return (int)array_pop($pathParts); | ||
} | ||
|
||
/** | ||
* @phpstan-assert array{ | ||
* 'destination': string, | ||
* } $data | ||
* | ||
* @param array<mixed> $data | ||
* | ||
* @throws \Ibexa\Rest\Server\Exceptions\ValidationFailedException | ||
*/ | ||
private function validateInputData(array $data): void | ||
{ | ||
$builder = new MoveLocationInputValidatorBuilder($this->validator); | ||
$builder->validateInputArray($data); | ||
$violations = $builder->build()->getViolations(); | ||
if ($violations->count() > 0) { | ||
throw new ValidationFailedException( | ||
'MoveLocation', | ||
$violations, | ||
); | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/lib/Server/Validation/Builder/Input/Parser/MoveLocationInputValidatorBuilder.php
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,29 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\Rest\Server\Validation\Builder\Input\Parser; | ||
|
||
use Ibexa\Rest\Server\Input\Parser\MoveLocation; | ||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
final class MoveLocationInputValidatorBuilder extends BaseInputParserValidatorBuilder | ||
{ | ||
protected function buildConstraint(): Constraint | ||
{ | ||
return new Assert\Collection( | ||
[ | ||
MoveLocation::DESTINATION_KEY => [ | ||
new Assert\NotBlank(), | ||
new Assert\Type('string'), | ||
new Assert\Regex('/^(\/\d+)+$/'), | ||
], | ||
], | ||
); | ||
} | ||
} |
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
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,105 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\Tests\Rest\Server\Input\Parser; | ||
|
||
use Ibexa\Contracts\Core\Repository\LocationService; | ||
use Ibexa\Core\Repository\Values\Content\Location; | ||
use Ibexa\Rest\Server\Exceptions\ValidationFailedException; | ||
use Ibexa\Rest\Server\Input\Parser\MoveLocation; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use Symfony\Component\Validator\Validation; | ||
use Symfony\Component\Validator\Validator\ValidatorInterface; | ||
|
||
final class MoveLocationTest extends BaseTest | ||
{ | ||
private const int TESTED_LOCATION_ID = 22; | ||
|
||
private MockObject&LocationService $locationService; | ||
|
||
private ValidatorInterface $validator; | ||
|
||
public function testParse(): void | ||
{ | ||
$destinationPath = sprintf('/1/2/%d', self::TESTED_LOCATION_ID); | ||
|
||
$inputArray = [ | ||
'destination' => $destinationPath, | ||
]; | ||
|
||
$moveLocationParser = $this->getParser(); | ||
|
||
$this->locationService | ||
->expects(self::once()) | ||
->method('loadLocation') | ||
->with(self::TESTED_LOCATION_ID) | ||
->willReturn($this->getMockedLocation()); | ||
|
||
$result = $moveLocationParser->parse($inputArray, $this->getParsingDispatcherMock()); | ||
|
||
self::assertEquals( | ||
$this->getMockedLocation()->id, | ||
$result->id, | ||
); | ||
|
||
self::assertEquals( | ||
$this->getMockedLocation()->getPathString(), | ||
$result->getPathString(), | ||
); | ||
} | ||
|
||
public function testParseExceptionOnMissingDestinationElement(): void | ||
{ | ||
$this->expectException(ValidationFailedException::class); | ||
$this->expectExceptionMessage('Input data validation failed for MoveLocation'); | ||
|
||
$inputArray = [ | ||
'new_destination' => '/1/2/3', | ||
]; | ||
|
||
$sessionInput = $this->getParser(); | ||
|
||
$sessionInput->parse($inputArray, $this->getParsingDispatcherMock()); | ||
} | ||
|
||
public function testParseExceptionOnInvalidDestinationElement(): void | ||
{ | ||
$inputArray = [ | ||
'destination' => 'test_destination', | ||
]; | ||
|
||
$sessionInput = $this->getParser(); | ||
|
||
$this->expectException(ValidationFailedException::class); | ||
$this->expectExceptionMessage('Input data validation failed for MoveLocation'); | ||
|
||
$sessionInput->parse($inputArray, $this->getParsingDispatcherMock()); | ||
} | ||
|
||
protected function internalGetParser(): MoveLocation | ||
{ | ||
$locationService = $this->createMock(LocationService::class); | ||
$this->locationService = $locationService; | ||
$this->validator = Validation::createValidator(); | ||
|
||
return new MoveLocation( | ||
$this->locationService, | ||
$this->validator, | ||
); | ||
} | ||
|
||
private function getMockedLocation(): Location | ||
{ | ||
return new Location( | ||
[ | ||
'id' => self::TESTED_LOCATION_ID, | ||
'pathString' => sprintf('/1/2/%d', self::TESTED_LOCATION_ID), | ||
], | ||
); | ||
} | ||
} |