|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tests\Unit; |
|
4
|
|
|
|
|
5
|
|
|
use Happyr\JsonApiResponseFactory\ResponseFactory; |
|
6
|
|
|
use Happyr\JsonApiResponseFactory\ResponseModelInterface; |
|
7
|
|
|
use Happyr\JsonApiResponseFactory\Transformer\AbstractTransformer; |
|
8
|
|
|
use League\Fractal\Manager; |
|
9
|
|
|
use League\Fractal\Pagination\Cursor; |
|
10
|
|
|
use League\Fractal\Pagination\PaginatorInterface; |
|
11
|
|
|
use League\Fractal\Scope; |
|
12
|
|
|
use Nyholm\NSA; |
|
13
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
14
|
|
|
use PHPUnit\Framework\TestCase; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @author Radoje Albijanic <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
class ResponseFactoryTest extends TestCase |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var ResponseFactory |
|
23
|
|
|
*/ |
|
24
|
|
|
private $factory; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var MockObject |
|
28
|
|
|
*/ |
|
29
|
|
|
private $fractal; |
|
30
|
|
|
|
|
31
|
|
|
protected function setUp(): void |
|
32
|
|
|
{ |
|
33
|
|
|
$this->fractal = $this->getMockBuilder(Manager::class) |
|
34
|
|
|
->disableOriginalConstructor() |
|
35
|
|
|
->getMock(); |
|
36
|
|
|
$this->factory = new ResponseFactory( |
|
37
|
|
|
$this->fractal |
|
38
|
|
|
); |
|
39
|
|
|
parent::setUp(); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function testWithPaginator(): void |
|
43
|
|
|
{ |
|
44
|
|
|
$paginator = new DummyPaginator(); |
|
45
|
|
|
|
|
46
|
|
|
$newFactory = $this->factory->withPaginator($paginator); |
|
47
|
|
|
self::assertEquals($paginator, NSA::getProperty($newFactory, 'paginator')); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function testWithCursor(): void |
|
51
|
|
|
{ |
|
52
|
|
|
$cursor = new Cursor(); |
|
53
|
|
|
|
|
54
|
|
|
$newFactory = $this->factory->withCursor($cursor); |
|
55
|
|
|
self::assertEquals($cursor, NSA::getProperty($newFactory, 'cursor')); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function testCreateWithResponseModel(): void |
|
59
|
|
|
{ |
|
60
|
|
|
$response = $this->factory->createWithResponseModel(new DummyResponseModel()); |
|
61
|
|
|
self::assertEquals(json_encode(['someKey' => 'someValue']), $response->getContent()); |
|
62
|
|
|
self::assertEquals(401, $response->getStatusCode()); |
|
63
|
|
|
self::assertTrue($response->headers->has('Content-Type')); |
|
64
|
|
|
self::assertEquals('application/vnd.api+json', $response->headers->get('Content-Type')); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function testCreateWithItem(): void |
|
68
|
|
|
{ |
|
69
|
|
|
$scope = $this->getMockBuilder(Scope::class) |
|
70
|
|
|
->disableOriginalConstructor() |
|
71
|
|
|
->getMock(); |
|
72
|
|
|
$scope->method('toArray') |
|
73
|
|
|
->willReturn(['someKey' => 'someValue']); |
|
74
|
|
|
$this->fractal->method('createData') |
|
75
|
|
|
->willReturn($scope); |
|
76
|
|
|
$response = $this->factory->createWithItem(new \stdClass(), new DummyTransformer()); |
|
77
|
|
|
|
|
78
|
|
|
self::assertEquals( |
|
79
|
|
|
json_encode(['someKey' => 'someValue']), |
|
80
|
|
|
$response->getContent() |
|
81
|
|
|
); |
|
82
|
|
|
self::assertTrue($response->headers->has('Content-Type')); |
|
83
|
|
|
self::assertEquals('application/vnd.api+json', $response->headers->get('Content-Type')); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function testCreateWithCollection(): void |
|
87
|
|
|
{ |
|
88
|
|
|
$scope = $this->getMockBuilder(Scope::class) |
|
89
|
|
|
->disableOriginalConstructor() |
|
90
|
|
|
->getMock(); |
|
91
|
|
|
$scope->method('toArray') |
|
92
|
|
|
->willReturn(['someKey' => 'someValue', 'someOtherKey' => 'someOtherValue']); |
|
93
|
|
|
$this->fractal->method('createData') |
|
94
|
|
|
->willReturn($scope); |
|
95
|
|
|
$response = $this->factory->createWithCollection([new \stdClass()], new DummyTransformer()); |
|
96
|
|
|
|
|
97
|
|
|
self::assertEquals( |
|
98
|
|
|
json_encode(['someKey' => 'someValue', 'someOtherKey' => 'someOtherValue']), |
|
99
|
|
|
$response->getContent() |
|
100
|
|
|
); |
|
101
|
|
|
self::assertTrue($response->headers->has('Content-Type')); |
|
102
|
|
|
self::assertEquals('application/vnd.api+json', $response->headers->get('Content-Type')); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
class DummyPaginator implements PaginatorInterface |
|
107
|
|
|
{ |
|
108
|
|
|
public function getCurrentPage() |
|
109
|
|
|
{ |
|
110
|
|
|
return 1; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
public function getLastPage() |
|
114
|
|
|
{ |
|
115
|
|
|
return 2; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
public function getTotal() |
|
119
|
|
|
{ |
|
120
|
|
|
return 2; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function getCount() |
|
124
|
|
|
{ |
|
125
|
|
|
return 20; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function getPerPage() |
|
129
|
|
|
{ |
|
130
|
|
|
return 10; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
public function getUrl($page) |
|
134
|
|
|
{ |
|
135
|
|
|
return 'http://dummy-domain-name.dummy-domain'; |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
class DummyResponseModel implements ResponseModelInterface |
|
140
|
|
|
{ |
|
141
|
|
|
public function getHttpStatusCode(): int |
|
142
|
|
|
{ |
|
143
|
|
|
return 401; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
public function getPayload(): array |
|
147
|
|
|
{ |
|
148
|
|
|
return ['someKey' => 'someValue']; |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
class DummyTransformer extends AbstractTransformer |
|
153
|
|
|
{ |
|
154
|
|
|
public function getResourceName(): string |
|
155
|
|
|
{ |
|
156
|
|
|
return 'dummy-item'; |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|