Skip to content

Commit

Permalink
Merge pull request #2 from CPS-IT/feature/fix-installed-packages-trait
Browse files Browse the repository at this point in the history
[BUGFIX] Fix handling of `InstalledPackagesTrait`
  • Loading branch information
dwenzel committed Jan 13, 2021
2 parents c021779 + 3060a2a commit 98bf372
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 42 deletions.
14 changes: 7 additions & 7 deletions src/InstalledPackagesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ trait InstalledPackagesTrait
static public function getInstalledPackages(): array
{
if (self::propertyExists(DescriberInterface::INSTALLED_PACKAGES)) {
return self::$installedPackages;
return self::$resolvedPackages;
}

return [];
Expand All @@ -56,7 +56,7 @@ static public function propertyExists(string $propertyName): bool
);
}
if (!static::arePackagesResolved()) {
static::resolvePackages();
static::resolvePackages($propertyName);
}
return true;
}
Expand All @@ -72,7 +72,7 @@ static public function getInstalledPackage(string $name): ?Package
if (!self::isPackageInstalled($name)) {
return null;
}
return new Package(self::$installedPackages[$name]);
return new Package(self::$resolvedPackages[$name]);
}

/**
Expand All @@ -84,19 +84,19 @@ static public function getInstalledPackage(string $name): ?Package
static public function isPackageInstalled(string $name): bool
{
self::propertyExists(DescriberInterface::INSTALLED_PACKAGES);
return (array_key_exists($name, self::$installedPackages));
return (array_key_exists($name, self::$resolvedPackages));
}

protected static function arePackagesResolved(): bool
{
return is_array(static::$resolvedPackages);
}

protected static function resolvePackages(): void
protected static function resolvePackages(string $propertyName): void
{
if (!property_exists(static::class, DescriberInterface::INSTALLED_PACKAGES)) {
if (!property_exists(static::class, $propertyName)) {
return;
}
static::$resolvedPackages = unserialize(static::${DescriberInterface::INSTALLED_PACKAGES}) ?: [];
static::$resolvedPackages = unserialize(static::${$propertyName}) ?: [];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use CPSIT\Auditor\DescriberInterface;
use CPSIT\Auditor\Dto\Package;
use CPSIT\Auditor\InstalledPackagesTrait;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

/***************************************************************
Expand All @@ -25,51 +24,47 @@
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/

class MockClassWithPropertyInstalledPackages
{
use InstalledPackagesTrait;

static protected $installedPackages = [
'foo' => ['bar']
];
}
class MockClassWithoutPropertyInstalledPackage
{
use InstalledPackagesTrait;
}

/**
* Class InstalledPackagesTraitTest
* @coversDefaultClass \CPSIT\Auditor\InstalledPackagesTrait
*/
class InstalledPackagesTraitTest extends TestCase
{
/**
* @var InstalledPackagesTrait|MockObject
* @var InstalledPackagesTrait
*/
protected $subject;
protected $subjectWithPropertyInstalledPackages;

protected $packages = [
'foo' => ['bar']
];
/**
* @var InstalledPackagesTrait
*/
protected $subjectWithoutPropertyInstalledPackages;

public function setUp(): void
{
$this->subject = $this->getMockBuilder(InstalledPackagesTrait::class)
->getMockForTrait();
$this->subjectWithPropertyInstalledPackages = new class()
{
use InstalledPackagesTrait;

protected static $installedPackages = 'a:1:{s:3:"foo";a:1:{i:0;s:3:"bar";}}';
};
$this->subjectWithoutPropertyInstalledPackages = new class()
{
use InstalledPackagesTrait;
};
}

public function testGetInstalledPackagesReturnsArray(): void
{
$this->assertIsArray(
$this->subject::getInstalledPackages()
self::assertIsArray(
$this->subjectWithPropertyInstalledPackages::getInstalledPackages()
);
}

public function testIsPackageInstalledReturnsTrueForInstalledPackage(): void
{
$this->assertTrue(
MockClassWithInstalledPackages::isPackageInstalled('foo')
self::assertTrue(
$this->subjectWithPropertyInstalledPackages::isPackageInstalled('foo')
);
}

Expand All @@ -78,43 +73,43 @@ public function testIsPackageInstalledReturnsTrueForInstalledPackage(): void
*/
public function testIsPackageInstalledReturnsFalseForMissingPackage(): void
{
$this->assertFalse(
MockClassWithInstalledPackages::isPackageInstalled('anyPackageName')
self::assertFalse(
$this->subjectWithPropertyInstalledPackages::isPackageInstalled('anyPackageName')
);
}

/**
* @covers ::propertyExists
* @expectedException \OutOfBoundsException
* @expextedExceptionCode 1557047757
*/
public function testPropertyExistThrowsExceptionForMissingProperty(): void
{
$expectedMessage = sprintf(
DescriberInterface::ERROR_MISSING_PROPERTY,
DescriberInterface::INSTALLED_PACKAGES,
MockClassWithoutPropertyInstalledPackage::class
get_class($this->subjectWithoutPropertyInstalledPackages)
);

$this->expectException(
\OutOfBoundsException::class
);
$this->expectExceptionCode(
1557047757
);
$this->expectExceptionMessage(
$expectedMessage
$expectedMessage
);
MockClassWithoutPropertyInstalledPackage::propertyExists(DescriberInterface::INSTALLED_PACKAGES);

$this->subjectWithoutPropertyInstalledPackages::propertyExists(DescriberInterface::INSTALLED_PACKAGES);
}

/**
* @covers ::getInstalledPackage
*/
public function testGetInstalledPackageReturnsPackageObject(): void
{
$this->assertInstanceOf(
self::assertInstanceOf(
Package::class,
MockClassWithPropertyInstalledPackages::getInstalledPackage('foo')
$this->subjectWithPropertyInstalledPackages::getInstalledPackage('foo')
);
}
}
}

0 comments on commit 98bf372

Please sign in to comment.