Skip to content

Commit

Permalink
Fix bug and better tests (#14)
Browse files Browse the repository at this point in the history
Tests and Manifest Generation fixed
  • Loading branch information
Spomky authored Dec 22, 2023
1 parent 6e786a1 commit 060327b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 25 deletions.
5 changes: 4 additions & 1 deletion src/Command/GenerateManifestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ private function storeFile(string $data, string $prefixUrl, string $storageFolde
$this->filesystem->remove($tempFilename);

return [

Check failure on line 115 in src/Command/GenerateManifestCommand.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Test on ubuntu-latest

Method SpomkyLabs\PwaBundle\Command\GenerateManifestCommand::storeFile() should return array{src: string, type: string} but returns array{src: non-falsy-string, type: string|null}.
'src' => sprintf('%s%s', $prefixUrl, $filename),
'src' => sprintf('%s/%s', $prefixUrl, $filename),
'type' => $mime,
];
}
Expand Down Expand Up @@ -370,6 +370,9 @@ private function processProgressBar(ProgressBar $progressBar, string $type, stri

private function processActions(SymfonyStyle $io, array $manifest): array|int
{
if ($this->config['file_handlers'] === []) {
return $manifest;
}
$io->info('Processing file handlers');
foreach ($manifest['file_handlers'] as $id => $handler) {
if (str_starts_with((string) $handler['action'], '/')) {
Expand Down
50 changes: 26 additions & 24 deletions tests/Functional/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,64 +8,66 @@
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\Filesystem\Filesystem;

/**
* @internal
*/
final class CommandTest extends KernelTestCase
{
protected function tearDown(): void
private static Application $application;

protected function setUp(): void
{
$filesystem = self::getContainer()->get('filesystem');
$filesystem->remove(sprintf('%s/samples', self::$kernel->getCacheDir()));
self::cleanupFolder();
self::$application = new Application(self::$kernel);
parent::setUp();
}

protected function tearDown(): void
{
self::cleanupFolder();
parent::tearDown();
}

#[Test]
public static function theCommandCanGenerateTheManifestAndIcons(): void
{
// Given
$kernel = self::bootKernel();
$application = new Application($kernel);
$filesystem = self::getContainer()->get('filesystem');
$filesystem->remove(sprintf('%s/samples', $kernel->getCacheDir()));

$command = $application->find('pwa:build');
$command = self::$application->find('pwa:build');
$commandTester = new CommandTester($command);

// When
$commandTester->execute([]);

// Then
$commandTester->assertCommandIsSuccessful();
$output = $commandTester->getDisplay();
static::assertStringContainsString('PWA Manifest Generator', $output);
static::assertFileExists(sprintf('%s/samples/manifest/my-pwa.json', $kernel->getCacheDir()));
static::assertDirectoryExists(sprintf('%s/samples/icons', $kernel->getCacheDir()));
static::assertDirectoryExists(sprintf('%s/samples/screenshots', $kernel->getCacheDir()));
static::assertDirectoryExists(sprintf('%s/samples/shortcut_icons', $kernel->getCacheDir()));
static::assertStringContainsString('PWA Manifest Generator', $commandTester->getDisplay());
static::assertFileExists(sprintf('%s/samples/manifest/my-pwa.json', self::$kernel->getCacheDir()));
static::assertDirectoryExists(sprintf('%s/samples/icons', self::$kernel->getCacheDir()));
static::assertDirectoryExists(sprintf('%s/samples/screenshots', self::$kernel->getCacheDir()));
static::assertDirectoryExists(sprintf('%s/samples/shortcut_icons', self::$kernel->getCacheDir()));
}

#[Test]
public static function theCommandCanCreateTheServiceWorker(): void
{
// Given
$kernel = self::bootKernel();
$application = new Application($kernel);
$filesystem = self::getContainer()->get('filesystem');
$filesystem->remove(sprintf('%s/samples', $kernel->getCacheDir()));

$command = $application->find('pwa:sw');
$command = self::$application->find('pwa:sw');
$commandTester = new CommandTester($command);

// When
$commandTester->execute([]);

// Then
$commandTester->assertCommandIsSuccessful();
$output = $commandTester->getDisplay();
static::assertStringContainsString('Workbox Service Worker', $output);
static::assertFileExists(sprintf('%s/samples/sw/my-sw.js', $kernel->getCacheDir()));
static::assertStringContainsString('Workbox Service Worker', $commandTester->getDisplay());
static::assertFileExists(sprintf('%s/samples/sw/my-sw.js', self::$kernel->getCacheDir()));
}

private static function cleanupFolder(): void
{
$filesystem = self::getContainer()->get(Filesystem::class);
$filesystem->remove(sprintf('%s/samples', self::$kernel->getCacheDir()));
}
}

0 comments on commit 060327b

Please sign in to comment.