-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allowed skipping a test if a Composer package is not installed (in a …
…version) this closes #60
- Loading branch information
1 parent
e8e199f
commit ecdf242
Showing
10 changed files
with
123 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace MyTester\Attributes; | ||
|
||
use Attribute; | ||
use Composer\InstalledVersions; | ||
use Composer\Semver\VersionParser; | ||
use MyTester\ISkipAttribute; | ||
|
||
/** | ||
* Requires package attribute | ||
* Defines a Composer package required for a test | ||
* | ||
* @author Jakub Konečný | ||
*/ | ||
#[Attribute(Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)] | ||
final readonly class RequiresPackage implements ISkipAttribute | ||
{ | ||
public function __construct(public string $packageName, public ?string $version = null) | ||
{ | ||
if ($this->version !== null && !InstalledVersions::isInstalled("composer/semver")) { | ||
trigger_error( | ||
"Specifying a version constraint for package requires package composer/semver", | ||
E_USER_ERROR | ||
); | ||
} | ||
} | ||
|
||
public function getSkipValue(): ?string | ||
{ | ||
if (!InstalledVersions::isInstalled($this->packageName)) { | ||
return "package $this->packageName is not installed"; | ||
} | ||
if ( | ||
$this->version !== null && | ||
!InstalledVersions::satisfies(new VersionParser(), $this->packageName, $this->version) | ||
) { | ||
return "package $this->packageName is not installed in version $this->version"; | ||
} | ||
return null; | ||
} | ||
|
||
public function getValue(): string | ||
{ | ||
return $this->packageName . (is_string($this->version) ? " $this->version" : ""); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace MyTester\Attributes; | ||
|
||
use MyTester\TestCase; | ||
|
||
/** | ||
* Test suite for class RequiresPackage | ||
* | ||
* @author Jakub Konečný | ||
*/ | ||
#[TestSuite("RequiresPackage")] | ||
final class RequiresPackageTest extends TestCase | ||
{ | ||
public function testGetSkipValue(): void | ||
{ | ||
$attribute = new RequiresPackage("composer/semver"); | ||
$this->assertNull($attribute->getSkipValue()); | ||
|
||
$attribute = new RequiresPackage("composer/semver", "^3.0"); | ||
$this->assertNull($attribute->getSkipValue()); | ||
|
||
$attribute = new RequiresPackage("phpunit/phpunit"); | ||
$this->assertSame("package phpunit/phpunit is not installed", $attribute->getSkipValue()); | ||
|
||
$attribute = new RequiresPackage("phpstan/phpstan", "^1.0"); | ||
$this->assertSame("package phpstan/phpstan is not installed in version ^1.0", $attribute->getSkipValue()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters