Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dakujem committed Jan 3, 2023
1 parent e0faa62 commit 68db0c0
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/ViteEntryAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public function __toString(): string
public function jsonSerialize(): array
{
return [
'modules' => $this->modules(),
'css' => $this->css(),
'modules' => $this->modules,
'css' => $this->css,
];
}
}
47 changes: 47 additions & 0 deletions tests/ViteAsset.default.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);


namespace Dakujem\Trest;

require_once __DIR__ . '/common.php';

use Dakujem\Peat\ViteEntryAsset;
use Tester\Assert;
use Tester\TestCase;

/**
* This test tests the capabilities on the official Vite backend integration example.
*
* @author Andrej Rypak (dakujem) <xrypak@gmail.com>
*/
class _DefaultViteAssetTest extends TestCase
{
public function testEmptyAsset()
{
$asset = new ViteEntryAsset();

Assert::same('', (string)$asset);
Assert::same([], $asset->modules());
Assert::same([], $asset->css());
Assert::same(['modules' => [], 'css' => [],], $asset->jsonSerialize());
}

public function testNonEmptyAsset()
{
$asset = new ViteEntryAsset(['a', 'b'], ['c', 'd']);

Assert::same('<script type="module" src="a"></script>' . "\n" .
'<script type="module" src="b"></script>' . "\n" .
'<link rel="stylesheet" href="c" />' . "\n" .
'<link rel="stylesheet" href="d" />'
, (string)$asset);
Assert::same(['a', 'b'], $asset->modules());
Assert::same(['c', 'd'], $asset->css());
Assert::same(['modules' => ['a', 'b'], 'css' => ['c', 'd'],], $asset->jsonSerialize());
}
}

// run the test
(new _DefaultViteAssetTest)->run();
42 changes: 42 additions & 0 deletions tests/ViteBridge.default.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ class _DefaultExampleViteBridgeTest extends TestCase
{
protected function setUp()
{
if (file_exists(TEMP . '/vite.default.php')) {
unlink(TEMP . '/vite.default.php');
}
parent::setUp();
}

Expand Down Expand Up @@ -299,6 +302,45 @@ class _DefaultExampleViteBridgeTest extends TestCase
'Prefix reflected in URLs',
);
}

public function testCachePopulation()
{
$bridgeService = $this->getBridgeService();
$bridgeService->populateCache();

$filename = TEMP . '/vite.default.php';
Assert::true(file_exists($filename), 'Cache file has not been written.');
$map = require $filename;
Assert::type('array', $map);
Assert::same(3, sizeof($map));

Assert::same(['main.js', 'views/foo.js', '_shared.83069a53.js'], array_keys($map));

// now use invalid manifest to see reading from the cache file
$bridgeService2 = new ViteBridge(
FIXTURES . '/invalid.json',
$filename,
'/something',
);
$locator = $bridgeService2->makePassiveEntryLocator(false);

$assets = $locator->entry('main.js');
Assert::same(
'<script type="module" src="/something/assets/main.4889e940.js"></script>' .
"\n" .
'<link rel="stylesheet" href="/something/assets/main.b82dbe22.css" />',
(string)$assets,
'Prefix reflected in URLs',
);
$assets = $locator->entry('views/foo.js');
Assert::same(
'<script type="module" src="/something/assets/foo.869aea0d.js"></script>' .
"\n" .
'<script type="module" src="/something/assets/shared.83069a53.js"></script>',
(string)$assets,
'Loaded from cache',
);
}
}

// run the test
Expand Down

0 comments on commit 68db0c0

Please sign in to comment.