Skip to content

Commit

Permalink
Enhancement: Extract Branch
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Dec 6, 2023
1 parent 2b70836 commit 74117e2
Show file tree
Hide file tree
Showing 10 changed files with 339 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/Release/Branch.php
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(),
);
}
}
32 changes: 32 additions & 0 deletions src/Release/Major.php
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;
}
}
32 changes: 32 additions & 0 deletions src/Release/Minor.php
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;
}
}
31 changes: 31 additions & 0 deletions tests/UserNotes/Release/Branch/create-returns-branch.phpt
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"
}
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-part-one-leading-zero' => '00.3',
'string-part-two-leading-zero' => '8.03',
'string-parts-too-few' => '8',
'string-parts-too-many' => '8.3.0',
'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-part-one-leading-zero"]=>
string(4) "00.3"
["string-part-two-leading-zero"]=>
string(4) "8.03"
["string-parts-too-few"]=>
string(1) "8"
["string-parts-too-many"]=>
string(5) "8.3.0"
["string-word"]=>
string(3) "foo"
}
17 changes: 17 additions & 0 deletions tests/UserNotes/Release/Branch/from-string-returns-branch.phpt
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"
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 tests/UserNotes/Release/Major/from-string-returns-major.phpt
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"
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 tests/UserNotes/Release/Minor/from-string-returns-minor.phpt
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"

0 comments on commit 74117e2

Please sign in to comment.