From 084f62717ac8a95c5c48927c3517baeeb8b0587d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chris=20M=C3=BCller?= Date: Fri, 21 Feb 2025 18:00:24 +0100 Subject: [PATCH] chore: use named constructor for exceptions in MultipleType class --- .../InvalidNumberOfTypesException.php | 30 +++++++++++++ .../SameTypeForMultipleTypeException.php | 29 ++++++++++++ Classes/Core/Model/MultipleType.php | 13 +++--- .../InvalidNumberOfTypesExceptionTest.php | 45 +++++++++++++++++++ .../SameTypeForMultipleTypeExceptionTest.php | 33 ++++++++++++++ Tests/Unit/Core/Model/MultipleTypeTest.php | 11 +++-- 6 files changed, 147 insertions(+), 14 deletions(-) create mode 100644 Classes/Core/Exception/InvalidNumberOfTypesException.php create mode 100644 Classes/Core/Exception/SameTypeForMultipleTypeException.php create mode 100644 Tests/Unit/Core/Exception/InvalidNumberOfTypesExceptionTest.php create mode 100644 Tests/Unit/Core/Exception/SameTypeForMultipleTypeExceptionTest.php diff --git a/Classes/Core/Exception/InvalidNumberOfTypesException.php b/Classes/Core/Exception/InvalidNumberOfTypesException.php new file mode 100644 index 00000000..d4e1a04e --- /dev/null +++ b/Classes/Core/Exception/InvalidNumberOfTypesException.php @@ -0,0 +1,30 @@ +typeNames); if (\count($this->typeNames) !== \count($uniqueTypeNames)) { - throw new \DomainException( - \sprintf('Only different types can be used as arguments for a multiple type, "%s" given', \implode(', ', $this->typeNames)), - 1621871950, - ); + throw SameTypeForMultipleTypeException::fromSingleTypes($this->typeNames); } } private function checkNumberOfTypes(): void { if (\count($this->typeNames) < 2) { - throw new \DomainException( - \sprintf('At least two types have to be assigned, %d given', \count($this->typeNames)), - 1621871446, - ); + throw InvalidNumberOfTypesException::fromSingleTypes($this->typeNames); } } diff --git a/Tests/Unit/Core/Exception/InvalidNumberOfTypesExceptionTest.php b/Tests/Unit/Core/Exception/InvalidNumberOfTypesExceptionTest.php new file mode 100644 index 00000000..776cc0aa --- /dev/null +++ b/Tests/Unit/Core/Exception/InvalidNumberOfTypesExceptionTest.php @@ -0,0 +1,45 @@ +getMessage(), + ); + self::assertSame(1621871446, $actual->getCode()); + } + + #[Test] + public function fromSingleTypesWithOneGiven(): void + { + $actual = InvalidNumberOfTypesException::fromSingleTypes(['SomeType']); + + self::assertSame( + 'At least two types have to be assigned, "SomeType" (1) given', + $actual->getMessage(), + ); + self::assertSame(1621871446, $actual->getCode()); + } +} diff --git a/Tests/Unit/Core/Exception/SameTypeForMultipleTypeExceptionTest.php b/Tests/Unit/Core/Exception/SameTypeForMultipleTypeExceptionTest.php new file mode 100644 index 00000000..9abc5f91 --- /dev/null +++ b/Tests/Unit/Core/Exception/SameTypeForMultipleTypeExceptionTest.php @@ -0,0 +1,33 @@ +getMessage(), + ); + self::assertSame(1621871950, $actual->getCode()); + } +} diff --git a/Tests/Unit/Core/Model/MultipleTypeTest.php b/Tests/Unit/Core/Model/MultipleTypeTest.php index e1913fca..0be19083 100644 --- a/Tests/Unit/Core/Model/MultipleTypeTest.php +++ b/Tests/Unit/Core/Model/MultipleTypeTest.php @@ -11,6 +11,8 @@ namespace Brotkrueml\Schema\Tests\Unit\Core\Model; +use Brotkrueml\Schema\Core\Exception\InvalidNumberOfTypesException; +use Brotkrueml\Schema\Core\Exception\SameTypeForMultipleTypeException; use Brotkrueml\Schema\Core\Model\MultipleType; use Brotkrueml\Schema\Core\Model\NodeIdentifierInterface; use Brotkrueml\Schema\Core\Model\TypeInterface; @@ -45,9 +47,8 @@ public function subjectImplementsNodeIdentifierInterface(): void #[Test] public function instantiatingSubjectWithNoTypeGivenThrowsException(): void { - $this->expectException(\DomainException::class); + $this->expectException(InvalidNumberOfTypesException::class); $this->expectExceptionCode(1621871446); - $this->expectExceptionMessage('At least two types have to be assigned, 0 given'); new MultipleType(); } @@ -55,9 +56,8 @@ public function instantiatingSubjectWithNoTypeGivenThrowsException(): void #[Test] public function instantiatingSubjectWithOneTypeGivenThrowsException(): void { - $this->expectException(\DomainException::class); + $this->expectException(InvalidNumberOfTypesException::class); $this->expectExceptionCode(1621871446); - $this->expectExceptionMessage('At least two types have to be assigned, 1 given'); new MultipleType(new ProductStub()); } @@ -65,9 +65,8 @@ public function instantiatingSubjectWithOneTypeGivenThrowsException(): void #[Test] public function instantiatingSubjectWithTwoSameTypesGivenThrowsException(): void { - $this->expectException(\DomainException::class); + $this->expectException(SameTypeForMultipleTypeException::class); $this->expectExceptionCode(1621871950); - $this->expectExceptionMessage('Only different types can be used as arguments for a multiple type, "ProductStub, ProductStub, ServiceStub" given'); new MultipleType(new ProductStub(), new ProductStub(), new ServiceStub()); }