-
Notifications
You must be signed in to change notification settings - Fork 14
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
For more details see https://issues.ibexa.co/browse/IBX-6875 and #288. `IsPreview` View Matcher allows to distinguish views depending on if template is used to render preview or view of a content item. Key changes: * Implemented IsPreview View Matcher * [Tests] Added unit test coverage for IsPreview View Matcher --------- Co-authored-by: Adam Wójs <[email protected]>
- Loading branch information
Showing
2 changed files
with
144 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?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\Core\MVC\Symfony\Matcher\ContentBased; | ||
|
||
use Ibexa\Core\Base\Exceptions\InvalidArgumentException; | ||
use Ibexa\Core\MVC\Symfony\Controller\Content\PreviewController; | ||
use Ibexa\Core\MVC\Symfony\Matcher\ViewMatcherInterface; | ||
use Ibexa\Core\MVC\Symfony\View\View; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class IsPreview implements ViewMatcherInterface | ||
{ | ||
private bool $isPreview = true; | ||
|
||
/** | ||
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException | ||
*/ | ||
public function setMatchingConfig($matchingConfig): void | ||
{ | ||
if (!is_bool($matchingConfig)) { | ||
throw new InvalidArgumentException( | ||
'$matchConfig', | ||
sprintf( | ||
'IsPreview matcher expects true or false value, got a value of %s type', | ||
gettype($matchingConfig) | ||
) | ||
); | ||
} | ||
|
||
$this->isPreview = $matchingConfig; | ||
} | ||
|
||
public function match(View $view): bool | ||
{ | ||
$isPreview = $view->hasParameter(PreviewController::PREVIEW_PARAMETER_NAME) | ||
&& $view->getParameter(PreviewController::PREVIEW_PARAMETER_NAME); | ||
|
||
return $this->isPreview === $isPreview; | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
tests/lib/MVC/Symfony/Matcher/ContentBased/IsPreviewTest.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,96 @@ | ||
<?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\Core\MVC\Symfony\Matcher\ContentBased; | ||
|
||
use Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException; | ||
use Ibexa\Core\MVC\Symfony\Matcher\ContentBased\IsPreview; | ||
use Ibexa\Core\MVC\Symfony\View\ContentView; | ||
use Ibexa\Core\MVC\Symfony\View\View; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @covers \Ibexa\Core\MVC\Symfony\Matcher\ContentBased\IsPreview | ||
*/ | ||
final class IsPreviewTest extends TestCase | ||
{ | ||
private IsPreview $isPreviewMatcher; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->isPreviewMatcher = new IsPreview(); | ||
} | ||
|
||
/** | ||
* @return iterable<string, array{\Ibexa\Core\MVC\Symfony\View\View, boolean, boolean}> | ||
*/ | ||
public static function getDataForTestMatch(): iterable | ||
{ | ||
$previewContentView = new ContentView(); | ||
$previewContentView->setParameters(['isPreview' => true]); | ||
|
||
$notPreviewContentView = new ContentView(); | ||
$notPreviewContentView->setParameters(['isPreview' => false]); | ||
|
||
$viewContentView = new ContentView(); | ||
yield 'match for preview content view' => [ | ||
$previewContentView, | ||
true, // IsPreview: true | ||
true, // matches the view | ||
]; | ||
|
||
yield 'do not match for preview content view' => [ | ||
$previewContentView, | ||
false, // IsPreview: false | ||
false, // doesn't match the view | ||
]; | ||
|
||
yield 'match for view content view' => [ | ||
$notPreviewContentView, | ||
false, // IsPreview: false | ||
true, // matches since it's not a preview content view | ||
]; | ||
|
||
yield 'do not match for view content view' => [ | ||
$notPreviewContentView, | ||
true, // IsPreview: true | ||
false, // doesn't match since it's not a preview content view | ||
]; | ||
|
||
yield 'not match for not set isPreview parameter' => [ | ||
$viewContentView, | ||
true, // IsPreview: true | ||
false, // by default, it's not a preview view if parameter is not set | ||
]; | ||
|
||
yield 'do not match for not set isPreview parameter' => [ | ||
$viewContentView, | ||
false, | ||
true, // matches not a preview view, when parameter is not set | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider getDataForTestMatch | ||
* | ||
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException | ||
*/ | ||
public function testMatch(View $view, bool $matchConfig, bool $expectedIsPreview): void | ||
{ | ||
$this->isPreviewMatcher->setMatchingConfig($matchConfig); | ||
|
||
self::assertSame($expectedIsPreview, $this->isPreviewMatcher->match($view)); | ||
} | ||
|
||
public function testSetMatchConfigThrowsInvalidArgumentException(): void | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
$this->expectExceptionMessage('IsPreview matcher expects true or false value, got a value of integer type'); | ||
$this->isPreviewMatcher->setMatchingConfig(123); | ||
} | ||
} |