Skip to content

Commit

Permalink
chore: use named constructor for exceptions in MultipleType class
Browse files Browse the repository at this point in the history
  • Loading branch information
brotkrueml committed Feb 21, 2025
1 parent be6b0a3 commit 084f627
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 14 deletions.
30 changes: 30 additions & 0 deletions Classes/Core/Exception/InvalidNumberOfTypesException.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 InvalidNumberOfTypesException extends \DomainException
{
/**
* @param string[] $singleTypes
*/
public static function fromSingleTypes(array $singleTypes): self
{
return new self(
\sprintf(
'At least two types have to be assigned, "%s" (%d) given',
\implode(', ', $singleTypes),
\count($singleTypes),
),
1621871446,
);
}
}
29 changes: 29 additions & 0 deletions Classes/Core/Exception/SameTypeForMultipleTypeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?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 SameTypeForMultipleTypeException extends \DomainException
{
/**
* @param string[] $singleTypes
*/
public static function fromSingleTypes(array $singleTypes): self
{
return new self(
\sprintf(
'Only different types can be used as arguments for a multiple type, "%s" given',
\implode(', ', $singleTypes),
),
1621871950,
);
}
}
13 changes: 5 additions & 8 deletions Classes/Core/Model/MultipleType.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

namespace Brotkrueml\Schema\Core\Model;

use Brotkrueml\Schema\Core\Exception\InvalidNumberOfTypesException;
use Brotkrueml\Schema\Core\Exception\SameTypeForMultipleTypeException;

final class MultipleType extends AbstractBaseType
{
/**
Expand Down Expand Up @@ -42,20 +45,14 @@ private function checkForSameTypes(): void
{
$uniqueTypeNames = \array_unique($this->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);
}
}

Expand Down
45 changes: 45 additions & 0 deletions Tests/Unit/Core/Exception/InvalidNumberOfTypesExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?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\InvalidNumberOfTypesException;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

#[CoversClass(InvalidNumberOfTypesException::class)]
final class InvalidNumberOfTypesExceptionTest extends TestCase
{
#[Test]
public function fromSingleTypesWithNoneGiven(): void
{
$actual = InvalidNumberOfTypesException::fromSingleTypes([]);

self::assertSame(
'At least two types have to be assigned, "" (0) given',
$actual->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());
}
}
33 changes: 33 additions & 0 deletions Tests/Unit/Core/Exception/SameTypeForMultipleTypeExceptionTest.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\SameTypeForMultipleTypeException;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

#[CoversClass(SameTypeForMultipleTypeException::class)]
final class SameTypeForMultipleTypeExceptionTest extends TestCase
{
#[Test]
public function fromSingleTypes(): void
{
$actual = SameTypeForMultipleTypeException::fromSingleTypes(['SomeType', 'AnotherType', 'SomeType']);

self::assertSame(
'Only different types can be used as arguments for a multiple type, "SomeType, AnotherType, SomeType" given',
$actual->getMessage(),
);
self::assertSame(1621871950, $actual->getCode());
}
}
11 changes: 5 additions & 6 deletions Tests/Unit/Core/Model/MultipleTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -45,29 +47,26 @@ 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();
}

#[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());
}

#[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());
}
Expand Down

0 comments on commit 084f627

Please sign in to comment.