Skip to content

Commit

Permalink
chore: use named constructor for exceptions in AbstractBaseType class
Browse files Browse the repository at this point in the history
  • Loading branch information
brotkrueml committed Feb 20, 2025
1 parent 0a31871 commit be6b0a3
Show file tree
Hide file tree
Showing 8 changed files with 203 additions and 30 deletions.
26 changes: 26 additions & 0 deletions Classes/Core/Exception/InvalidIdValueException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

/*
* This file is part of the "schema" extension for TYPO3 CMS.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*/

namespace Brotkrueml\Schema\Core\Exception;

final class InvalidIdValueException extends \InvalidArgumentException
{
public static function fromValueType(string $valueType): self
{
return new self(
\sprintf(
'Value for id has not a valid data type (given: "%s"). Valid types are: null, string, instanceof NodeIdentifierInterface',
$valueType,
),
1620654936,
);
}
}
27 changes: 27 additions & 0 deletions Classes/Core/Exception/InvalidPropertyValueException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

/*
* This file is part of the "schema" extension for TYPO3 CMS.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*/

namespace Brotkrueml\Schema\Core\Exception;

final class InvalidPropertyValueException extends \InvalidArgumentException
{
public static function fromValueType(string $propertyName, string $propertyValueType): self
{
return new self(
\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,
$propertyValueType,
),
1561830012,
);
}
}
30 changes: 30 additions & 0 deletions Classes/Core/Exception/UnknownPropertyException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

/*
* This file is part of the "schema" extension for TYPO3 CMS.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*/

namespace Brotkrueml\Schema\Core\Exception;

final class UnknownPropertyException extends \DomainException
{
/**
* @param string|string[] $type
*/
public static function fromPropertyName(string|array $type, string $propertyName): self
{
return new self(
\sprintf(
'Property "%s" is unknown for type "%s"',
$propertyName,
\is_array($type) ? \implode(' / ', $type) : $type,
),
1561829996,
);
}
}
31 changes: 7 additions & 24 deletions Classes/Core/Model/AbstractBaseType.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@

namespace Brotkrueml\Schema\Core\Model;

use Brotkrueml\Schema\Core\Exception\InvalidIdValueException;
use Brotkrueml\Schema\Core\Exception\InvalidPropertyValueException;
use Brotkrueml\Schema\Core\Exception\UnknownPropertyException;

/**
* This class provides the logic for both, a single type and a multiple type.
* It is not for use in custom extensions. Use AbstractType instead.
Expand All @@ -37,13 +41,7 @@ abstract class AbstractBaseType implements TypeInterface
public function setId($id): static
{
if (! $this->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 === '') {
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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));
}
}

Expand Down
33 changes: 33 additions & 0 deletions Tests/Unit/Core/Exception/InvalidIdValueExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

/*
* This file is part of the "schema" extension for TYPO3 CMS.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*/

namespace Brotkrueml\Schema\Tests\Unit\Core\Exception;

use Brotkrueml\Schema\Core\Exception\InvalidIdValueException;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

#[CoversClass(InvalidIdValueException::class)]
final class InvalidIdValueExceptionTest extends TestCase
{
#[Test]
public function fromDebugType(): void
{
$actual = InvalidIdValueException::fromValueType('invalid_type');

self::assertSame(
'Value for id has not a valid data type (given: "invalid_type"). Valid types are: null, string, instanceof NodeIdentifierInterface',
$actual->getMessage(),
);
self::assertSame(1620654936, $actual->getCode());
}
}
33 changes: 33 additions & 0 deletions Tests/Unit/Core/Exception/InvalidPropertyValueExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

/*
* This file is part of the "schema" extension for TYPO3 CMS.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*/

namespace Brotkrueml\Schema\Tests\Unit\Core\Exception;

use Brotkrueml\Schema\Core\Exception\InvalidPropertyValueException;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

#[CoversClass(InvalidPropertyValueException::class)]
final class InvalidPropertyValueExceptionTest extends TestCase
{
#[Test]
public function fromValueType(): void
{
$actual = InvalidPropertyValueException::fromValueType('someProperty', 'invalid_type');

self::assertSame(
'Value for property "someProperty" has not a valid data type (given: "invalid_type"). Valid types are: null, string, int, array, bool, instanceof TypeInterface, instanceof NodeIdentifierInterface',
$actual->getMessage(),
);
self::assertSame(1561830012, $actual->getCode());
}
}
39 changes: 39 additions & 0 deletions Tests/Unit/Core/Exception/UnknownPropertyExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

/*
* This file is part of the "schema" extension for TYPO3 CMS.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*/

namespace Brotkrueml\Schema\Tests\Unit\Core\Exception;

use Brotkrueml\Schema\Core\Exception\UnknownPropertyException;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

#[CoversClass(UnknownPropertyException::class)]
final class UnknownPropertyExceptionTest extends TestCase
{
#[Test]
public function fromPropertyNameWithTypeAsString(): void
{
$actual = UnknownPropertyException::fromPropertyName('SomeType', 'someProperty');

self::assertSame('Property "someProperty" is unknown for type "SomeType"', $actual->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());
}
}
14 changes: 8 additions & 6 deletions Tests/Unit/Core/Model/AbstractTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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');
Expand All @@ -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());
Expand All @@ -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');
Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit be6b0a3

Please sign in to comment.