Skip to content

Commit

Permalink
Added a test for AssetPathExtractor service
Browse files Browse the repository at this point in the history
- Ensured build/index.html exists in constructor
  • Loading branch information
Macavity committed Jan 17, 2024
1 parent d0de968 commit 4a8d24f
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 10 deletions.
6 changes: 5 additions & 1 deletion src/Service/AssetPathExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@ class AssetPathExtractor

public function __construct(string $indexPath, string $buildDir, LoggerInterface $logger)
{
if (!file_exists($indexPath)) {

Check failure on line 14 in src/Service/AssetPathExtractor.php

View workflow job for this annotation

GitHub Actions / Symfony Build (8.1)

Constructor of class App\Service\AssetPathExtractor has an unused parameter $logger.
return;
}

$content = file_get_contents($indexPath);
preg_match_all('/<script src="(.+?)" type="module"><\/script>/', $content, $matches);
foreach ($matches[1] as $path) {
$baseName = $this->extractBaseName($path, 'js');
$this->javascriptFiles[$baseName] = $buildDir . $path;
}

preg_match_all('/<link rel="stylesheet" href="(.+?)">/', $content, $matches);
preg_match_all('/<link rel="stylesheet" href="(.+?)"\s(\/)?>/', $content, $matches);
foreach ($matches[1] as $path) {
$baseName = $this->extractBaseName($path, 'css');
$this->cssFiles[$baseName] = $buildDir . $path;
Expand Down
12 changes: 3 additions & 9 deletions tests/Api/TransactionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,11 @@ public function testGetCollection(): void
'@context' => '/api/contexts/Transaction',
'@id' => '/api/transactions',
'@type' => 'hydra:Collection',
'hydra:totalItems' => 100,
'hydra:view' => [
'@id' => '/api/transactions?page=1',
'@type' => 'hydra:PartialCollectionView',
'hydra:first' => '/api/transactions?page=1',
'hydra:last' => '/api/transactions?page=4',
'hydra:next' => '/api/transactions?page=2',
],
'hydra:totalItems' => 10,
]);

// Because test fixtures are automatically loaded between each test, you can assert on them
$this->assertCount(30, $response->toArray()['hydra:member']);
$this->assertCount(10, $response->toArray()['hydra:member']);
// Asserts that the returned JSON is validated by the JSON Schema generated for this resource by API Platform
// This generated JSON Schema is also used in the OpenAPI spec!
$this->assertMatchesResourceCollectionJsonSchema(Transaction::class);
Expand Down
32 changes: 32 additions & 0 deletions tests/Service/AssetPathExtractorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace App\Tests\Service;

use App\Service\AssetPathExtractor;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;

class AssetPathExtractorTest extends TestCase
{
private LoggerInterface $loggerMock;
private string $indexPath = 'tests/mock/index.html';
private string $buildDir = '/build/';

protected function setUp(): void
{
$this->loggerMock = $this->createMock(LoggerInterface::class);
}

public function testGetScriptPath(): void
{
$extractor = new AssetPathExtractor($this->indexPath, $this->buildDir, $this->loggerMock);
$this->assertEquals($this->buildDir . 'runtime.abcdef.js', $extractor->getScriptPath('runtime'));
$this->assertEquals($this->buildDir . 'main.789012.js', $extractor->getScriptPath('main'));
}

public function testGetCssPath(): void
{
$extractor = new AssetPathExtractor($this->indexPath, $this->buildDir, $this->loggerMock);
$this->assertEquals($this->buildDir . 'styles.123456.css', $extractor->getCssPath('styles'));
}
}
10 changes: 10 additions & 0 deletions tests/mock/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles.123456.css" />
</head>
<body>
<script src="runtime.abcdef.js" type="module"></script>
<script src="main.789012.js" type="module"></script>
</body>
</html>

0 comments on commit 4a8d24f

Please sign in to comment.