diff --git a/Classes/Core/Exception/InvalidIdValueException.php b/Classes/Core/Exception/InvalidIdValueException.php new file mode 100644 index 00000000..d99df8ff --- /dev/null +++ b/Classes/Core/Exception/InvalidIdValueException.php @@ -0,0 +1,26 @@ +isValidDataTypeForId($id)) { - throw new \InvalidArgumentException( - \sprintf( - 'Value for id has not a valid data type (given: "%s"). Valid types are: null, string, instanceof NodeIdentifierInterface', - \get_debug_type($id), - ), - 1620654936, - ); + throw InvalidIdValueException::fromValueType(\get_debug_type($id)); } if ($id === '') { @@ -95,15 +93,7 @@ public function getProperty(string $propertyName): mixed private function checkPropertyExists(string $propertyName): void { if (! \array_key_exists($propertyName, $this->properties)) { - $type = $this->getType(); - throw new \DomainException( - \sprintf( - 'Property "%s" is unknown for type "%s"', - $propertyName, - \is_array($type) ? \implode(' / ', $type) : $type, - ), - 1561829996, - ); + throw UnknownPropertyException::fromPropertyName($this->getType(), $propertyName); } } @@ -141,14 +131,7 @@ private function checkProperty(string $propertyName, mixed $propertyValue): void $this->checkPropertyExists($propertyName); if (! $this->isValidDataTypeForPropertyValue($propertyValue)) { - throw new \InvalidArgumentException( - \sprintf( - 'Value for property "%s" has not a valid data type (given: "%s"). Valid types are: null, string, int, array, bool, instanceof TypeInterface, instanceof NodeIdentifierInterface', - $propertyName, - \get_debug_type($propertyValue), - ), - 1561830012, - ); + throw InvalidPropertyValueException::fromValueType($propertyName, \get_debug_type($propertyValue)); } } diff --git a/Tests/Unit/Core/Exception/InvalidIdValueExceptionTest.php b/Tests/Unit/Core/Exception/InvalidIdValueExceptionTest.php new file mode 100644 index 00000000..589356dc --- /dev/null +++ b/Tests/Unit/Core/Exception/InvalidIdValueExceptionTest.php @@ -0,0 +1,33 @@ +getMessage(), + ); + self::assertSame(1620654936, $actual->getCode()); + } +} diff --git a/Tests/Unit/Core/Exception/InvalidPropertyValueExceptionTest.php b/Tests/Unit/Core/Exception/InvalidPropertyValueExceptionTest.php new file mode 100644 index 00000000..2a04f0d3 --- /dev/null +++ b/Tests/Unit/Core/Exception/InvalidPropertyValueExceptionTest.php @@ -0,0 +1,33 @@ +getMessage(), + ); + self::assertSame(1561830012, $actual->getCode()); + } +} diff --git a/Tests/Unit/Core/Exception/UnknownPropertyExceptionTest.php b/Tests/Unit/Core/Exception/UnknownPropertyExceptionTest.php new file mode 100644 index 00000000..2eb918f1 --- /dev/null +++ b/Tests/Unit/Core/Exception/UnknownPropertyExceptionTest.php @@ -0,0 +1,39 @@ +getMessage()); + self::assertSame(1561829996, $actual->getCode()); + } + + #[Test] + public function fromPropertyNameWithTypeAsArray(): void + { + $actual = UnknownPropertyException::fromPropertyName(['SomeType', 'AnotherType'], 'someProperty'); + + self::assertSame('Property "someProperty" is unknown for type "SomeType / AnotherType"', $actual->getMessage()); + self::assertSame(1561829996, $actual->getCode()); + } +} diff --git a/Tests/Unit/Core/Model/AbstractTypeTest.php b/Tests/Unit/Core/Model/AbstractTypeTest.php index 58902769..07a910b1 100644 --- a/Tests/Unit/Core/Model/AbstractTypeTest.php +++ b/Tests/Unit/Core/Model/AbstractTypeTest.php @@ -11,6 +11,9 @@ namespace Brotkrueml\Schema\Tests\Unit\Core\Model; +use Brotkrueml\Schema\Core\Exception\InvalidIdValueException; +use Brotkrueml\Schema\Core\Exception\InvalidPropertyValueException; +use Brotkrueml\Schema\Core\Exception\UnknownPropertyException; use Brotkrueml\Schema\Core\Model\AbstractType; use Brotkrueml\Schema\Core\Model\EnumerationInterface; use Brotkrueml\Schema\Core\Model\NodeIdentifierInterface; @@ -110,9 +113,8 @@ public function getId(): ?string #[Test] public function setIdThrowsExceptionWhenInvalidTypeGiven(): void { - $this->expectException(\InvalidArgumentException::class); + $this->expectException(InvalidIdValueException::class); $this->expectExceptionCode(1620654936); - $this->expectExceptionMessage('Value for id has not a valid data type (given: "bool"). Valid types are: null, string, instanceof NodeIdentifierInterface'); $this->subject->setId(true); } @@ -169,7 +171,7 @@ public function canonical(): string #[Test] public function setPropertyThrowsDomainExceptionIfPropertyNameNotValid(): void { - $this->expectException(\DomainException::class); + $this->expectException(UnknownPropertyException::class); $this->expectExceptionCode(1561829996); $this->subject->setProperty('invalidProperty', 'some value'); @@ -178,7 +180,7 @@ public function setPropertyThrowsDomainExceptionIfPropertyNameNotValid(): void #[Test] public function setPropertyThrowsInvalidArgumentExceptionIfPropertyNotValid(): void { - $this->expectException(\InvalidArgumentException::class); + $this->expectException(InvalidPropertyValueException::class); $this->expectExceptionCode(1561830012); $this->subject->setProperty('image', new \stdClass()); @@ -205,7 +207,7 @@ public function getPropertyReturnsCorrectValue(): void #[Test] public function getPropertyThrowsDomainExceptionIfPropertyNameDoesNotExist(): void { - $this->expectException(\DomainException::class); + $this->expectException(UnknownPropertyException::class); $this->expectExceptionCode(1561829996); $this->subject->getProperty('invalidPropertyName'); @@ -383,7 +385,7 @@ public function clearPropertySetsValueToNull(): void #[Test] public function clearPropertyThrowsDomainExceptionIfPropertyNameDoesNotExist(): void { - $this->expectException(\DomainException::class); + $this->expectException(UnknownPropertyException::class); $this->expectExceptionCode(1561829996); $this->subject->clearProperty('invalidPropertyName');