Skip to content

Commit

Permalink
Add unit tests for exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
msmakouz committed Feb 1, 2024
1 parent 78d4859 commit f2483b1
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 0 deletions.
53 changes: 53 additions & 0 deletions tests/Unit/Exception/BadDeclarationExceptionTest.php
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'];
}
}
76 changes: 76 additions & 0 deletions tests/Unit/Exception/CumulativeExceptionTest.php
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()
);
}
}

0 comments on commit f2483b1

Please sign in to comment.