diff --git a/src/Release/Branch.php b/src/Release/Branch.php new file mode 100644 index 0000000000..2c5a978e65 --- /dev/null +++ b/src/Release/Branch.php @@ -0,0 +1,61 @@ +(0|[1-9]\d*))\.(?P(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(), + ); + } +} diff --git a/src/Release/Major.php b/src/Release/Major.php new file mode 100644 index 0000000000..9060a316dd --- /dev/null +++ b/src/Release/Major.php @@ -0,0 +1,32 @@ +value; + } +} diff --git a/src/Release/Minor.php b/src/Release/Minor.php new file mode 100644 index 0000000000..ebecb31b2f --- /dev/null +++ b/src/Release/Minor.php @@ -0,0 +1,32 @@ +value; + } +} diff --git a/tests/UserNotes/Release/Branch/create-returns-branch.phpt b/tests/UserNotes/Release/Branch/create-returns-branch.phpt new file mode 100644 index 0000000000..a6fa0604ec --- /dev/null +++ b/tests/UserNotes/Release/Branch/create-returns-branch.phpt @@ -0,0 +1,31 @@ +--TEST-- +Branch::create() returns Branch +--FILE-- +toString(), + $branch->major()->toString(), + $branch->minor()->toString(), +]); +?> +--EXPECT-- +array(3) { + [0]=> + string(3) "8.4" + [1]=> + string(1) "8" + [2]=> + string(1) "4" +} diff --git a/tests/UserNotes/Release/Branch/from-string-rejects-invalid-value.phpt b/tests/UserNotes/Release/Branch/from-string-rejects-invalid-value.phpt new file mode 100644 index 0000000000..d566d894cb --- /dev/null +++ b/tests/UserNotes/Release/Branch/from-string-rejects-invalid-value.phpt @@ -0,0 +1,50 @@ +--TEST-- +Branch::fromString() rejects invalid values +--FILE-- + ' ', + '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" +} diff --git a/tests/UserNotes/Release/Branch/from-string-returns-branch.phpt b/tests/UserNotes/Release/Branch/from-string-returns-branch.phpt new file mode 100644 index 0000000000..0ecff1430f --- /dev/null +++ b/tests/UserNotes/Release/Branch/from-string-returns-branch.phpt @@ -0,0 +1,17 @@ +--TEST-- +Branch::fromString() returns Branch +--FILE-- +toString()); +?> +--EXPECT-- +string(3) "8.3" diff --git a/tests/UserNotes/Release/Major/from-string-rejects-invalid-value.phpt b/tests/UserNotes/Release/Major/from-string-rejects-invalid-value.phpt new file mode 100644 index 0000000000..a6c7b1cad3 --- /dev/null +++ b/tests/UserNotes/Release/Major/from-string-rejects-invalid-value.phpt @@ -0,0 +1,41 @@ +--TEST-- +Major::fromString() rejects invalid values +--FILE-- + ' ', + '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" +} diff --git a/tests/UserNotes/Release/Major/from-string-returns-major.phpt b/tests/UserNotes/Release/Major/from-string-returns-major.phpt new file mode 100644 index 0000000000..1d1d27ad34 --- /dev/null +++ b/tests/UserNotes/Release/Major/from-string-returns-major.phpt @@ -0,0 +1,17 @@ +--TEST-- +Major::fromString() returns Major +--FILE-- +toString()); +?> +--EXPECT-- +string(1) "8" diff --git a/tests/UserNotes/Release/Minor/from-string-rejects-invalid-value.phpt b/tests/UserNotes/Release/Minor/from-string-rejects-invalid-value.phpt new file mode 100644 index 0000000000..5cf538454f --- /dev/null +++ b/tests/UserNotes/Release/Minor/from-string-rejects-invalid-value.phpt @@ -0,0 +1,41 @@ +--TEST-- +Minor::fromString() rejects invalid values +--FILE-- + ' ', + '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" +} diff --git a/tests/UserNotes/Release/Minor/from-string-returns-minor.phpt b/tests/UserNotes/Release/Minor/from-string-returns-minor.phpt new file mode 100644 index 0000000000..b9aa029638 --- /dev/null +++ b/tests/UserNotes/Release/Minor/from-string-returns-minor.phpt @@ -0,0 +1,17 @@ +--TEST-- +Minor::fromString() returns Minor +--FILE-- +toString()); +?> +--EXPECT-- +string(1) "3"