Skip to content

Commit

Permalink
Add more tests (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
samdark committed Jun 20, 2021
1 parent 747d674 commit 2af18ab
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/WebView.php
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ protected function renderBodyEndHtml(bool $ajaxMode): string
$lines[] = Html::script($js)->render();
}
if (!empty($this->js[self::POSITION_LOAD])) {
$js = "window.addEventListener('load', function (event) {\n" .
$js = "window.addEventListener('load', function(event) {\n" .
$this->generateJsWithoutTag($this->js[self::POSITION_LOAD]) .
"\n});";
$lines[] = Html::script($js)->render();
Expand Down
13 changes: 13 additions & 0 deletions tests/CachedContentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\View\Tests;

use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use Yiisoft\Cache\ArrayCache;
use Yiisoft\Cache\Cache;
Expand Down Expand Up @@ -99,4 +100,16 @@ public function testCacheWithVariants(): void
$this->assertNull($cacheContent->get());
}
}

public function testSettingInvalidDynamicContents(): void
{
$this->expectException(InvalidArgumentException::class);
new CachedContent('test', $this->cache, ['test']);
}

public function testSettingInvalidVariations(): void
{
$this->expectException(InvalidArgumentException::class);
new CachedContent('test', $this->cache, [], [42]);
}
}
16 changes: 16 additions & 0 deletions tests/ViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use RuntimeException;
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\Tests\TestSupport\TestHelper;
use Yiisoft\View\Theme;
use Yiisoft\View\View;
Expand Down Expand Up @@ -264,4 +266,18 @@ public function getViewPath(): string
}
};
}

public function testPageEvents(): void
{
$eventDispatcher = new SimpleEventDispatcher();
$view = TestHelper::createView($eventDispatcher);

$view->beginPage();
$view->endPage();

$this->assertSame([
PageBegin::class,
PageEnd::class,
], $eventDispatcher->getEventClasses());
}
}
79 changes: 77 additions & 2 deletions tests/WebViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,15 @@ public function testRegisterJsFile(string $url): void
$this->assertStringContainsString('[ENDBODY]<script src="' . $url . '"></script>[/ENDBODY]', $html);
}

public function testRegisterJsFileWithInvalidPosition(): void
{
$webView = TestHelper::createWebView();

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid position of JS file.');
$webView->registerJsFile('/somefile.js', 42);
}

public function dataRegisterJsFileWithPosition(): array
{
return [
Expand Down Expand Up @@ -130,6 +139,15 @@ public function testRegisterCssFile(string $url): void
$this->assertStringContainsString('[HEAD]<link href="' . $url . '" rel="stylesheet">[/HEAD]', $html);
}

public function testRegisterCssFileWithInvalidPosition(): void
{
$webView = TestHelper::createWebView();

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid position of CSS file.');
$webView->registerCssFile('/somefile.css', 42);
}

public function dataRegisterCssFileWithPosition(): array
{
return [
Expand Down Expand Up @@ -190,7 +208,7 @@ public function testRegisterMeta(): void
$this->assertStringContainsString('[HEAD]<meta name="keywords" content="yii">[/HEAD]', $html);
}

public function testRegisterMetaTag(): void
public function testRegisterMetaTagAppend(): void
{
$webView = TestHelper::createWebView();

Expand All @@ -199,9 +217,35 @@ public function testRegisterMetaTag(): void
'content' => 'yii',
]));

$webView->registerMetaTag(Html::meta([
'name' => 'keywords',
'content' => 'yii3',
]));

$html = $webView->render('//positions.php');

$this->assertStringContainsString('[HEAD]<meta name="keywords" content="yii">[/HEAD]', $html);
$this->assertStringContainsString('<meta name="keywords" content="yii">', $html);
$this->assertStringContainsString('<meta name="keywords" content="yii3">', $html);
}

public function testRegisterMetaTagOverride(): void
{
$webView = TestHelper::createWebView();

$webView->registerMetaTag(Html::meta([
'name' => 'keywords',
'content' => 'yii',
]), 'keywords');

$webView->registerMetaTag(Html::meta([
'name' => 'keywords',
'content' => 'yii3',
]), 'keywords');

$html = $webView->render('//positions.php');

$this->assertStringContainsString('<meta name="keywords" content="yii3">', $html);
$this->assertStringNotContainsString('<meta name="keywords" content="yii">', $html);
}

public function dataRegisterLink(): array
Expand Down Expand Up @@ -247,6 +291,22 @@ public function testRegisterLinkTag(string $expected, ?int $position): void
$this->assertStringContainsString($expected, $html);
}

public function testRegisterLinkTagOverride(): void
{
$webView = TestHelper::createWebView();

$link1 = Html::link()->href('/main1.css');
$link2 = Html::link()->href('/main2.css');

$webView->registerLinkTag($link1, WebView::POSITION_HEAD, 'unique');
$webView->registerLinkTag($link2, WebView::POSITION_HEAD, 'unique');

$html = $webView->render('//positions.php');

$this->assertStringContainsString('<link href="/main2.css">', $html);
$this->assertStringNotContainsString('<link href="/main1.css">', $html);
}

public function dataRegisterCss(): array
{
return [
Expand Down Expand Up @@ -362,6 +422,7 @@ public function testRegisterJsAndRegisterScriptTag(): void
$script5 = Html::script('alert("script5");');
$script6 = Html::script('alert("script6");');
$js7 = 'alert(7);';
$js8 = 'alert(8);';

$webView->registerJs($js1);
$webView->registerJs($js2);
Expand All @@ -370,6 +431,7 @@ public function testRegisterJsAndRegisterScriptTag(): void
$webView->registerScriptTag($script5);
$webView->registerScriptTag($script6, WebView::POSITION_READY);
$webView->registerJs($js7, WebView::POSITION_READY);
$webView->registerJs($js8, WebView::POSITION_LOAD);
$html = $webView->render('//layout.php', ['content' => '']);

$this->assertStringContainsString(
Expand All @@ -386,6 +448,12 @@ public function testRegisterJsAndRegisterScriptTag(): void
'});</script>',
$html
);
$this->assertStringContainsString(
"<script>window.addEventListener('load', function(event) {\n" .
"$js8\n" .
'});</script>',
$html
);
}

public function testRegisterJsAndRegisterScriptTagWithAjax(): void
Expand Down Expand Up @@ -544,4 +612,11 @@ public function testFailAddJsVars(string $message, array $jsVars): void
$this->expectExceptionMessage($message);
TestHelper::createWebView()->addJsVars($jsVars);
}

public function testTitle(): void
{
$view = TestHelper::createWebView();
$view->setTitle('test');
$this->assertSame('test', $view->getTitle());
}
}

0 comments on commit 2af18ab

Please sign in to comment.