Skip to content

Commit

Permalink
Test coverage improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
asgrim committed May 16, 2024
1 parent 812a814 commit 9629815
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Output\OutputInterface;

/** @internal This is not public API for PIE, so should not be depended upon unless you accept the risk of BC breaks */
final class Container
{
public static function factory(): ContainerInterface
Expand Down
2 changes: 2 additions & 0 deletions src/Downloading/AssertHttp.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@

use function sprintf;

/** @internal This is not public API for PIE, so should not be depended upon unless you accept the risk of BC breaks */
class AssertHttp
{
/** Helper assertion that includes the HTTP response body when the HTTP status code does not match */
public static function responseStatusCode(int $expectedStatusCode, ResponseInterface $response): void
{
$actualStatusCode = $response->getStatusCode();
Expand Down
8 changes: 7 additions & 1 deletion src/Downloading/Path.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@

use const DIRECTORY_SEPARATOR;

/** @internal This is not public API for PIE, so should not be depended upon unless you accept the risk of BC breaks */
final class Path
{
/** @return non-empty-string */
/**
* Static helper to generate a vaguely random temporary path. This is not intended to be cryptographically secure,
* nor do we need to support high concurrency or strong randomness.
*
* @return non-empty-string
*/
public static function vaguelyRandomTempPath(): string
{
$localTempPath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid('pie_downloader_', true);
Expand Down
24 changes: 24 additions & 0 deletions test/unit/Downloading/AssertHttpTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Php\PieUnitTest\Downloading;

use GuzzleHttp\Psr7\Response;
use InvalidArgumentException;
use Php\Pie\Downloading\AssertHttp;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;

#[CoversClass(AssertHttp::class)]
final class AssertHttpTest extends TestCase
{
public function testResponseStatusCode(): void
{
$response = new Response(404, [], 'some body content');

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Expected HTTP 201 response, got 404 - response: some body content');
AssertHttp::responseStatusCode(201, $response);
}
}
19 changes: 19 additions & 0 deletions test/unit/Downloading/Exception/CouldNotFindReleaseAssetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
use Php\Pie\DependencyResolver\Package;
use Php\Pie\Downloading\Exception\CouldNotFindReleaseAsset;
use Php\Pie\ExtensionName;
use Php\Pie\Platform\Architecture;
use Php\Pie\Platform\OperatingSystem;
use Php\Pie\Platform\TargetPhp\PhpBinaryPath;
use Php\Pie\Platform\TargetPlatform;
use Php\Pie\Platform\ThreadSafetyMode;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;

Expand All @@ -30,4 +35,18 @@ public function testForPackageWithMissingTag(): void

self::assertSame('Could not find release by tag name for foo/bar:1.2.3', $exception->getMessage());
}

public function testForMissingWindowsCompiler(): void
{
$phpBinary = PhpBinaryPath::fromCurrentProcess();
$exception = CouldNotFindReleaseAsset::forMissingWindowsCompiler(new TargetPlatform(
OperatingSystem::NonWindows,
$phpBinary,
Architecture::x86,
ThreadSafetyMode::NonThreadSafe,
null,
));

self::assertSame('Could not determine Windows Compiler for PHP ' . $phpBinary->version() . ' on NonWindows', $exception->getMessage());
}
}
21 changes: 21 additions & 0 deletions test/unit/Downloading/PathTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Php\PieUnitTest\Downloading;

use Php\Pie\Downloading\Path;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;

#[CoversClass(Path::class)]
final class PathTest extends TestCase
{
public function testVaguelyRandomTempPath(): void
{
$path1 = Path::vaguelyRandomTempPath();
$path2 = Path::vaguelyRandomTempPath();

self::assertNotSame($path1, $path2);
}
}
38 changes: 38 additions & 0 deletions test/unit/Platform/ArchitectureTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Php\PieUnitTest\Platform;

use Php\Pie\Platform\Architecture;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

#[CoversClass(Architecture::class)]
final class ArchitectureTest extends TestCase
{
/**
* @return array<non-empty-string, array{0: non-empty-string, 1: Architecture}>
*
* @psalm-suppress PossiblyUnusedMethod
*/
public static function architectureMapProvider(): array
{
return [
'x64' => ['x64', Architecture::x86_64],
'x86_64' => ['x86_64', Architecture::x86_64],
'AMD64' => ['AMD64', Architecture::x86_64],
'arm64' => ['arm64', Architecture::arm64],
'x86' => ['x86', Architecture::x86],
'something' => ['something', Architecture::x86],
];
}

/** @param non-empty-string $architectureString */
#[DataProvider('architectureMapProvider')]
public function testParseArchitecture(string $architectureString, Architecture $expectedArchitecture): void
{
self::assertSame($expectedArchitecture, Architecture::parseArchitecture($architectureString));
}
}
25 changes: 25 additions & 0 deletions test/unit/Platform/TargetPhp/PhpBinaryPathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@

namespace Php\PieUnitTest\Platform\TargetPhp;

use Php\Pie\Platform\Architecture;
use Php\Pie\Platform\OperatingSystem;
use Php\Pie\Platform\TargetPhp\PhpBinaryPath;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Process\Process;

use function assert;
use function defined;
use function file_exists;
use function is_executable;
use function php_uname;
use function sprintf;
use function trim;

Expand Down Expand Up @@ -52,6 +57,15 @@ public function testFromPhpConfigExecutable(): void
);
}

public function testOperatingSystem(): void
{
self::assertSame(
defined('PHP_WINDOWS_VERSION_BUILD') ? OperatingSystem::Windows : OperatingSystem::NonWindows,
PhpBinaryPath::fromCurrentProcess()
->operatingSystem(),
);
}

public function testMajorMinorVersion(): void
{
self::assertSame(
Expand All @@ -61,6 +75,17 @@ public function testMajorMinorVersion(): void
);
}

public function testMachineType(): void
{
$myUnameMachineType = php_uname('m');
assert($myUnameMachineType !== '');
self::assertSame(
Architecture::parseArchitecture($myUnameMachineType),
PhpBinaryPath::fromCurrentProcess()
->machineType(),
);
}

public function testPhpIntSize(): void
{
self::assertSame(
Expand Down

0 comments on commit 9629815

Please sign in to comment.