Skip to content

Commit

Permalink
EZP-31098: Introduced strict types for URLWildcardService (#2855)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwojs authored Nov 7, 2019
1 parent 696301a commit 08b7b1a
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 52 deletions.
19 changes: 10 additions & 9 deletions eZ/Publish/API/Repository/URLWildcardService.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
<?php

/**
* File containing the eZ\Publish\API\Repository\URLWildcardService class.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace eZ\Publish\API\Repository;

use eZ\Publish\API\Repository\Values\Content\URLWildcard;
use eZ\Publish\API\Repository\Values\Content\URLWildcardTranslationResult;

/**
* URLAlias service.
Expand All @@ -32,7 +33,7 @@ interface URLWildcardService
*
* @return \eZ\Publish\API\Repository\Values\Content\UrlWildcard
*/
public function create($sourceUrl, $destinationUrl, $forward = false);
public function create(string $sourceUrl, string $destinationUrl, bool $forward = false): UrlWildcard;

/**
* Removes an url wildcard.
Expand All @@ -41,18 +42,18 @@ public function create($sourceUrl, $destinationUrl, $forward = false);
*
* @param \eZ\Publish\API\Repository\Values\Content\UrlWildcard $urlWildcard the url wildcard to remove
*/
public function remove(URLWildcard $urlWildcard);
public function remove(URLWildcard $urlWildcard): void;

/**
* Loads a url wild card.
*
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the url wild card was not found
*
* @param mixed $id
* @param int $id
*
* @return \eZ\Publish\API\Repository\Values\Content\UrlWildcard
*/
public function load($id);
public function load(int $id): UrlWildcard;

/**
* Loads all url wild card (paged).
Expand All @@ -62,17 +63,17 @@ public function load($id);
*
* @return \eZ\Publish\API\Repository\Values\Content\UrlWildcard[]
*/
public function loadAll($offset = 0, $limit = -1);
public function loadAll(int $offset = 0, int $limit = -1): iterable;

/**
* Translates an url to an existing uri resource based on the
* source/destination patterns of the url wildcard.
*
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the url could not be translated
*
* @param mixed $url
* @param string $url
*
* @return \eZ\Publish\API\Repository\Values\Content\URLWildcardTranslationResult
*/
public function translate($url);
public function translate(string $url): URLWildcardTranslationResult;
}
11 changes: 6 additions & 5 deletions eZ/Publish/Core/Event/URLWildcardService.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use eZ\Publish\API\Repository\URLWildcardService as URLWildcardServiceInterface;
use eZ\Publish\API\Repository\Values\Content\URLWildcard;
use eZ\Publish\API\Repository\Values\Content\URLWildcardTranslationResult;
use eZ\Publish\API\Repository\Events\URLWildcard\BeforeCreateEvent;
use eZ\Publish\API\Repository\Events\URLWildcard\BeforeRemoveEvent;
use eZ\Publish\API\Repository\Events\URLWildcard\BeforeTranslateEvent;
Expand All @@ -34,10 +35,10 @@ public function __construct(
}

public function create(
$sourceUrl,
$destinationUrl,
$forward = false
) {
string $sourceUrl,
string $destinationUrl,
bool $forward = false
): UrlWildcard {
$eventData = [
$sourceUrl,
$destinationUrl,
Expand Down Expand Up @@ -80,7 +81,7 @@ public function remove(URLWildcard $urlWildcard): void
);
}

public function translate($url)
public function translate(string $url): URLWildcardTranslationResult
{
$eventData = [$url];

Expand Down
26 changes: 14 additions & 12 deletions eZ/Publish/Core/Repository/Tests/Service/Mock/UrlWildcardTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

/**
* File contains: eZ\Publish\Core\Repository\Tests\Service\Mock\UrlWildcardTest class.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace eZ\Publish\Core\Repository\Tests\Service\Mock;

use Exception;
Expand All @@ -24,6 +24,8 @@
*/
class UrlWildcardTest extends BaseServiceMockTest
{
private const EXAMPLE_URL_WILDCARD_ID = 1;

/** @var \eZ\Publish\API\Repository\PermissionResolver|\PHPUnit\Framework\MockObject\MockObject */
private $permissionResolver;

Expand All @@ -44,7 +46,7 @@ protected function setUp(): void
*/
public function testCreateThrowsUnauthorizedException()
{
$this->expectException(\eZ\Publish\API\Repository\Exceptions\UnauthorizedException::class);
$this->expectException(UnauthorizedException::class);

$mockedService = $this->getPartlyMockedURLWildcardService();

Expand Down Expand Up @@ -283,7 +285,7 @@ public function testCreateWithRollback()
*/
public function testRemoveThrowsUnauthorizedException()
{
$this->expectException(\eZ\Publish\API\Repository\Exceptions\UnauthorizedException::class);
$this->expectException(UnauthorizedException::class);

$wildcard = new URLWildcard(['id' => 'McBoom']);

Expand Down Expand Up @@ -407,14 +409,14 @@ public function testLoadThrowsException()
)->method(
'load'
)->with(
$this->equalTo('Luigi')
$this->equalTo(self::EXAMPLE_URL_WILDCARD_ID)
)->will(
$this->throwException(new Exception())
);

$this->expectException(Exception::class);

$mockedService->load('Luigi');
$mockedService->load(self::EXAMPLE_URL_WILDCARD_ID);
}

/**
Expand All @@ -431,12 +433,12 @@ public function testLoad()
)->method(
'load'
)->with(
$this->equalTo('Luigi')
$this->equalTo(self::EXAMPLE_URL_WILDCARD_ID)
)->will(
$this->returnValue(
new SPIURLWildcard(
[
'id' => 'Luigi',
'id' => self::EXAMPLE_URL_WILDCARD_ID,
'sourceUrl' => 'this',
'destinationUrl' => 'that',
'forward' => true,
Expand All @@ -445,12 +447,12 @@ public function testLoad()
)
);

$urlWildcard = $mockedService->load('Luigi');
$urlWildcard = $mockedService->load(self::EXAMPLE_URL_WILDCARD_ID);

$this->assertEquals(
new URLWildcard(
[
'id' => 'Luigi',
'id' => self::EXAMPLE_URL_WILDCARD_ID,
'sourceUrl' => 'this',
'destinationUrl' => 'that',
'forward' => true,
Expand Down Expand Up @@ -504,7 +506,7 @@ public function testLoadAllWithLimitAndOffset()
[
new SPIURLWildcard(
[
'id' => 'Luigi',
'id' => self::EXAMPLE_URL_WILDCARD_ID,
'sourceUrl' => 'this',
'destinationUrl' => 'that',
'forward' => true,
Expand All @@ -520,7 +522,7 @@ public function testLoadAllWithLimitAndOffset()
[
new URLWildcard(
[
'id' => 'Luigi',
'id' => self::EXAMPLE_URL_WILDCARD_ID,
'sourceUrl' => 'this',
'destinationUrl' => 'that',
'forward' => true,
Expand Down
16 changes: 8 additions & 8 deletions eZ/Publish/Core/Repository/URLWildcardService.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

/**
* File containing the eZ\Publish\Core\Repository\URLWildcardService class.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace eZ\Publish\Core\Repository;

use eZ\Publish\API\Repository\PermissionResolver;
Expand Down Expand Up @@ -73,7 +73,7 @@ public function __construct(
*
* @return \eZ\Publish\API\Repository\Values\Content\UrlWildcard
*/
public function create($sourceUrl, $destinationUrl, $forward = false): URLWildcard
public function create(string $sourceUrl, string $destinationUrl, bool $forward = false): UrlWildcard
{
if ($this->permissionResolver->hasAccess('content', 'urltranslator') === false) {
throw new UnauthorizedException('content', 'urltranslator');
Expand Down Expand Up @@ -145,11 +145,11 @@ public function remove(URLWildcard $urlWildcard): void
*
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the url wild card was not found
*
* @param mixed $id
* @param int $id
*
* @return \eZ\Publish\API\Repository\Values\Content\UrlWildcard
*/
public function load($id): URLWildcard
public function load(int $id): UrlWildcard
{
return $this->buildUrlWildcardDomainObject(
$this->urlWildcardHandler->load($id)
Expand All @@ -164,7 +164,7 @@ public function load($id): URLWildcard
*
* @return \eZ\Publish\API\Repository\Values\Content\UrlWildcard[]
*/
public function loadAll($offset = 0, $limit = -1): array
public function loadAll(int $offset = 0, int $limit = -1): iterable
{
$spiUrlWildcards = $this->urlWildcardHandler->loadAll(
$offset,
Expand All @@ -185,11 +185,11 @@ public function loadAll($offset = 0, $limit = -1): array
*
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the url could not be translated
*
* @param mixed $url
* @param string $url
*
* @return \eZ\Publish\API\Repository\Values\Content\URLWildcardTranslationResult
*/
public function translate($url): URLWildcardTranslationResult
public function translate(string $url): URLWildcardTranslationResult
{
$spiWildcard = $this->urlWildcardHandler->translate($this->cleanPath($url));

Expand Down
23 changes: 12 additions & 11 deletions eZ/Publish/SPI/Repository/Decorator/URLWildcardServiceDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use eZ\Publish\API\Repository\URLWildcardService;
use eZ\Publish\API\Repository\Values\Content\URLWildcard;
use eZ\Publish\API\Repository\Values\Content\URLWildcardTranslationResult;

abstract class URLWildcardServiceDecorator implements URLWildcardService
{
Expand All @@ -22,31 +23,31 @@ public function __construct(URLWildcardService $innerService)
}

public function create(
$sourceUrl,
$destinationUrl,
$forward = false
) {
string $sourceUrl,
string $destinationUrl,
bool $forward = false
): UrlWildcard {
return $this->innerService->create($sourceUrl, $destinationUrl, $forward);
}

public function remove(URLWildcard $urlWildcard)
public function remove(URLWildcard $urlWildcard): void
{
return $this->innerService->remove($urlWildcard);
$this->innerService->remove($urlWildcard);
}

public function load($id)
public function load(int $id): UrlWildcard
{
return $this->innerService->load($id);
}

public function loadAll(
$offset = 0,
$limit = -1
) {
int $offset = 0,
int $limit = -1
): iterable {
return $this->innerService->loadAll($offset, $limit);
}

public function translate($url)
public function translate(string $url): URLWildcardTranslationResult
{
return $this->innerService->translate($url);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public function testCreateDecorator()
$decoratedService = $this->createDecorator($serviceMock);

$parameters = [
'random_value_5ced05ce0f76d9.50834569',
'random_value_5ced05ce0f7709.88573575',
'random_value_5ced05ce0f7713.40900633',
'source_url_value',
'destination_url_value',
true,
];

$serviceMock->expects($this->once())->method('create')->with(...$parameters);
Expand All @@ -60,7 +60,7 @@ public function testLoadDecorator()
$serviceMock = $this->createServiceMock();
$decoratedService = $this->createDecorator($serviceMock);

$parameters = ['random_value_5ced05ce0f7e67.73670278'];
$parameters = [1];

$serviceMock->expects($this->once())->method('load')->with(...$parameters);

Expand All @@ -73,8 +73,8 @@ public function testLoadAllDecorator()
$decoratedService = $this->createDecorator($serviceMock);

$parameters = [
'random_value_5ced05ce0f7ea4.77940790',
'random_value_5ced05ce0f7eb2.19026311',
10,
100,
];

$serviceMock->expects($this->once())->method('loadAll')->with(...$parameters);
Expand All @@ -87,7 +87,7 @@ public function testTranslateDecorator()
$serviceMock = $this->createServiceMock();
$decoratedService = $this->createDecorator($serviceMock);

$parameters = ['random_value_5ced05ce0f7ee7.19290474'];
$parameters = ['ez.no'];

$serviceMock->expects($this->once())->method('translate')->with(...$parameters);

Expand Down

0 comments on commit 08b7b1a

Please sign in to comment.