-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathErrorToPhpArrayEncoderTest.php
91 lines (77 loc) · 3.62 KB
/
ErrorToPhpArrayEncoderTest.php
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
declare(strict_types=1);
namespace Undabot\JsonApi\Tests\Unit\Encoding\PhpArray\Encode;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Undabot\JsonApi\Definition\Encoding\ErrorToPhpArrayEncoderInterface;
use Undabot\JsonApi\Definition\Encoding\LinkToPhpArrayEncoderInterface;
use Undabot\JsonApi\Definition\Encoding\MetaToPhpArrayEncoderInterface;
use Undabot\JsonApi\Definition\Encoding\SourceToPhpArrayEncoderInterface;
use Undabot\JsonApi\Definition\Model\Error\ErrorInterface;
use Undabot\JsonApi\Definition\Model\Link\LinkInterface;
use Undabot\JsonApi\Definition\Model\Meta\MetaInterface;
use Undabot\JsonApi\Definition\Model\Source\SourceInterface;
use Undabot\JsonApi\Implementation\Encoding\ErrorToPhpArrayEncoder;
/**
* @internal
*
* @covers \Undabot\JsonApi\Implementation\Encoding\ErrorToPhpArrayEncoder
*
* @small
*/
final class ErrorToPhpArrayEncoderTest extends TestCase
{
private MockObject $linkEncoderMock;
private MockObject $sourceEncoderMock;
private MockObject $metaEncoderMock;
private ErrorToPhpArrayEncoder $errorEncoder;
protected function setUp(): void
{
$this->linkEncoderMock = $this->createMock(LinkToPhpArrayEncoderInterface::class);
$this->sourceEncoderMock = $this->createMock(SourceToPhpArrayEncoderInterface::class);
$this->metaEncoderMock = $this->createMock(MetaToPhpArrayEncoderInterface::class);
$this->errorEncoder = new ErrorToPhpArrayEncoder(
$this->linkEncoderMock,
$this->sourceEncoderMock,
$this->metaEncoderMock
);
}
public function testItCanBeConstructed(): void
{
self::assertInstanceOf(ErrorToPhpArrayEncoder::class, $this->errorEncoder);
self::assertInstanceOf(ErrorToPhpArrayEncoderInterface::class, $this->errorEncoder);
}
public function testEncoderSuccessfullyEncodesPrimitiveValues(): void
{
$error = $this->createMock(ErrorInterface::class);
$error->expects(self::exactly(1))->method('getId')->willReturn('id');
$error->expects(self::exactly(1))->method('getStatus')->willReturn('status 1');
$error->expects(self::exactly(1))->method('getCode')->willReturn('code 1');
$error->expects(self::exactly(1))->method('getTitle')->willReturn('title 1');
$error->expects(self::exactly(1))->method('getDetail')->willReturn('detail 1');
$encoded = $this->errorEncoder->encode($error);
self::assertIsArray($encoded);
self::assertCount(5, $encoded);
self::assertSame('id', $encoded['id']);
self::assertSame('status 1', $encoded['status']);
self::assertSame('code 1', $encoded['code']);
self::assertSame('title 1', $encoded['title']);
self::assertSame('detail 1', $encoded['detail']);
}
public function testErrorEncoderWillCallSpecificObjectEncoders(): void
{
$error = $this->createMock(ErrorInterface::class);
$link = $this->createMock(LinkInterface::class);
$error->expects(self::exactly(1))->method('getAboutLink')->willReturn($link);
$source = $this->createMock(SourceInterface::class);
$error->expects(self::exactly(1))->method('getSource')->willReturn($source);
$meta = $this->createMock(MetaInterface::class);
$error->expects(self::exactly(1))->method('getMeta')->willReturn($meta);
$encoded = $this->errorEncoder->encode($error);
self::assertIsArray($encoded);
self::assertCount(3, $encoded);
self::assertArrayHasKey('links', $encoded);
self::assertArrayHasKey('source', $encoded);
self::assertArrayHasKey('meta', $encoded);
}
}