-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathJsonSerializerTest.php
More file actions
168 lines (144 loc) · 5.1 KB
/
JsonSerializerTest.php
File metadata and controls
168 lines (144 loc) · 5.1 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
namespace MixerApi\HalView\Test\TestCase;
use Cake\Datasource\FactoryLocator;
use Cake\Datasource\Paging\PaginatedResultSet;
use Cake\Event\EventList;
use Cake\Event\EventManager;
use Cake\Http\Response;
use Cake\Http\ServerRequest;
use Cake\ORM\Table;
use Cake\Routing\RouteBuilder;
use Cake\Routing\Router;
use Cake\TestSuite\TestCase;
use Cake\View\Helper\PaginatorHelper;
use MixerApi\HalView\JsonSerializer;
use MixerApi\HalView\View\HalJsonView;
class JsonSerializerTest extends TestCase
{
/**
* @var string
*/
private const EXT = 'haljson';
/**
* @var string
*/
private const VIEW_CLASS = 'MixerApi/HalView.HalJson';
/**
* @var string[]
*/
public array $fixtures = [
'plugin.MixerApi/HalView.Actors',
'plugin.MixerApi/HalView.FilmActors',
'plugin.MixerApi/HalView.Films',
];
/**
* @var ServerRequest
*/
private $request;
/**
* @var Response
*/
private $response;
public function setUp(): void
{
parent::setUp(); // TODO: Change the autogenerated stub
static::setAppNamespace('MixerApi\HalView\Test\App');
$this->request = (new ServerRequest([
'url' => '/',
'params' => [
'plugin' => null,
'controller' => 'Actor',
'action' => 'index',
],
]))->withEnv('HTTP_ACCEPT', 'application/hal+json');
Router::reload();
Router::createRouteBuilder('/')->scope('/', function (RouteBuilder $builder) {
$builder->setExtensions(['json']);
$builder->connect('/', ['controller' => 'Actors', 'action' => 'index']);
$builder->connect('/{controller}/{action}/*');
$builder->connect('/{plugin}/{controller}/{action}/*');
});
Router::setRequest($this->request);
$this->response = new Response();
}
public function test_collection(): void
{
EventManager::instance()->trackEvents(true)->setEventList(new EventList());
$actor = FactoryLocator::get('Table')->get('Actors');
$result = new PaginatedResultSet($actor->find()->applyOptions(['contain' => 'Films'])->limit(1)->all(), [
'sort' => null,
'direction' => null,
'perPage' => 20,
'requestedPage' => 1,
'alias' => 'Actors',
'scope' => null,
'limit' => null,
'count' => 20,
'totalCount' => 60,
'pageCount' => 1,
'currentPage' => 1,
'start' => 1,
'end' => 3,
'hasPrevPage' => false,
'hasNextPage' => true,
]);
$paginator = new PaginatorHelper(
new HalJsonView($this->request, $this->response),
['templates' => 'MixerApi/HalView.paginator-template']
);
$paginator->setPaginated($result);
$jsonSerializer = new JsonSerializer($result, $this->request, $paginator);
$json = $jsonSerializer->asJson(JSON_PRETTY_PRINT);
$this->assertIsString($json);
$this->assertIsObject(json_decode($json));
$this->assertEventFired(JsonSerializer::BEFORE_SERIALIZE_EVENT);
$this->assertEventFired(JsonSerializer::AFTER_SERIALIZE_EVENT);
}
public function test_item(): void
{
/** @var Table $actorsTable */
$actorsTable = FactoryLocator::get('Table')->get('Actors');
$result = $actorsTable->get(primaryKey: 1, contain : 'Films');
$paginator = new PaginatorHelper(
new HalJsonView($this->request, $this->response),
['templates' => 'MixerApi/HalView.paginator-template']
);
$jsonSerializer = new JsonSerializer($result, $this->request, $paginator);
$json = $jsonSerializer->asJson(JSON_PRETTY_PRINT);
$this->assertIsString($json);
$this->assertIsObject(json_decode($json));
}
public function test_get_data(): void
{
$actor = FactoryLocator::get('Table')->get('Actors');
$result = new PaginatedResultSet($actor->find()->applyOptions(['contain' => 'Films'])->limit(1)->all(), [
'sort' => null,
'direction' => null,
'perPage' => 20,
'requestedPage' => 1,
'alias' => 'Actors',
'scope' => null,
'limit' => null,
'count' => 20,
'totalCount' => 60,
'pageCount' => 1,
'currentPage' => 1,
'start' => 1,
'end' => 3,
'hasPrevPage' => false,
'hasNextPage' => true,
]);
$paginator = new PaginatorHelper(
new HalJsonView($this->request, $this->response),
['templates' => 'MixerApi/HalView.paginator-template']
);
$paginator->setPaginated($result);
$jsonSerializer = new JsonSerializer($result, $this->request, $paginator);
$this->assertIsArray($jsonSerializer->getData());
}
public function test_as_json_throws_run_time_exception(): void
{
$this->expectException(\RuntimeException::class);
(new JsonSerializer(NAN))->asJson(0);
}
}