Skip to content

Commit

Permalink
Merge pull request #8 from mimmi20/updates
Browse files Browse the repository at this point in the history
test for manifest location for vite v5
  • Loading branch information
mimmi20 authored Dec 3, 2023
2 parents e4debcd + 08af230 commit 75b816b
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 17 deletions.
17 changes: 12 additions & 5 deletions src/View/Helper/ViteUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,17 +136,24 @@ private function manifestContents(): array
throw new RuntimeException('A Build Dir is required');
}

$manifestPath = $this->publicDir . '/' . $this->buildDir . '/manifest.json';

if (!is_file($manifestPath)) {
throw new RuntimeException(sprintf('Vite manifest not found at: %s', $manifestPath));
$manifestPathV4 = $this->publicDir . '/' . $this->buildDir . '/manifest.json';
$manifestPathV5 = $this->publicDir . '/' . $this->buildDir . '/.vite/manifest.json';

if (is_file($manifestPathV5)) {
$manifestPath = $manifestPathV5;
} elseif (is_file($manifestPathV4)) {
$manifestPath = $manifestPathV4;
} else {
throw new RuntimeException(
sprintf('Vite manifest not found at %s or at %s', $manifestPathV4, $manifestPathV5),
);
}

$content = file_get_contents($manifestPath);

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

Expand Down
108 changes: 96 additions & 12 deletions tests/View/Helper/ViteUrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ public function testJsWithHotRelaoding3(): void

$root->addChild($file1);

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

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

Expand All @@ -158,7 +159,9 @@ public function testJsWithHotRelaoding3(): void

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

$object->js($name);
}
Expand All @@ -177,8 +180,9 @@ public function testJsWithoutManifest(): void

$root->addChild($dir);

$publicDir = 'test-public-dir';
$manifestPath = $publicDir . '/' . $buildDir . '/manifest.json';
$publicDir = 'test-public-dir';
$manifestPathV4 = $publicDir . '/' . $buildDir . '/manifest.json';
$manifestPathV5 = $publicDir . '/' . $buildDir . '/.vite/manifest.json';

$object = new ViteUrl($publicDir, $buildDir);

Expand All @@ -190,7 +194,9 @@ public function testJsWithoutManifest(): void

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

$object->js($name);
}
Expand Down Expand Up @@ -316,7 +322,7 @@ public function testJsWithManifest4(): void
$this->expectException(RuntimeException::class);
$this->expectExceptionCode(0);
$this->expectExceptionMessage(
sprintf('Could not read and decode Vite manifest at: %s', $manifestPath),
sprintf('Could not read Vite manifest at: %s', $manifestPath),
);

$object->js($name);
Expand Down Expand Up @@ -358,6 +364,42 @@ public function testJsWithManifest5(): void
$object->js($name);
}

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

$file1 = vfsStream::newFile('manifest.json', 0777);
$file1->setContent((string) json_encode([$name => ['file' => $file]]));

$dir2 = vfsStream::newDirectory('.vite');
$dir2->addChild($file1);

$dir1 = vfsStream::newDirectory($buildDir);
$dir1->addChild($dir2);

$root->addChild($dir1);

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

$view = $this->createMock(PhpRenderer::class);
$view->expects(self::once())
->method('__call')
->with('serverUrl', ['/' . $buildDir . '/' . $file])
->willReturn('/' . $buildDir . '/' . $file2);

$object->setView($view);

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

/** @throws RuntimeException */
public function testCssWithoutPublicDir(): void
{
Expand Down Expand Up @@ -469,7 +511,8 @@ public function testCssWithHotRelaoding3(): void

$root->addChild($file1);

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

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

Expand All @@ -481,7 +524,9 @@ public function testCssWithHotRelaoding3(): void

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

$object->css($name);
}
Expand All @@ -500,8 +545,9 @@ public function testCssWithoutManifest(): void

$root->addChild($dir);

$publicDir = 'test-public-dir';
$manifestPath = $publicDir . '/' . $buildDir . '/manifest.json';
$publicDir = 'test-public-dir';
$manifestPathV4 = $publicDir . '/' . $buildDir . '/manifest.json';
$manifestPathV5 = $publicDir . '/' . $buildDir . '/.vite/manifest.json';

$object = new ViteUrl($publicDir, $buildDir);

Expand All @@ -513,7 +559,9 @@ public function testCssWithoutManifest(): void

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

$object->css($name);
}
Expand Down Expand Up @@ -639,7 +687,7 @@ public function testCssWithManifest4(): void
$this->expectException(RuntimeException::class);
$this->expectExceptionCode(0);
$this->expectExceptionMessage(
sprintf('Could not read and decode Vite manifest at: %s', $manifestPath),
sprintf('Could not read Vite manifest at: %s', $manifestPath),
);

$object->css($name);
Expand Down Expand Up @@ -680,4 +728,40 @@ public function testCssWithManifest5(): void

$object->css($name);
}

/**
* @throws Exception
* @throws RuntimeException
*/
public function testCssWithManifest6(): void
{
$root = vfsStream::setup('root');
$name = 'test.css';
$buildDir = 'test-build-dir';
$file = 'test-xyz.css';
$file2 = 'test-xyz2.css';

$file1 = vfsStream::newFile('manifest.json', 0777);
$file1->setContent((string) json_encode([$name => ['file' => $file]]));

$dir2 = vfsStream::newDirectory('.vite');
$dir2->addChild($file1);

$dir1 = vfsStream::newDirectory($buildDir);
$dir1->addChild($dir2);

$root->addChild($dir1);

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

$view = $this->createMock(PhpRenderer::class);
$view->expects(self::once())
->method('__call')
->with('serverUrl', ['/' . $buildDir . '/' . $file])
->willReturn('/' . $buildDir . '/' . $file2);

$object->setView($view);

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

0 comments on commit 75b816b

Please sign in to comment.