Skip to content

Commit

Permalink
Add method withTheme() in ViewTemplateRenderer::class. (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Aug 17, 2023
1 parent 4e6031c commit 3c6df3e
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/ViewTemplateRenderer.php
Expand Up @@ -7,6 +7,7 @@
use Psr\Http\Message\ResponseInterface;
use Yiisoft\Aliases\Aliases;
use Yiisoft\DataResponse\DataResponseFactoryInterface;
use Yiisoft\View\Theme;
use Yiisoft\View\ViewContextInterface;
use Yiisoft\View\WebView;

Expand Down Expand Up @@ -57,6 +58,14 @@ public function withLayoutParameters(array $parameters): self
return $new;
}

public function withTheme(Theme $theme): self
{
$new = clone $this;
$new->webView->setTheme($theme);

return $new;
}

public function withViewPath(string $path): self
{
$new = clone $this;
Expand Down
2 changes: 2 additions & 0 deletions tests/ViewTemplateRenderer/ImmutabilityTest.php
Expand Up @@ -6,6 +6,7 @@

use PHPUnit\Framework\TestCase;
use Yii\Service\Tests\Support\TestTrait;
use Yiisoft\View\Theme;

final class ImmutabilityTest extends TestCase
{
Expand All @@ -17,6 +18,7 @@ public function testImmutability(): void

$this->assertNotSame($viewTemplateRenderer, $viewTemplateRenderer->withLayoutFile(''));
$this->assertNotSame($viewTemplateRenderer, $viewTemplateRenderer->withLayoutParameters([]));
$this->assertNotSame($viewTemplateRenderer, $viewTemplateRenderer->withTheme(new Theme()));
$this->assertNotSame($viewTemplateRenderer, $viewTemplateRenderer->withViewPath(''));
}
}
33 changes: 33 additions & 0 deletions tests/ViewTemplateRenderer/Test.php
Expand Up @@ -8,6 +8,7 @@
use Yii\Service\Tests\Support\TestTrait;
use Yii\Support\Assert;
use Yiisoft\DataResponse\DataResponse;
use Yiisoft\View\Theme;

final class Test extends TestCase
{
Expand Down Expand Up @@ -41,6 +42,38 @@ public function testRender(): void
);
}

public function testTheme(): void
{
$this->aliases->set('@layout', dirname(__DIR__) . '/data/resources/view');

/** @var DataResponse $dataResponse */
$dataResponse = $this->viewTemplateRenderer
->withViewPath('@resources/view')
->withTheme(
new Theme(
[$this->aliases->get('@resources/view') => $this->aliases->get('@resources/view/theme')]
)
)->render('view', ['title' => 'View'], ['title' => 'View']);

Assert::equalsWithoutLE(
<<<HTML
<!DOCTYPE html>
<html>
<head>
<title>Main Theme</title>
</head>
<body>
<h1>View</h1>
</body>
</html>
HTML,
$dataResponse->getData(),
);
}

public function testWithLayoutParameters(): void
{
$this->aliases->set('@layout', dirname(__DIR__) . '/data/resources/view');
Expand Down
26 changes: 26 additions & 0 deletions tests/data/resources/view/theme/main.php
@@ -0,0 +1,26 @@
<?php
/**
* @var $this \Yiisoft\View\WebView
* @var $content string
* @var string|null $csrfToken
*/
$csrfToken = $csrfToken ?? null;
?>
<?php $this->beginPage(); ?>
<!DOCTYPE html>
<html>
<head>
<?php if ($csrfToken !== null): ?>
<meta name="csrf-token" content="<?= $csrfToken ?>">
<?php endif ?>
<title>Main Theme</title>
</head>
<body>
<?php $this->beginBody(); ?>

<?= $content ?>

<?php $this->endBody(); ?>
</body>
</html>
<?php $this->endPage();

0 comments on commit 3c6df3e

Please sign in to comment.