|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Solid Client PHP project. |
| 5 | + * (c) Kévin Dunglas <kevin@dunglas.fr> |
| 6 | + * For the full copyright and license information, please view the LICENSE |
| 7 | + * file that was distributed with this source code. |
| 8 | + */ |
| 9 | + |
| 10 | +declare(strict_types=1); |
| 11 | + |
| 12 | +namespace Dunglas\PhpSolidClient\Tests; |
| 13 | + |
| 14 | +use Dunglas\PhpSolidClient\ContainerEntry; |
| 15 | +use Dunglas\PhpSolidClient\SolidClient; |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | +use Symfony\Component\HttpClient\MockHttpClient; |
| 18 | +use Symfony\Component\HttpClient\Response\MockResponse; |
| 19 | + |
| 20 | +class ContainerOperationsTest extends TestCase |
| 21 | +{ |
| 22 | + public function testGetResourceMetadata(): void |
| 23 | + { |
| 24 | + $response = new MockResponse('', [ |
| 25 | + 'http_code' => 200, |
| 26 | + 'response_headers' => [ |
| 27 | + 'Content-Type' => 'text/turtle', |
| 28 | + 'Content-Length' => '512', |
| 29 | + 'Link' => '<http://www.w3.org/ns/ldp#Resource>; rel="type"', |
| 30 | + 'WAC-Allow' => 'user="read write",public="read"', |
| 31 | + ], |
| 32 | + ]); |
| 33 | + $httpClient = new MockHttpClient($response); |
| 34 | + $client = new SolidClient($httpClient); |
| 35 | + |
| 36 | + $metadata = $client->getResourceMetadata('http://pod.example/resource'); |
| 37 | + |
| 38 | + $this->assertSame('text/turtle', $metadata->contentType); |
| 39 | + $this->assertSame(512, $metadata->contentLength); |
| 40 | + $this->assertSame('http://www.w3.org/ns/ldp#Resource', $metadata->ldpType); |
| 41 | + $this->assertFalse($metadata->isContainer()); |
| 42 | + $this->assertSame(['read', 'write'], $metadata->wacAllow['user']); |
| 43 | + $this->assertSame(['read'], $metadata->wacAllow['public']); |
| 44 | + } |
| 45 | + |
| 46 | + public function testGetContainerContents(): void |
| 47 | + { |
| 48 | + $jsonLd = json_encode([ |
| 49 | + '@id' => 'http://pod.example/container/', |
| 50 | + '@type' => ['http://www.w3.org/ns/ldp#BasicContainer'], |
| 51 | + 'http://www.w3.org/ns/ldp#contains' => [ |
| 52 | + ['@id' => 'http://pod.example/container/file.txt'], |
| 53 | + [ |
| 54 | + '@id' => 'http://pod.example/container/sub/', |
| 55 | + '@type' => ['http://www.w3.org/ns/ldp#BasicContainer'], |
| 56 | + ], |
| 57 | + ], |
| 58 | + ]); |
| 59 | + |
| 60 | + $response = new MockResponse($jsonLd, [ |
| 61 | + 'http_code' => 200, |
| 62 | + 'response_headers' => ['Content-Type' => 'application/ld+json'], |
| 63 | + ]); |
| 64 | + $httpClient = new MockHttpClient($response); |
| 65 | + $client = new SolidClient($httpClient); |
| 66 | + |
| 67 | + $entries = $client->getContainerContents('http://pod.example/container/'); |
| 68 | + |
| 69 | + $this->assertCount(2, $entries); |
| 70 | + $this->assertInstanceOf(ContainerEntry::class, $entries[0]); |
| 71 | + $this->assertSame('http://pod.example/container/file.txt', $entries[0]->url); |
| 72 | + $this->assertFalse($entries[0]->isContainer); |
| 73 | + $this->assertSame('http://pod.example/container/sub/', $entries[1]->url); |
| 74 | + $this->assertTrue($entries[1]->isContainer); |
| 75 | + } |
| 76 | + |
| 77 | + public function testGetContainerContentsWithRelativeIds(): void |
| 78 | + { |
| 79 | + $jsonLd = json_encode([ |
| 80 | + '@id' => './', |
| 81 | + '@type' => ['http://www.w3.org/ns/ldp#BasicContainer'], |
| 82 | + 'http://www.w3.org/ns/ldp#contains' => [ |
| 83 | + ['@id' => 'file.txt'], |
| 84 | + ['@id' => 'sub/', '@type' => ['http://www.w3.org/ns/ldp#BasicContainer']], |
| 85 | + ], |
| 86 | + ]); |
| 87 | + |
| 88 | + $response = new MockResponse($jsonLd, [ |
| 89 | + 'http_code' => 200, |
| 90 | + 'response_headers' => ['Content-Type' => 'application/ld+json'], |
| 91 | + ]); |
| 92 | + $httpClient = new MockHttpClient($response); |
| 93 | + $client = new SolidClient($httpClient); |
| 94 | + |
| 95 | + $entries = $client->getContainerContents('http://pod.example/data/'); |
| 96 | + |
| 97 | + $this->assertCount(2, $entries); |
| 98 | + $this->assertSame('http://pod.example/data/file.txt', $entries[0]->url); |
| 99 | + $this->assertSame('http://pod.example/data/sub/', $entries[1]->url); |
| 100 | + } |
| 101 | +} |
0 commit comments