-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathFieldsTest.php
102 lines (80 loc) · 3.21 KB
/
FieldsTest.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
92
93
94
95
96
97
98
99
100
101
102
<?php
declare(strict_types=1);
namespace Tests\Unit\Support;
use Illuminate\Foundation\Application;
use Illuminate\Http\Request;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Throwable;
use TiMacDonald\JsonApi\Support\Fields;
class FieldsTest extends TestCase
{
public function testItIsASingleton(): void
{
$this->assertSame(Fields::getInstance(), Fields::getInstance());
}
public function testItHandlesMultipleRequests(): void
{
$requests = [
Request::create('https://example.com?fields[a]=a'),
Request::create('https://example.com?fields[b]=b'),
];
$fields = [
Fields::getInstance()->parse($requests[0], 'a', true),
Fields::getInstance()->parse($requests[1], 'b', true),
];
$this->assertSame(['a'], $fields[0]);
$this->assertSame(['b'], $fields[1]);
}
public function testItHandlesEmptyValues(): void
{
$request = Request::create('https://example.com?fields[a]=');
$this->assertSame([], Fields::getInstance()->parse($request, 'a', true));
}
public function testItAbortsWhenFieldsIsNotAnArray(): void
{
Application::getInstance();
$request = Request::create('https://example.com?fields=as');
try {
Fields::getInstance()->parse($request, 'a', true);
$this->fail('Exception should have been thrown');
} catch (HttpException $e) {
$this->assertSame('The fields parameter must be an array of resource types.', $e->getMessage());
$this->assertSame(400, $e->getStatusCode());
} catch (Throwable) {
$this->fail('Http exception should have been thrown');
}
}
public function testItMustProvideStringForFields(): void
{
Application::getInstance();
$request = Request::create('https://example.com?fields[foo][bar]=1');
try {
Fields::getInstance()->parse($request, 'foo', true);
$this->fail('Exception should have been thrown');
} catch (HttpException $e) {
$this->assertSame('The fields parameter value must be a comma separated list of attributes.', $e->getMessage());
$this->assertSame(400, $e->getStatusCode());
} catch (Throwable) {
$this->fail('Http exception should have been thrown');
}
}
public function testWhenRequestingEmptyListItReturnsEmptyArray()
{
$request = Request::create('https://example.com?fields[foo]=');
$includes = Fields::getInstance()->parse($request, 'foo', true);
$this->assertSame([], $includes);
}
public function testWhenNotSpecifyingResourceFieldsReturnsNull()
{
$request = Request::create('https://example.com?fields[foo]=bar');
$includes = Fields::getInstance()->parse($request, 'baz', false);
$this->assertNull($includes);
}
public function testWhenNotSpecifyingResourceFieldsReturnsEmptyArrayForMinimalFields()
{
$request = Request::create('https://example.com?fields[foo]=bar');
$includes = Fields::getInstance()->parse($request, 'baz', true);
$this->assertSame([], $includes);
}
}