Skip to content

Commit

Permalink
Add missing extension requirements to manifest.xml
Browse files Browse the repository at this point in the history
  • Loading branch information
villfa committed Nov 25, 2022
1 parent 0c97221 commit 5a7402d
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 9 deletions.
1 change: 1 addition & 0 deletions manifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<requires>
<php version="^7.1 || ^8.0">
<ext name="json"/>
<ext name="tokenizer"/>
</php>
</requires>
</phar>
97 changes: 88 additions & 9 deletions tests/Integration/ManifestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,103 @@
namespace Churn\Tests\Integration;

use Churn\Tests\BaseTestCase;
use PharIo\Manifest\Manifest;
use PharIo\Manifest\ManifestLoader;
use PharIo\Manifest\PhpExtensionRequirement;
use RuntimeException;

class ManifestTest extends BaseTestCase
{
/** @test */
public function manifest_is_valid(): void
/** @var Manifest */
private $manifest;

/** @return void */
public function setUp()
{
parent::setUp();

$path = __DIR__ . '/../../manifest.xml';
self::assertTrue(is_file($path), 'manifest.xml not found');

$manifest = ManifestLoader::fromFile($path);
$this->manifest = ManifestLoader::fromFile($path);
}

/**
* @psalm-suppress InvalidCast
* @phpstan-ignore-next-line
*/
$name = method_exists($manifest->getName(), 'asString') ? $manifest->getName()->asString() : (string) $manifest->getName();
/** @test */
public function manifest_is_valid(): void
{
$name = $this->getStringRepresentation($this->manifest->getName());
self::assertSame('bmitch/churn-php', $name);
self::assertGreaterThan(0, $manifest->getRequirements()->count());
self::assertGreaterThan(0, $this->manifest->getRequirements()->count());
}

/** @test */
public function manifest_requires_same_extensions_than_composer(): void
{
self::assertEqualsCanonicalizing(
$this->getComposerExtensionRequirements(),
$this->getManifestExtensionRequirements()
);
}

/**
* @return array<string>
*/
private function getManifestExtensionRequirements(): array
{
$requirements = [];
foreach ($this->manifest->getRequirements() as $requirement) {
if (!$requirement instanceof PhpExtensionRequirement) {
continue;
}

$requirements[] = $this->getStringRepresentation($requirement);
}

return $requirements;
}

/**
* @return array<string>
*/
private function getComposerExtensionRequirements(): array
{
$path = __DIR__ . '/../../composer.json';
self::assertNotFalse($contents = file_get_contents($path));
/** @var mixed $json */
$json = json_decode($contents, true);
self::assertIsArray($json);
self::assertArrayHasKey('require', $json);
self::assertIsArray($json['require']);

$requirements = [];
foreach (array_keys($json['require']) as $requirement) {
if (!is_string($requirement) || strpos($requirement, 'ext-') !== 0) {
continue;
}

$requirements[] = substr($requirement, 4);
}

return $requirements;
}

/**
* @param object $object
*/
private function getStringRepresentation($object): string
{
$result = null;
if (method_exists($object, 'asString')) {
/** @var mixed $result */
$result = $object->asString();
} elseif (method_exists($object, '__toString')) {
/** @var mixed $result */
$result = $object->__toString();
}
if (!is_string($result)) {
throw new RuntimeException('String representation not found');
}

return $result;
}
}

0 comments on commit 5a7402d

Please sign in to comment.