|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * conjoon |
| 5 | + * php-lib-conjoon |
| 6 | + * Copyright (C) 2022 Thorsten Suckow-Homberg https://github.com/conjoon/php-lib-conjoon |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person |
| 9 | + * obtaining a copy of this software and associated documentation |
| 10 | + * files (the "Software"), to deal in the Software without restriction, |
| 11 | + * including without limitation the rights to use, copy, modify, merge, |
| 12 | + * publish, distribute, sublicense, and/or sell copies of the Software, |
| 13 | + * and to permit persons to whom the Software is furnished to do so, |
| 14 | + * subject to the following conditions: |
| 15 | + * |
| 16 | + * The above copyright notice and this permission notice shall be included |
| 17 | + * in all copies or substantial portions of the Software. |
| 18 | + * |
| 19 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 20 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 21 | + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 22 | + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 23 | + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 24 | + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
| 25 | + * USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 26 | + */ |
| 27 | + |
| 28 | +declare(strict_types=1); |
| 29 | + |
| 30 | +namespace Tests\Conjoon\JsonApi\Resource; |
| 31 | + |
| 32 | +use Conjoon\Core\AbstractList; |
| 33 | +use Conjoon\JsonApi\Resource\Resource; |
| 34 | +use Conjoon\JsonApi\Resource\ResourceList; |
| 35 | +use Conjoon\Net\Uri\Component\Path\Template; |
| 36 | +use InvalidArgumentException; |
| 37 | +use PHPUnit\Framework\MockObject\MockObject; |
| 38 | +use Tests\TestCase; |
| 39 | + |
| 40 | +/** |
| 41 | + * tests ResourceList |
| 42 | + */ |
| 43 | +class ResourceListTest extends TestCase |
| 44 | +{ |
| 45 | + /** |
| 46 | + * Tests constructor |
| 47 | + */ |
| 48 | + public function testClass(): void |
| 49 | + { |
| 50 | + $list = $this->createList(); |
| 51 | + $this->assertInstanceOf(AbstractList::class, $list); |
| 52 | + |
| 53 | + $this->assertSame(Resource::class, $list->getEntityType()); |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | + /** |
| 58 | + * tests offsetSet(), makes sure no URI duplicates may occur |
| 59 | + * @return void |
| 60 | + */ |
| 61 | + public function testOffsetSet(): void |
| 62 | + { |
| 63 | + $resource = function (string $uri): MockObject&Resource { |
| 64 | + /** |
| 65 | + * @var MockObject&Resource $res |
| 66 | + */ |
| 67 | + $res = $this->createMockForAbstract(Resource::class, ["getUri"]); |
| 68 | + $res->expects($this->any())->method("getUri")->willReturn(new Template($uri)); |
| 69 | + return $res; |
| 70 | + }; |
| 71 | + |
| 72 | + $resourceList = new ResourceList(); |
| 73 | + $resourceList[] = $resource("/uri1"); |
| 74 | + $resourceList[] = $resource("/uri2"); |
| 75 | + $resourceList[] = $resource("/uri3"); |
| 76 | + |
| 77 | + try { |
| 78 | + $resourceList[] = $resource("/uri2"); |
| 79 | + $this->fail(); |
| 80 | + } catch (InvalidArgumentException $e) { |
| 81 | + $this->assertStringContainsString( |
| 82 | + strtolower("a resource for the URI \"/uri2\" already exists"), |
| 83 | + strtolower($e->getMessage()) |
| 84 | + ); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + |
| 89 | + /** |
| 90 | + * @return ResourceList |
| 91 | + */ |
| 92 | + protected function createList(): ResourceList |
| 93 | + { |
| 94 | + $list = new ResourceList(); |
| 95 | + |
| 96 | + |
| 97 | + return $list; |
| 98 | + } |
| 99 | +} |
0 commit comments