Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mimmi20 committed Nov 19, 2023
1 parent 598eb64 commit 3d69df6
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/View/Helper/ViteUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ private function hotServer(): string | null
private function manifestContents(): array
{
if ($this->buildDir === null) {
return [];
throw new RuntimeException('A Build Dir is required');
}

$manifestPath = $this->publicDir . '/' . $this->buildDir . '/manifest.json';
Expand All @@ -211,7 +211,7 @@ private function manifestContents(): array

if (!$content) {
throw new RuntimeException(
sprintf('coule not read and decode Vite manifest at: %s', $manifestPath),
sprintf('Could not read and decode Vite manifest at: %s', $manifestPath),
);
}

Expand Down
90 changes: 90 additions & 0 deletions tests/View/Helper/ViteUrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,35 @@ public function testJsWithHotRelaoding2(): void
self::assertSame($hotDir . '/' . $urlName, $object->js($name));
}

/** @throws RuntimeException */
public function testJsWithHotRelaoding3(): void
{
$root = vfsStream::setup('root');
$name = 'test.js';
$buildDir = 'test-build-dir';

$file1 = vfsStream::newFile('hot', 0777);
$file1->setContent('');

$root->addChild($file1);

$manifestPath = $root->url() . '/' . $buildDir . '/manifest.json';

$object = new ViteUrl($root->url(), $buildDir);

$view = $this->createMock(PhpRenderer::class);
$view->expects(self::never())
->method('__call');

$object->setView($view);

$this->expectException(RuntimeException::class);
$this->expectExceptionCode(0);
$this->expectExceptionMessage(sprintf('Vite manifest not found at: %s', $manifestPath));

$object->js($name);
}

/**
* @throws Exception
* @throws RuntimeException
Expand Down Expand Up @@ -237,4 +266,65 @@ public function testJsWithManifest2(): void

self::assertSame($buildDir . '/' . $file2, $object->js($name));
}

/**
* @throws Exception
* @throws RuntimeException
*/
public function testJsWithManifest3(): void
{
$root = vfsStream::setup('root');
$name = 'test.js';
$buildDir = null;

$object = new ViteUrl($root->url(), $buildDir);

$view = $this->createMock(PhpRenderer::class);
$view->expects(self::never())
->method('__call');

$object->setView($view);

$this->expectException(RuntimeException::class);
$this->expectExceptionCode(0);
$this->expectExceptionMessage('A Build Dir is required');

$object->js($name);
}

/**
* @throws Exception
* @throws RuntimeException
*/
public function testJsWithManifest4(): void
{
$root = vfsStream::setup('root');
$name = 'test.js';
$buildDir = 'test-build-dir';

$file1 = vfsStream::newFile('manifest.json', 0777);
$file1->setContent('');

$dir = vfsStream::newDirectory($buildDir);
$dir->addChild($file1);

$root->addChild($dir);
$manifestPath = $root->url() . '/' . $buildDir . '/manifest.json';

$object = new ViteUrl($root->url(), $buildDir);

$view = $this->createMock(PhpRenderer::class);
$view->expects(self::never())
->method('__call');

$object->setView($view);

$this->expectException(RuntimeException::class);
$this->expectExceptionCode(0);
$this->expectExceptionMessage(
sprintf('Could not read and decode Vite manifest at: %s', $manifestPath),
);

$object->js($name);
}
}

0 comments on commit 3d69df6

Please sign in to comment.