Skip to content

Commit 7c4d75a

Browse files
committed
tests(archive): add unit tests for internal
1 parent 2577183 commit 7c4d75a

File tree

5 files changed

+193
-0
lines changed

5 files changed

+193
-0
lines changed

context.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
---
2+
13
$schema: 'https://raw.githubusercontent.com/context-hub/generator/refs/heads/main/json-schema.json'
24

35
import:
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Internal\DLoad\Tests\Unit\Module\Archive\Internal;
6+
7+
use Internal\DLoad\Module\Archive\Internal\Archive;
8+
use PHPUnit\Framework\Attributes\CoversClass;
9+
use PHPUnit\Framework\TestCase;
10+
11+
#[CoversClass(Archive::class)]
12+
final class ArchiveTest extends TestCase
13+
{
14+
public function testConstructorThrowsExceptionWhenFileDoesNotExist(): void
15+
{
16+
// Arrange
17+
$file = $this->createMock(\SplFileInfo::class);
18+
$file->method('isFile')->willReturn(false);
19+
$file->method('getFilename')->willReturn('non-existent.zip');
20+
21+
// Assert
22+
$this->expectException(\InvalidArgumentException::class);
23+
$this->expectExceptionMessage('Archive "non-existent.zip" is not a file.');
24+
25+
// Act
26+
$this->createArchiveInstance($file);
27+
}
28+
29+
public function testConstructorThrowsExceptionWhenFileIsNotReadable(): void
30+
{
31+
// Arrange
32+
$file = $this->createMock(\SplFileInfo::class);
33+
$file->method('isFile')->willReturn(true);
34+
$file->method('isReadable')->willReturn(false);
35+
$file->method('getFilename')->willReturn('unreadable.zip');
36+
37+
// Assert
38+
$this->expectException(\InvalidArgumentException::class);
39+
$this->expectExceptionMessage('Archive file "unreadable.zip" is not readable.');
40+
41+
// Act
42+
$this->createArchiveInstance($file);
43+
}
44+
45+
public function testConstructorSucceedsWithValidFile(): void
46+
{
47+
// Arrange
48+
$file = $this->createMock(\SplFileInfo::class);
49+
$file->method('isFile')->willReturn(true);
50+
$file->method('isReadable')->willReturn(true);
51+
52+
// Act
53+
$archive = $this->createArchiveInstance($file);
54+
55+
// Assert
56+
self::assertInstanceOf(Archive::class, $archive);
57+
}
58+
59+
/**
60+
* Creates a concrete implementation of the abstract Archive class for testing
61+
*/
62+
private function createArchiveInstance(\SplFileInfo $file): Archive
63+
{
64+
return new class($file) extends Archive {
65+
public function extract(): \Generator
66+
{
67+
// Minimal implementation for testing the constructor
68+
yield 'test' => new \SplFileInfo('test');
69+
}
70+
};
71+
}
72+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Internal\DLoad\Tests\Unit\Module\Archive\Internal;
6+
7+
use Internal\DLoad\Module\Archive\Exception\ArchiveException;
8+
use Internal\DLoad\Module\Archive\Internal\PharAwareArchive;
9+
use PHPUnit\Framework\Attributes\CoversClass;
10+
use PHPUnit\Framework\TestCase;
11+
12+
#[CoversClass(PharAwareArchive::class)]
13+
final class PharAwareArchiveTest extends TestCase
14+
{
15+
private \SplFileInfo $fileInfo;
16+
17+
public function testExtractThrowsExceptionWhenArchiveIsNotReadable(): void
18+
{
19+
// Arrange
20+
$pharData = $this->createMock(\PharData::class);
21+
$pharData->method('isReadable')->willReturn(false);
22+
$pharData->method('getPathname')->willReturn('unreadable.phar');
23+
24+
$archive = $this->createPharAwareArchive($pharData);
25+
26+
// Assert
27+
$this->expectException(ArchiveException::class);
28+
$this->expectExceptionMessage('Could not open "unreadable.phar" for reading.');
29+
30+
// Act
31+
\iterator_to_array($archive->extract());
32+
}
33+
34+
protected function setUp(): void
35+
{
36+
// Arrange
37+
$this->fileInfo = $this->createMock(\SplFileInfo::class);
38+
$this->fileInfo->method('isFile')->willReturn(true);
39+
$this->fileInfo->method('isReadable')->willReturn(true);
40+
}
41+
42+
/**
43+
* Creates a concrete implementation of the abstract PharAwareArchive for testing
44+
*/
45+
private function createPharAwareArchive(\PharData $pharData): PharAwareArchive
46+
{
47+
return new class($this->fileInfo, $pharData) extends PharAwareArchive {
48+
private \PharData $testPharData;
49+
50+
public function __construct(\SplFileInfo $archive, \PharData $pharData)
51+
{
52+
$this->testPharData = $pharData;
53+
parent::__construct($archive);
54+
}
55+
56+
protected function open(\SplFileInfo $file): \PharData
57+
{
58+
return $this->testPharData;
59+
}
60+
};
61+
}
62+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Internal\DLoad\Tests\Unit\Module\Archive\Internal;
6+
7+
use Internal\DLoad\Module\Archive\Internal\TarPharArchive;
8+
use PHPUnit\Framework\Attributes\CoversClass;
9+
use PHPUnit\Framework\TestCase;
10+
11+
#[CoversClass(TarPharArchive::class)]
12+
final class TarPharArchiveTest extends TestCase
13+
{
14+
public function testConstructorValidatesFile(): void
15+
{
16+
// Arrange
17+
$file = $this->createMock(\SplFileInfo::class);
18+
$file->method('isFile')->willReturn(true);
19+
$file->method('isReadable')->willReturn(false);
20+
$file->method('getFilename')->willReturn('unreadable.tar.gz');
21+
22+
// Assert
23+
$this->expectException(\InvalidArgumentException::class);
24+
$this->expectExceptionMessage('Archive file "unreadable.tar.gz" is not readable.');
25+
26+
// Act
27+
new TarPharArchive($file);
28+
}
29+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Internal\DLoad\Tests\Unit\Module\Archive\Internal;
6+
7+
use Internal\DLoad\Module\Archive\Internal\ZipPharArchive;
8+
use PHPUnit\Framework\Attributes\CoversClass;
9+
use PHPUnit\Framework\TestCase;
10+
11+
#[CoversClass(ZipPharArchive::class)]
12+
final class ZipPharArchiveTest extends TestCase
13+
{
14+
public function testConstructorValidatesFile(): void
15+
{
16+
// Arrange
17+
$file = $this->createMock(\SplFileInfo::class);
18+
$file->method('isFile')->willReturn(false);
19+
$file->method('getFilename')->willReturn('invalid.zip');
20+
21+
// Assert
22+
$this->expectException(\InvalidArgumentException::class);
23+
$this->expectExceptionMessage('Archive "invalid.zip" is not a file.');
24+
25+
// Act
26+
new ZipPharArchive($file);
27+
}
28+
}

0 commit comments

Comments
 (0)