-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathChainTest.php
More file actions
73 lines (60 loc) · 2.37 KB
/
ChainTest.php
File metadata and controls
73 lines (60 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
declare(strict_types=1);
namespace PhproTest\ApiProblemBundle\Transformer;
use Exception;
use Phpro\ApiProblem\ApiProblemInterface;
use Phpro\ApiProblem\Http\ExceptionApiProblem;
use Phpro\ApiProblemBundle\Transformer\Chain;
use Phpro\ApiProblemBundle\Transformer\ExceptionTransformerInterface;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
#[CoversClass(Chain::class)]
class ChainTest extends TestCase
{
use ProphecyTrait;
#[Test]
public function it_is_an_exception_transformer(): void
{
$transformer = new Chain([]);
$this->assertInstanceOf(ExceptionTransformerInterface::class, $transformer);
}
#[Test]
public function it_accepts_any_exception(): void
{
$transformer = new Chain([]);
$this->assertTrue($transformer->accepts(new Exception()));
}
#[Test]
public function it_transforms_with_first_acceptable_transformer(): void
{
$transformer = new Chain([
$this->mockTransformer(false),
$this->mockTransformer(true, $apiProblem1 = $this->prophesize(ApiProblemInterface::class)->reveal()),
$this->mockTransformer(true, $apiProblem2 = $this->prophesize(ApiProblemInterface::class)->reveal()),
]);
$this->assertEquals($apiProblem1, $transformer->transform(new Exception()));
}
#[Test]
public function it_transforms_to_basic_exception_problem_when_no_transformer_matches(): void
{
$transformer = new Chain([$this->mockTransformer(false)]);
$this->assertInstanceOf(ExceptionApiProblem::class, $transformer->transform(new Exception()));
}
private function mockTransformer(bool $accepts, ?ApiProblemInterface $apiProblem = null): ExceptionTransformerInterface
{
/** @var ExceptionTransformerInterface|ObjectProphecy $transformer */
$transformer = $this->prophesize(ExceptionTransformerInterface::class);
$transformer->accepts(Argument::any())->willReturn($accepts);
if ($apiProblem) {
$transformer->transform(Argument::any())->willReturn($apiProblem);
}
if (!$accepts) {
$transformer->transform(Argument::any())->shouldNotBeCalled();
}
return $transformer->reveal();
}
}