-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cycle\Schema\Provider\Tests\Unit\Exception; | ||
|
||
use Cycle\Schema\Provider\Exception\BadDeclarationException; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class BadDeclarationExceptionTest extends TestCase | ||
{ | ||
private const DEFAULT_CLASS = \stdClass::class; | ||
private const DEFAULT_PARAMETER = 'Default parameter'; | ||
private const DEFAULT_MESSAGE_PATTERN = '/Default parameter should be instance of stdClass or its declaration\\./'; | ||
private const RECEIVED_PATTERN = '/%s was received instead\\./'; | ||
|
||
protected function prepareException( | ||
mixed $argument, | ||
string $parameter = self::DEFAULT_PARAMETER, | ||
string $class = self::DEFAULT_CLASS | ||
): BadDeclarationException { | ||
return new BadDeclarationException($parameter, $class, $argument); | ||
} | ||
|
||
/** | ||
* @dataProvider argumentValueProvider | ||
*/ | ||
public function testTypeMessage(mixed $value, string $message): void | ||
{ | ||
$exception = $this->prepareException($value); | ||
$pattern = sprintf(self::RECEIVED_PATTERN, $message); | ||
|
||
$this->assertMatchesRegularExpression($pattern, $exception->getMessage()); | ||
} | ||
|
||
public function testDefaultState(): void | ||
{ | ||
$exception = $this->prepareException(null); | ||
|
||
$this->assertInstanceOf(\Throwable::class, $exception); | ||
$this->assertSame(0, $exception->getCode()); | ||
$this->assertMatchesRegularExpression(self::DEFAULT_MESSAGE_PATTERN, $exception->getMessage()); | ||
} | ||
|
||
public static function argumentValueProvider(): \Traversable | ||
{ | ||
yield [null, 'Null']; | ||
yield [42, 'Int']; | ||
yield [new \DateTimeImmutable(), 'Instance of DateTimeImmutable']; | ||
yield [STDIN, 'Resource \\(stream\\)']; | ||
yield [[], 'Array']; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cycle\Schema\Provider\Tests\Unit\Exception; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Cycle\Schema\Provider\Exception\CumulativeException; | ||
|
||
final class CumulativeExceptionTest extends TestCase | ||
{ | ||
private function prepareException(\Throwable ...$exceptions): CumulativeException | ||
{ | ||
return new CumulativeException(...$exceptions); | ||
} | ||
|
||
public function testDefaultState(): void | ||
{ | ||
$exception = $this->prepareException(); | ||
|
||
$this->assertInstanceOf(\Throwable::class, $exception); | ||
$this->assertSame('0 exceptions were thrown.', $exception->getMessage()); | ||
$this->assertSame(0, $exception->getCode()); | ||
} | ||
|
||
public function testGetExceptions(): void | ||
{ | ||
$list = [ | ||
new \RuntimeException(), | ||
new \Exception(), | ||
new \InvalidArgumentException(), | ||
]; | ||
|
||
$exception = $this->prepareException(...$list); | ||
|
||
$this->assertSame($list, $exception->getExceptions()); | ||
} | ||
|
||
public function testGetMessageWithOneException(): void | ||
{ | ||
$list = [ | ||
new \RuntimeException('Foo message.', 42), | ||
]; | ||
|
||
$exception = $this->prepareException(...$list); | ||
|
||
$this->assertIsInt(strpos($exception->getMessage(), '[RuntimeException] #42: Foo message.')); | ||
$this->assertMatchesRegularExpression('/One exception was thrown\\./', $exception->getMessage()); | ||
} | ||
|
||
public function testGetMessageWithMultipleExceptions(): void | ||
{ | ||
$list = [ | ||
new \RuntimeException('Foo message.', 42), | ||
new \Exception('Bar message.'), | ||
new \InvalidArgumentException('Baz message.'), | ||
]; | ||
|
||
$exception = $this->prepareException(...$list); | ||
|
||
$this->assertMatchesRegularExpression('/3 exceptions were thrown\\./', $exception->getMessage()); | ||
|
||
$this->assertMatchesRegularExpression( | ||
'/\\n1\\) [^\\n]++\\n\\[RuntimeException\\] \\#42\\: Foo message\\./', | ||
$exception->getMessage() | ||
); | ||
$this->assertMatchesRegularExpression( | ||
'/\\n2\\) [^\\n]++\\n\\[Exception] #0: Bar message./', | ||
$exception->getMessage() | ||
); | ||
$this->assertMatchesRegularExpression( | ||
'/\\n3\\) [^\\n]++\\n\\[InvalidArgumentException] #0: Baz message./', | ||
$exception->getMessage() | ||
); | ||
} | ||
} |