-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSolidClientTest.php
More file actions
156 lines (123 loc) · 5.78 KB
/
SolidClientTest.php
File metadata and controls
156 lines (123 loc) · 5.78 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
<?php
/*
* This file is part of the Solid Client PHP project.
* (c) Kévin Dunglas <kevin@dunglas.fr>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Dunglas\PhpSolidClient\Tests;
use Dunglas\PhpSolidClient\SolidClient;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;
class SolidClientTest extends TestCase
{
private static function findHeader(array $headers, string $name): ?string
{
$prefix = $name.': ';
foreach ($headers as $header) {
if (str_starts_with($header, $prefix)) {
return substr($header, \strlen($prefix));
}
}
return null;
}
public function testPut(): void
{
$response = new MockResponse('', ['http_code' => 201]);
$httpClient = new MockHttpClient($response);
$client = new SolidClient($httpClient);
$client->put('http://pod.example/resource', '<> a <http://schema.org/Thing> .');
$this->assertSame('PUT', $response->getRequestMethod());
$this->assertSame('http://pod.example/resource', $response->getRequestUrl());
$this->assertSame('<> a <http://schema.org/Thing> .', $response->getRequestOptions()['body']);
$this->assertSame('text/turtle', self::findHeader($response->getRequestOptions()['headers'], 'Content-Type'));
}
public function testPutContainer(): void
{
$response = new MockResponse('', ['http_code' => 201]);
$httpClient = new MockHttpClient($response);
$client = new SolidClient($httpClient);
$client->put('http://pod.example/container/', null, true);
$this->assertSame('PUT', $response->getRequestMethod());
$this->assertStringContainsString('ldp#BasicContainer', self::findHeader($response->getRequestOptions()['headers'], 'Link') ?? '');
}
public function testPutCustomContentType(): void
{
$response = new MockResponse('', ['http_code' => 201]);
$httpClient = new MockHttpClient($response);
$client = new SolidClient($httpClient);
$client->put('http://pod.example/resource', '{}', false, [
'headers' => ['Content-Type' => 'application/ld+json'],
]);
$this->assertSame('application/ld+json', self::findHeader($response->getRequestOptions()['headers'], 'Content-Type'));
}
public function testHead(): void
{
$response = new MockResponse('', [
'http_code' => 200,
'response_headers' => [
'Content-Type' => 'text/turtle',
'Content-Length' => '1234',
],
]);
$httpClient = new MockHttpClient($response);
$client = new SolidClient($httpClient);
$client->head('http://pod.example/resource');
$this->assertSame('HEAD', $response->getRequestMethod());
$this->assertSame('http://pod.example/resource', $response->getRequestUrl());
}
public function testDelete(): void
{
$response = new MockResponse('', ['http_code' => 200]);
$httpClient = new MockHttpClient($response);
$client = new SolidClient($httpClient);
$client->delete('http://pod.example/resource');
$this->assertSame('DELETE', $response->getRequestMethod());
$this->assertSame('http://pod.example/resource', $response->getRequestUrl());
}
public function testPatchSparqlUpdate(): void
{
$sparql = 'INSERT DATA { <> <http://schema.org/name> "Test" . }';
$response = new MockResponse('', ['http_code' => 200]);
$httpClient = new MockHttpClient($response);
$client = new SolidClient($httpClient);
$client->patch('http://pod.example/resource', $sparql);
$this->assertSame('PATCH', $response->getRequestMethod());
$this->assertSame($sparql, $response->getRequestOptions()['body']);
$this->assertSame('application/sparql-update', self::findHeader($response->getRequestOptions()['headers'], 'Content-Type'));
}
public function testPatchN3(): void
{
$n3Patch = '@prefix solid: <http://www.w3.org/ns/solid/terms#>. _:patch a solid:InsertDeletePatch .';
$response = new MockResponse('', ['http_code' => 200]);
$httpClient = new MockHttpClient($response);
$client = new SolidClient($httpClient);
$client->patch('http://pod.example/resource', $n3Patch, 'text/n3');
$this->assertSame('text/n3', self::findHeader($response->getRequestOptions()['headers'], 'Content-Type'));
}
public function testPost(): void
{
$response = new MockResponse('', [
'http_code' => 201,
'response_headers' => ['Location' => 'http://pod.example/container/new-resource'],
]);
$httpClient = new MockHttpClient($response);
$client = new SolidClient($httpClient);
$client->post('http://pod.example/container/', '<> a <http://schema.org/Thing> .', 'new-resource');
$this->assertSame('POST', $response->getRequestMethod());
$this->assertSame('new-resource', self::findHeader($response->getRequestOptions()['headers'], 'Slug'));
$this->assertStringContainsString('ldp#Resource', self::findHeader($response->getRequestOptions()['headers'], 'Link') ?? '');
}
public function testGet(): void
{
$body = '<> a <http://schema.org/Thing> .';
$response = new MockResponse($body, ['http_code' => 200]);
$httpClient = new MockHttpClient($response);
$client = new SolidClient($httpClient);
$result = $client->get('http://pod.example/resource');
$this->assertSame('GET', $response->getRequestMethod());
$this->assertSame($body, $result->getContent());
}
}