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.
Merge pull request #4 from ergebnis/feature/minor
Enhancement: Implement `Minor`
- Loading branch information
Showing
9 changed files
with
306 additions
and
2 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,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 InvalidMinor 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?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 Minor | ||
{ | ||
/** | ||
* @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<minor>0|[1-9]\d*)$/'; | ||
|
||
private function __construct(private readonly string $value) | ||
{ | ||
} | ||
|
||
/** | ||
* @throws Exception\InvalidMinor | ||
*/ | ||
public static function fromString(string $value): self | ||
{ | ||
if (1 !== \preg_match(self::REGEX, $value)) { | ||
throw Exception\InvalidMinor::fromString($value); | ||
} | ||
|
||
return new self($value); | ||
} | ||
|
||
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
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\InvalidMinor::class)] | ||
final class InvalidMinorTest extends Framework\TestCase | ||
{ | ||
use Test\Util\Helper; | ||
|
||
public function testFromStringReturnsException(): void | ||
{ | ||
$value = self::faker()->word(); | ||
|
||
$exception = Exception\InvalidMinor::fromString($value); | ||
|
||
$message = \sprintf( | ||
'Value "%s" does not appear to be valid.', | ||
$value, | ||
); | ||
|
||
self::assertSame($message, $exception->getMessage()); | ||
} | ||
} |
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,107 @@ | ||
<?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 | ||
*/ | ||
|
||
use Ergebnis\Version\Exception; | ||
use Ergebnis\Version\Minor; | ||
use Ergebnis\Version\Test; | ||
use Ergebnis\Version\Version; | ||
use PHPUnit\Framework; | ||
|
||
#[Framework\Attributes\CoversClass(Minor::class)] | ||
#[Framework\Attributes\UsesClass(Exception\InvalidMinor::class)] | ||
final class MinorTest extends Framework\TestCase | ||
{ | ||
use Test\Util\Helper; | ||
|
||
#[Framework\Attributes\DataProvider('provideInvalidValue')] | ||
public function testFromStringRejectsInvalidValue(string $value): void | ||
{ | ||
$this->expectException(Exception\InvalidMinor::class); | ||
|
||
Minor::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 | ||
{ | ||
$faker = self::faker(); | ||
|
||
$values = [ | ||
'leading-zero' => \sprintf( | ||
'0%d', | ||
$faker->numberBetween(1), | ||
), | ||
'word' => $faker->word(), | ||
]; | ||
|
||
foreach ($values as $value) { | ||
yield $value => [ | ||
$value, | ||
]; | ||
} | ||
} | ||
|
||
#[Framework\Attributes\DataProvider('provideValidValue')] | ||
public function testFromStringReturnsMinor(string $value): void | ||
{ | ||
$minor = Minor::fromString($value); | ||
|
||
self::assertSame($value, $minor->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 = [ | ||
'zero' => '0', | ||
'one' => '1', | ||
'greater-than-one' => (string) self::faker()->numberBetween(2), | ||
]; | ||
|
||
foreach ($values as $key => $value) { | ||
yield $key => [ | ||
$value, | ||
]; | ||
} | ||
} | ||
|
||
public function testEqualsReturnsFalseWhenValuesAreDifferent(): void | ||
{ | ||
$faker = self::faker()->unique(); | ||
|
||
$one = Minor::fromString((string) $faker->numberBetween(0)); | ||
$two = Minor::fromString((string) $faker->numberBetween(0)); | ||
|
||
self::assertFalse($one->equals($two)); | ||
} | ||
|
||
public function testEqualsReturnsTrueWhenValueIsSame(): void | ||
{ | ||
$value = (string) self::faker()->numberBetween(0); | ||
|
||
$one = Minor::fromString($value); | ||
$two = Minor::fromString($value); | ||
|
||
self::assertTrue($one->equals($two)); | ||
} | ||
} |
Oops, something went wrong.