|
20 | 20 | namespace Art4\JsonApiClient\Utils\Tests; |
21 | 21 |
|
22 | 22 | use Art4\JsonApiClient\Utils\Helper; |
| 23 | +use Art4\JsonApiClient\Exception\ValidationException; |
23 | 24 |
|
24 | 25 | class HelperTest extends \Art4\JsonApiClient\Tests\Fixtures\TestCase |
25 | 26 | { |
| 27 | + /** |
| 28 | + * @test parseResponseBody() with valid JSON API returns Document Object |
| 29 | + */ |
| 30 | + public function testParseResponseBodyWithValidJsonapiReturnsDocument() |
| 31 | + { |
| 32 | + $jsonapi = '{"meta":{}}'; |
| 33 | + |
| 34 | + $this->assertInstanceOf('Art4\JsonApiClient\Document', Helper::parseResponseBody($jsonapi)); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * @test parseResponseBody throw Exception if input is invalid jsonapi |
| 39 | + */ |
| 40 | + public function testParseResponseBodyWithInvalidJsonapiThrowsException() |
| 41 | + { |
| 42 | + $invalid_jsonapi = '["This is valid JSON", "but invalid JSON API"]'; |
| 43 | + |
| 44 | + $this->expectException(ValidationException::class); |
| 45 | + |
| 46 | + $output = Helper::parseResponseBody($invalid_jsonapi); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * JSON API documents are defined in JavaScript Object Notation (JSON) [RFC4627]. |
| 51 | + */ |
| 52 | + public function testParseResponseBodyWithInvalidJsonThrowsException() |
| 53 | + { |
| 54 | + $invalid_json = 'invalid_json_string'; |
| 55 | + |
| 56 | + $this->expectException(ValidationException::class); |
| 57 | + |
| 58 | + $output = Helper::parseResponseBody($invalid_json); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @test isValidResponseBody() with valid JSON API returns true |
| 63 | + */ |
| 64 | + public function testIsValidResponseBodyWithValidJsonapi() |
| 65 | + { |
| 66 | + $jsonapi = '{"meta":{}}'; |
| 67 | + |
| 68 | + $this->assertTrue(Helper::isValidResponseBody($jsonapi)); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * @test isValidResponseBody() with invalid jsonapi |
| 73 | + */ |
| 74 | + public function testIsValidResponseBodyWithInvalidJsonapi() |
| 75 | + { |
| 76 | + $invalid_jsonapi = '["This is valid JSON", "but invalid JSON API"]'; |
| 77 | + |
| 78 | + $this->assertFalse(Helper::isValidResponseBody($invalid_jsonapi)); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * @test isValidResponseBody() with invalid json |
| 83 | + */ |
| 84 | + public function testIsValidResponseBodyWithInvalidJson() |
| 85 | + { |
| 86 | + $invalid_json = 'invalid_json_string'; |
| 87 | + |
| 88 | + $this->assertFalse(Helper::isValidResponseBody($invalid_json)); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * @test parseRequestBody() with valid JSON API returns Document Object |
| 93 | + */ |
| 94 | + public function testParseRequestBodyWithValidJsonapiReturnsDocument() |
| 95 | + { |
| 96 | + $jsonapi = '{"meta":{}}'; |
| 97 | + |
| 98 | + $this->assertInstanceOf('Art4\JsonApiClient\Document', Helper::parseRequestBody($jsonapi)); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * @test parseRequestBody() throw Exception if input is invalid jsonapi |
| 103 | + */ |
| 104 | + public function testParseRequestBodyWithInvalidJsonapiThrowsException() |
| 105 | + { |
| 106 | + $invalid_jsonapi = '["This is valid JSON", "but invalid JSON API"]'; |
| 107 | + |
| 108 | + $this->expectException(ValidationException::class); |
| 109 | + |
| 110 | + $output = Helper::parseRequestBody($invalid_jsonapi); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * JSON API documents are defined in JavaScript Object Notation (JSON) [RFC4627]. |
| 115 | + */ |
| 116 | + public function testParseRequestBodyWithInvalidJsonThrowsException() |
| 117 | + { |
| 118 | + $invalid_json = 'invalid_json_string'; |
| 119 | + |
| 120 | + $this->expectException(ValidationException::class); |
| 121 | + |
| 122 | + $output = Helper::parseRequestBody($invalid_json); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * @test isValidRequestBody() with valid JSON API returns true |
| 127 | + */ |
| 128 | + public function testIsValidRequestBodyWithValidJsonapi() |
| 129 | + { |
| 130 | + $jsonapi = '{"meta":{}}'; |
| 131 | + |
| 132 | + $this->assertTrue(Helper::isValidRequestBody($jsonapi)); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * @test isValidRequestBody() with invalid jsonapi |
| 137 | + */ |
| 138 | + public function testIsValidRequestBodyWithInvalidJsonapi() |
| 139 | + { |
| 140 | + $invalid_jsonapi = '["This is valid JSON", "but invalid JSON API"]'; |
| 141 | + |
| 142 | + $this->assertFalse(Helper::isValidRequestBody($invalid_jsonapi)); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * @test isValidRequestBody() with invalid json |
| 147 | + */ |
| 148 | + public function testIsValidRequestBodyWithInvalidJson() |
| 149 | + { |
| 150 | + $invalid_json = 'invalid_json_string'; |
| 151 | + |
| 152 | + $this->assertFalse(Helper::isValidRequestBody($invalid_json)); |
| 153 | + } |
| 154 | + |
| 155 | + // Tests for deprecated methods |
| 156 | + |
26 | 157 | /** |
27 | 158 | * @test parse() with valid JSON API returns Document Object |
28 | 159 | */ |
|
0 commit comments