-
Notifications
You must be signed in to change notification settings - Fork 553
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4ac417b
commit 8ef1ee8
Showing
10 changed files
with
339 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace phpweb\Release; | ||
|
||
final readonly class Branch | ||
{ | ||
private function __construct( | ||
private Major $major, | ||
private Minor $minor | ||
) { | ||
} | ||
|
||
public static function create( | ||
Major $major, | ||
Minor $minor | ||
): self { | ||
return new self( | ||
$major, | ||
$minor | ||
); | ||
} | ||
|
||
/** | ||
* @throws \InvalidArgumentException | ||
*/ | ||
public static function fromString(string $value): self | ||
{ | ||
if (1 !== preg_match('/^(?P<major>(0|[1-9]\d*))\.(?P<minor>(0|[1-9]\d*)$)/', $value, $matches)) { | ||
throw new \InvalidArgumentException(\sprintf( | ||
'Value "%s" does not appear to be a valid value for a branch name.', | ||
$value | ||
)); | ||
} | ||
|
||
return new self( | ||
Major::fromString($matches['major']), | ||
Minor::fromString($matches['minor']), | ||
); | ||
} | ||
|
||
public function major(): Major | ||
{ | ||
return $this->major; | ||
} | ||
|
||
public function minor(): Minor | ||
{ | ||
return $this->minor; | ||
} | ||
|
||
public function toString(): string | ||
{ | ||
return sprintf( | ||
'%s.%s', | ||
$this->major->toString(), | ||
$this->minor->toString(), | ||
); | ||
} | ||
} |
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,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace phpweb\Release; | ||
|
||
final readonly class Major | ||
{ | ||
private function __construct(private readonly string $value) | ||
{ | ||
} | ||
|
||
/** | ||
* @throws \InvalidArgumentException | ||
*/ | ||
public static function fromString(string $value): self | ||
{ | ||
if (1 !== preg_match('/^(0|[1-9]\d*)$/', $value)) { | ||
throw new \InvalidArgumentException(\sprintf( | ||
'Value "%s" does not appear to be a valid value for a major version.', | ||
$value | ||
)); | ||
} | ||
|
||
return new self($value); | ||
} | ||
|
||
public function toString(): string | ||
{ | ||
return $this->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,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace phpweb\Release; | ||
|
||
final readonly class Minor | ||
{ | ||
private function __construct(private readonly string $value) | ||
{ | ||
} | ||
|
||
/** | ||
* @throws \InvalidArgumentException | ||
*/ | ||
public static function fromString(string $value): self | ||
{ | ||
if (1 !== preg_match('/^(0|[1-9]\d*)$/', $value)) { | ||
throw new \InvalidArgumentException(\sprintf( | ||
'Value "%s" does not appear to be a valid value for a minor version.', | ||
$value | ||
)); | ||
} | ||
|
||
return new self($value); | ||
} | ||
|
||
public function toString(): string | ||
{ | ||
return $this->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,31 @@ | ||
--TEST-- | ||
Branch::create() returns Branch | ||
--FILE-- | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use phpweb\Release; | ||
|
||
require_once __DIR__ . '/../../../../src/autoload.php'; | ||
|
||
$branch = Release\Branch::create( | ||
Release\Major::fromString('8'), | ||
Release\Minor::fromString('4'), | ||
); | ||
|
||
var_dump([ | ||
$branch->toString(), | ||
$branch->major()->toString(), | ||
$branch->minor()->toString(), | ||
]); | ||
?> | ||
--EXPECT-- | ||
array(3) { | ||
[0]=> | ||
string(3) "8.4" | ||
[1]=> | ||
string(1) "8" | ||
[2]=> | ||
string(1) "4" | ||
} |
50 changes: 50 additions & 0 deletions
50
tests/UserNotes/Release/Branch/from-string-rejects-invalid-value.phpt
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,50 @@ | ||
--TEST-- | ||
Branch::fromString() rejects invalid values | ||
--FILE-- | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use phpweb\Release; | ||
|
||
require_once __DIR__ . '/../../../../src/autoload.php'; | ||
|
||
$values = [ | ||
'string-blank' => ' ', | ||
'string-empty' => '', | ||
'string-major-leading-zero' => '00.3', | ||
'string-major-minor-patch' => '8.3.0', | ||
'string-major-only' => '8', | ||
'string-minor-leading-zero' => '8.03', | ||
'string-word' => 'foo', | ||
]; | ||
|
||
$invalidValues = array_filter($values, static function (string $value): bool { | ||
try { | ||
Release\Branch::fromString($value); | ||
} catch (\InvalidArgumentException) { | ||
return true; | ||
} | ||
|
||
return false; | ||
}); | ||
|
||
var_dump($invalidValues); | ||
?> | ||
--EXPECT-- | ||
array(7) { | ||
["string-blank"]=> | ||
string(1) " " | ||
["string-empty"]=> | ||
string(0) "" | ||
["string-major-leading-zero"]=> | ||
string(4) "00.3" | ||
["string-major-minor-patch"]=> | ||
string(5) "8.3.0" | ||
["string-major-only"]=> | ||
string(1) "8" | ||
["string-minor-leading-zero"]=> | ||
string(4) "8.03" | ||
["string-word"]=> | ||
string(3) "foo" | ||
} |
17 changes: 17 additions & 0 deletions
17
tests/UserNotes/Release/Branch/from-string-returns-branch.phpt
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,17 @@ | ||
--TEST-- | ||
Branch::fromString() returns Branch | ||
--FILE-- | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use phpweb\Release; | ||
|
||
require_once __DIR__ . '/../../../../src/autoload.php'; | ||
|
||
$branch = Release\Branch::fromString('8.3'); | ||
|
||
var_dump($branch->toString()); | ||
?> | ||
--EXPECT-- | ||
string(3) "8.3" |
41 changes: 41 additions & 0 deletions
41
tests/UserNotes/Release/Major/from-string-rejects-invalid-value.phpt
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,41 @@ | ||
--TEST-- | ||
Major::fromString() rejects invalid values | ||
--FILE-- | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use phpweb\Release; | ||
|
||
require_once __DIR__ . '/../../../../src/autoload.php'; | ||
|
||
$values = [ | ||
'string-blank' => ' ', | ||
'string-empty' => '', | ||
'string-leading-zero' => '01', | ||
'string-word' => 'foo', | ||
]; | ||
|
||
$invalidValues = array_filter($values, static function (string $value): bool { | ||
try { | ||
Release\Major::fromString($value); | ||
} catch (\InvalidArgumentException) { | ||
return true; | ||
} | ||
|
||
return false; | ||
}); | ||
|
||
var_dump($invalidValues); | ||
?> | ||
--EXPECT-- | ||
array(4) { | ||
["string-blank"]=> | ||
string(1) " " | ||
["string-empty"]=> | ||
string(0) "" | ||
["string-leading-zero"]=> | ||
string(2) "01" | ||
["string-word"]=> | ||
string(3) "foo" | ||
} |
17 changes: 17 additions & 0 deletions
17
tests/UserNotes/Release/Major/from-string-returns-major.phpt
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,17 @@ | ||
--TEST-- | ||
Major::fromString() returns Major | ||
--FILE-- | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use phpweb\Release; | ||
|
||
require_once __DIR__ . '/../../../../src/autoload.php'; | ||
|
||
$major = Release\Major::fromString('8'); | ||
|
||
var_dump($major->toString()); | ||
?> | ||
--EXPECT-- | ||
string(1) "8" |
41 changes: 41 additions & 0 deletions
41
tests/UserNotes/Release/Minor/from-string-rejects-invalid-value.phpt
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,41 @@ | ||
--TEST-- | ||
Minor::fromString() rejects invalid values | ||
--FILE-- | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use phpweb\Release; | ||
|
||
require_once __DIR__ . '/../../../../src/autoload.php'; | ||
|
||
$values = [ | ||
'string-blank' => ' ', | ||
'string-empty' => '', | ||
'string-leading-zero' => '01', | ||
'string-word' => 'foo', | ||
]; | ||
|
||
$invalidValues = array_filter($values, static function (string $value): bool { | ||
try { | ||
Release\Minor::fromString($value); | ||
} catch (\InvalidArgumentException) { | ||
return true; | ||
} | ||
|
||
return false; | ||
}); | ||
|
||
var_dump($invalidValues); | ||
?> | ||
--EXPECT-- | ||
array(4) { | ||
["string-blank"]=> | ||
string(1) " " | ||
["string-empty"]=> | ||
string(0) "" | ||
["string-leading-zero"]=> | ||
string(2) "01" | ||
["string-word"]=> | ||
string(3) "foo" | ||
} |
17 changes: 17 additions & 0 deletions
17
tests/UserNotes/Release/Minor/from-string-returns-minor.phpt
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,17 @@ | ||
--TEST-- | ||
Minor::fromString() returns Minor | ||
--FILE-- | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use phpweb\Release; | ||
|
||
require_once __DIR__ . '/../../../../src/autoload.php'; | ||
|
||
$minor = Release\Minor::fromString('3'); | ||
|
||
var_dump($minor->toString()); | ||
?> | ||
--EXPECT-- | ||
string(1) "3" |