Skip to content
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## 12.2.3 under development

- Enh #289: Add validation of renderers configuration to `ViewTrait::withRenderers()` (@samdark)
- Bug #295: Remove unnecessary `CacheKeyNormalizer` instance creation in `CachedContent` (@samdark)
- Enh #295: Minor refactor `ViewTrait::getParameter()` and `ViewTrait::resolveViewFilePath()` (@samdark)

Expand Down
33 changes: 33 additions & 0 deletions src/ViewTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,39 @@
*/
public function withRenderers(array $renderers): static
{
foreach ($renderers as $extension => $renderer) {
if (!is_string($extension)) {

Check failure on line 91 in src/ViewTrait.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

DocblockTypeContradiction

src/ViewTrait.php:91:18: DocblockTypeContradiction: Docblock-defined type string for $extension is always string (see https://psalm.dev/155)

Check failure on line 91 in src/ViewTrait.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.4-ubuntu-latest

DocblockTypeContradiction

src/ViewTrait.php:91:18: DocblockTypeContradiction: Docblock-defined type string for $extension is always string (see https://psalm.dev/155)
throw new InvalidArgumentException(
sprintf(
'Extension must be a non-empty string, %s provided for %s.',
get_debug_type($extension),
$renderer::class
)
);
}

$rendererType = get_debug_type($renderer);

if ($extension === '') {
throw new InvalidArgumentException(
sprintf(
'Empty extension is not supported. Please add extension for %s.',
$rendererType
)
);
}

if (!is_object($renderer) || !$renderer instanceof TemplateRendererInterface) {

Check failure on line 112 in src/ViewTrait.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

DocblockTypeContradiction

src/ViewTrait.php:112:17: DocblockTypeContradiction: Docblock-defined type Yiisoft\View\TemplateRendererInterface for $renderer is always object (see https://psalm.dev/155)

Check failure on line 112 in src/ViewTrait.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.4-ubuntu-latest

DocblockTypeContradiction

src/ViewTrait.php:112:17: DocblockTypeContradiction: Docblock-defined type Yiisoft\View\TemplateRendererInterface for $renderer is always object (see https://psalm.dev/155)
throw new InvalidArgumentException(
sprintf(
'Renderer %s is not an instance of %s.',
$rendererType,
TemplateRendererInterface::class
)
);
}
}

$new = clone $this;
$new->renderers = $renderers;
return $new;
Expand Down
60 changes: 59 additions & 1 deletion tests/ViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use stdClass;
use Yiisoft\Files\FileHelper;
use Yiisoft\Test\Support\EventDispatcher\SimpleEventDispatcher;
use Yiisoft\View\Event\View\PageBegin;
use Yiisoft\View\Event\View\PageEnd;
use Yiisoft\View\Exception\ViewNotFoundException;
use Yiisoft\View\PhpTemplateRenderer;
use Yiisoft\View\TemplateRendererInterface;
use Yiisoft\View\Tests\TestSupport\TestHelper;
use Yiisoft\View\Tests\TestSupport\TestTrait;
use Yiisoft\View\Theme;
Expand Down Expand Up @@ -232,6 +234,62 @@ public function testRenderWithoutFileExtension(string $filename, string $extensi
);
}

public function testWithRenderersEmptyExtensionThrowsException(): void
{
$view = TestHelper::createView();

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage(
'Empty extension is not supported. Please add extension for ' . PhpTemplateRenderer::class . '.'
);

$view->withRenderers([
'' => new PhpTemplateRenderer(),
]);
}

public function testWithRenderersInvalidRendererTypeThrowsException(): void
{
$view = TestHelper::createView();

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage(
'Renderer stdClass is not an instance of ' . TemplateRendererInterface::class . '.'
);

$view->withRenderers([
'php' => new stdClass(),
]);
}

public function testWithRenderersNumericKeyImplicitThrowsException(): void
{
$view = TestHelper::createView();

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage(
'Extension must be a non-empty string, int provided for ' . PhpTemplateRenderer::class . '.'
);

$view->withRenderers([
new PhpTemplateRenderer(),
]);
}

public function testWithRenderersNumericKeyExplicitThrowsException(): void
{
$view = TestHelper::createView();

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage(
'Extension must be a non-empty string, int provided for ' . PhpTemplateRenderer::class . '.'
);

$view->withRenderers([
0 => new PhpTemplateRenderer(),
]);
}

public function testLocalize(): void
{
$view = $this->createViewWithBasePath($this->tempDirectory);
Expand Down Expand Up @@ -598,7 +656,7 @@ public function testImmutability(): void
$view = TestHelper::createView();

$this->assertNotSame($view, $view->withBasePath(''));
$this->assertNotSame($view, $view->withRenderers([new PhpTemplateRenderer()]));
$this->assertNotSame($view, $view->withRenderers(['php' => new PhpTemplateRenderer()]));
$this->assertNotSame($view, $view->withSourceLocale('en'));
$this->assertNotSame($view, $view->withContext($this->createContext($this->tempDirectory)));
$this->assertNotSame($view, $view->withContextPath(__DIR__));
Expand Down
Loading