Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IBX-6875: Implemented IsPreview View Matcher #288

Merged
merged 3 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/lib/MVC/Symfony/Matcher/ContentBased/IsPreview.php
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 tests/lib/MVC/Symfony/Matcher/ContentBased/IsPreviewTest.php
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);
}
}
Loading