generated from ergebnis/php-package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhancement: Implement BuildMetaData
- Loading branch information
1 parent
eca4e26
commit 8d189ae
Showing
9 changed files
with
347 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright (c) 2023 Andreas Möller | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE.md file that was distributed with this source code. | ||
* | ||
* @see https://github.com/ergebnis/version | ||
*/ | ||
|
||
namespace Ergebnis\Version; | ||
|
||
final class BuildMetaData | ||
{ | ||
/** | ||
* @see https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string | ||
* @see https://regex101.com/r/Ly7O1x/3/ | ||
*/ | ||
private const REGEX = '/^(?P<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*)$/'; | ||
|
||
private function __construct(private readonly string $value) | ||
{ | ||
} | ||
|
||
/** | ||
* @throws Exception\InvalidBuildMetaData | ||
*/ | ||
public static function fromString(string $value): self | ||
{ | ||
if (1 !== \preg_match(self::REGEX, $value)) { | ||
throw Exception\InvalidBuildMetaData::fromString($value); | ||
} | ||
|
||
return new self($value); | ||
} | ||
|
||
public static function empty(): self | ||
{ | ||
return new self(''); | ||
} | ||
|
||
public function toString(): string | ||
{ | ||
return $this->value; | ||
} | ||
|
||
public function equals(self $other): bool | ||
{ | ||
return $this->value === $other->value; | ||
} | ||
} |
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,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright (c) 2023 Andreas Möller | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE.md file that was distributed with this source code. | ||
* | ||
* @see https://github.com/ergebnis/version | ||
*/ | ||
|
||
namespace Ergebnis\Version\Exception; | ||
|
||
final class InvalidBuildMetaData extends \InvalidArgumentException | ||
{ | ||
public static function fromString(string $value): self | ||
{ | ||
return new self(\sprintf( | ||
'Value "%s" does not appear to be valid.', | ||
$value, | ||
)); | ||
} | ||
} |
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,116 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright (c) 2023 Andreas Möller | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE.md file that was distributed with this source code. | ||
* | ||
* @see https://github.com/ergebnis/version | ||
*/ | ||
|
||
namespace Ergebnis\Version\Test\Unit; | ||
|
||
use Ergebnis\Version\BuildMetaData; | ||
use Ergebnis\Version\Exception; | ||
use Ergebnis\Version\Test; | ||
use PHPUnit\Framework; | ||
|
||
#[Framework\Attributes\CoversClass(BuildMetaData::class)] | ||
#[Framework\Attributes\UsesClass(Exception\InvalidBuildMetaData::class)] | ||
final class BuildMetaDataTest extends Framework\TestCase | ||
{ | ||
use Test\Util\Helper; | ||
|
||
#[Framework\Attributes\DataProvider('provideInvalidValue')] | ||
public function testFromStringRejectsInvalidValue(string $value): void | ||
{ | ||
$this->expectException(Exception\InvalidBuildMetaData::class); | ||
|
||
BuildMetaData::fromString($value); | ||
} | ||
|
||
/** | ||
* @see https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string | ||
* @see https://regex101.com/r/Ly7O1x/3/ | ||
* | ||
* @return \Generator<string, array{0: string}> | ||
*/ | ||
public static function provideInvalidValue(): \Generator | ||
{ | ||
$values = [ | ||
'meta+meta', | ||
'whatever+meta+meta', | ||
]; | ||
|
||
foreach ($values as $value) { | ||
yield $value => [ | ||
$value, | ||
]; | ||
} | ||
} | ||
|
||
#[Framework\Attributes\DataProvider('provideValidValue')] | ||
public function testFromStringReturnsBuildMetaData(string $value): void | ||
{ | ||
$buildMetaData = BuildMetaData::fromString($value); | ||
|
||
self::assertSame($value, $buildMetaData->toString()); | ||
} | ||
|
||
/** | ||
* @see https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string | ||
* @see https://regex101.com/r/Ly7O1x/3/ | ||
* | ||
* @return \Generator<string, array{0: string}> | ||
*/ | ||
public static function provideValidValue(): \Generator | ||
{ | ||
$values = [ | ||
'meta', | ||
'meta-valid', | ||
'build.1-aef.1-its-okay', | ||
'build.1', | ||
'build.123', | ||
'build.1848', | ||
'beta', | ||
'788', | ||
'0.build.1-rc.10000aaa-kk-0.1', | ||
]; | ||
|
||
foreach ($values as $value) { | ||
yield $value => [ | ||
$value, | ||
]; | ||
} | ||
} | ||
|
||
public function testEmptyReturnsBuildMetaData(): void | ||
{ | ||
$buildMetaData = BuildMetaData::empty(); | ||
|
||
self::assertSame('', $buildMetaData->toString()); | ||
} | ||
|
||
public function testEqualsReturnsFalseWhenValuesAreDifferent(): void | ||
{ | ||
$faker = self::faker()->unique(); | ||
|
||
$one = BuildMetaData::fromString($faker->word()); | ||
$two = BuildMetaData::fromString($faker->word()); | ||
|
||
self::assertFalse($one->equals($two)); | ||
} | ||
|
||
public function testEqualsReturnsTrueWhenValueIsSame(): void | ||
{ | ||
$value = self::faker()->word(); | ||
|
||
$one = BuildMetaData::fromString($value); | ||
$two = BuildMetaData::fromString($value); | ||
|
||
self::assertTrue($one->equals($two)); | ||
} | ||
} |
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,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright (c) 2023 Andreas Möller | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE.md file that was distributed with this source code. | ||
* | ||
* @see https://github.com/ergebnis/version | ||
*/ | ||
|
||
namespace Ergebnis\Version\Test\Unit\Exception; | ||
|
||
use Ergebnis\Version\Exception; | ||
use Ergebnis\Version\Test; | ||
use PHPUnit\Framework; | ||
|
||
#[Framework\Attributes\CoversClass(Exception\InvalidBuildMetaData::class)] | ||
final class InvalidBuildMetaDataTest extends Framework\TestCase | ||
{ | ||
use Test\Util\Helper; | ||
|
||
public function testFromStringReturnsException(): void | ||
{ | ||
$value = self::faker()->word(); | ||
|
||
$exception = Exception\InvalidBuildMetaData::fromString($value); | ||
|
||
$message = \sprintf( | ||
'Value "%s" does not appear to be valid.', | ||
$value, | ||
); | ||
|
||
self::assertSame($message, $exception->getMessage()); | ||
} | ||
} |
Oops, something went wrong.