|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Art4\JsonApiClient\Resource\Tests; |
| 4 | + |
| 5 | +use Art4\JsonApiClient\Resource\ItemLink; |
| 6 | +use Art4\JsonApiClient\Tests\Fixtures\HelperTrait; |
| 7 | + |
| 8 | +class ItemLinkTest extends \PHPUnit_Framework_TestCase |
| 9 | +{ |
| 10 | + use HelperTrait; |
| 11 | + |
| 12 | + /** |
| 13 | + * @setup |
| 14 | + */ |
| 15 | + public function setUp() |
| 16 | + { |
| 17 | + $this->manager = $this->buildManagerMock(); |
| 18 | + |
| 19 | + // Mock parent |
| 20 | + $this->parent = $this->getMockBuilder('Art4\JsonApiClient\AccessInterface') |
| 21 | + ->getMock(); |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * @test parsing of all properties |
| 26 | + */ |
| 27 | + public function testParsingPropertiesExists() |
| 28 | + { |
| 29 | + $object = new \stdClass(); |
| 30 | + $object->self = 'http://example.org/self'; |
| 31 | + $object->custom = 'http://example.org/custom'; |
| 32 | + $object->related = new \stdClass(); |
| 33 | + |
| 34 | + $link = new ItemLink($object, $this->manager, $this->parent); |
| 35 | + |
| 36 | + $this->assertInstanceOf('Art4\JsonApiClient\Resource\ItemLink', $link); |
| 37 | + $this->assertInstanceOf('Art4\JsonApiClient\AccessInterface', $link); |
| 38 | + $this->assertSame($link->getKeys(), array('self', 'custom', 'related')); |
| 39 | + |
| 40 | + $this->assertTrue($link->has('self')); |
| 41 | + $this->assertSame($link->get('self'), 'http://example.org/self'); |
| 42 | + $this->assertTrue($link->has('custom')); |
| 43 | + $this->assertSame($link->get('custom'), 'http://example.org/custom'); |
| 44 | + $this->assertTrue($link->has('related')); |
| 45 | + $this->assertInstanceOf('Art4\JsonApiClient\LinkInterface', $link->get('related')); |
| 46 | + |
| 47 | + $this->assertSame($link->asArray(), array( |
| 48 | + 'self' => $link->get('self'), |
| 49 | + 'custom' => $link->get('custom'), |
| 50 | + 'related' => $link->get('related'), |
| 51 | + )); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @dataProvider jsonValuesProvider |
| 56 | + * |
| 57 | + * links: a links object related to the primary data. |
| 58 | + */ |
| 59 | + public function testCreateWithoutObjectThrowsException($input) |
| 60 | + { |
| 61 | + // Input must be an object |
| 62 | + if ( gettype($input) === 'object' ) |
| 63 | + { |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + $this->setExpectedException( |
| 68 | + 'Art4\JsonApiClient\Exception\ValidationException', |
| 69 | + 'ItemLink has to be an object, "' . gettype($input) . '" given.' |
| 70 | + ); |
| 71 | + |
| 72 | + $link = new ItemLink($input, $this->manager, $this->parent); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @test |
| 77 | + */ |
| 78 | + public function testGetOnANonExistingKeyThrowsException() |
| 79 | + { |
| 80 | + $object = new \stdClass(); |
| 81 | + $object->self = 'http://example.org/self'; |
| 82 | + |
| 83 | + $link = new ItemLink($object, $this->manager, $this->parent); |
| 84 | + |
| 85 | + $this->assertFalse($link->has('something')); |
| 86 | + |
| 87 | + $this->setExpectedException( |
| 88 | + 'Art4\JsonApiClient\Exception\AccessException', |
| 89 | + '"something" doesn\'t exist in this object.' |
| 90 | + ); |
| 91 | + |
| 92 | + $link->get('something'); |
| 93 | + } |
| 94 | +} |
0 commit comments