14
14
use Swis \JsonApi \Client \Link ;
15
15
use Swis \JsonApi \Client \Links ;
16
16
use Swis \JsonApi \Client \Meta ;
17
- use Swis \JsonApi \Client \Parsers \CollectionParser ;
18
17
use Swis \JsonApi \Client \Parsers \DocumentParser ;
19
- use Swis \JsonApi \Client \Parsers \ErrorCollectionParser ;
20
- use Swis \JsonApi \Client \Parsers \ErrorParser ;
21
- use Swis \JsonApi \Client \Parsers \ItemParser ;
22
- use Swis \JsonApi \Client \Parsers \JsonapiParser ;
23
- use Swis \JsonApi \Client \Parsers \LinksParser ;
24
- use Swis \JsonApi \Client \Parsers \MetaParser ;
25
18
use Swis \JsonApi \Client \Tests \AbstractTest ;
26
19
use Swis \JsonApi \Client \Tests \Mocks \Items \ChildItem ;
27
20
use Swis \JsonApi \Client \Tests \Mocks \Items \MasterItem ;
28
21
use Swis \JsonApi \Client \TypeMapper ;
29
22
30
23
class DocumentParserTest extends AbstractTest
31
24
{
25
+ /**
26
+ * @test
27
+ */
28
+ public function it_can_create_an_instance_using_a_factory_method ()
29
+ {
30
+ $ this ->assertInstanceOf (DocumentParser::class, DocumentParser::create ());
31
+ }
32
+
32
33
/**
33
34
* @test
34
35
*/
35
36
public function it_converts_jsondocument_to_document ()
36
37
{
37
- $ parser = $ this -> getDocumentParser ();
38
+ $ parser = DocumentParser:: create ();
38
39
$ document = $ parser ->parse (
39
40
json_encode (
40
41
[
@@ -51,7 +52,7 @@ public function it_converts_jsondocument_to_document()
51
52
*/
52
53
public function it_throws_when_json_is_not_valid ()
53
54
{
54
- $ parser = $ this -> getDocumentParser ();
55
+ $ parser = DocumentParser:: create ();
55
56
56
57
$ this ->expectException (ValidationException::class);
57
58
$ this ->expectExceptionMessage ('Unable to parse JSON data: Malformed UTF-8 characters, possibly incorrectly encoded ' );
@@ -67,7 +68,7 @@ public function it_throws_when_json_is_not_valid()
67
68
*/
68
69
public function it_throws_when_json_is_not_a_jsonapi_document (string $ invalidJson )
69
70
{
70
- $ parser = $ this -> getDocumentParser ();
71
+ $ parser = DocumentParser:: create ();
71
72
72
73
$ this ->expectException (ValidationException::class);
73
74
$ this ->expectExceptionMessage (sprintf ('Document MUST be an object, "%s" given. ' , gettype (json_decode ($ invalidJson , false ))));
@@ -92,7 +93,7 @@ public function provideInvalidJson(): array
92
93
*/
93
94
public function it_throws_when_data_errors_and_meta_are_missing ()
94
95
{
95
- $ parser = $ this -> getDocumentParser ();
96
+ $ parser = DocumentParser:: create ();
96
97
97
98
$ this ->expectException (ValidationException::class);
98
99
$ this ->expectExceptionMessage ('Document MUST contain at least one of the following properties: `data`, `errors`, `meta`. ' );
@@ -105,7 +106,7 @@ public function it_throws_when_data_errors_and_meta_are_missing()
105
106
*/
106
107
public function it_throws_when_both_data_and_errors_are_present ()
107
108
{
108
- $ parser = $ this -> getDocumentParser ();
109
+ $ parser = DocumentParser:: create ();
109
110
110
111
$ this ->expectException (ValidationException::class);
111
112
$ this ->expectExceptionMessage ('The properties `data` and `errors` MUST NOT coexist in Document. ' );
@@ -118,7 +119,7 @@ public function it_throws_when_both_data_and_errors_are_present()
118
119
*/
119
120
public function it_throws_when_included_is_present_but_data_is_not ()
120
121
{
121
- $ parser = $ this -> getDocumentParser ();
122
+ $ parser = DocumentParser:: create ();
122
123
123
124
$ this ->expectException (ValidationException::class);
124
125
$ this ->expectExceptionMessage ('If Document does not contain a `data` property, the `included` property MUST NOT be present either. ' );
@@ -134,7 +135,7 @@ public function it_throws_when_included_is_present_but_data_is_not()
134
135
*/
135
136
public function it_throws_when_data_is_not_an_array_object_or_null ($ invalidData )
136
137
{
137
- $ parser = $ this -> getDocumentParser ();
138
+ $ parser = DocumentParser:: create ();
138
139
139
140
$ this ->expectException (ValidationException::class);
140
141
$ this ->expectExceptionMessage (sprintf ('Document property "data" MUST be null, an array or an object, "%s" given. ' , gettype (json_decode ($ invalidData , false )->data )));
@@ -160,7 +161,7 @@ public function provideInvalidData(): array
160
161
*/
161
162
public function it_throws_when_included_is_not_an_array ($ invalidIncluded )
162
163
{
163
- $ parser = $ this -> getDocumentParser ();
164
+ $ parser = DocumentParser:: create ();
164
165
165
166
$ this ->expectException (ValidationException::class);
166
167
$ this ->expectExceptionMessage (sprintf ('Document property "included" MUST be an array, "%s" given. ' , gettype (json_decode ($ invalidIncluded , false )->included )));
@@ -185,7 +186,7 @@ public function provideInvalidIncluded(): array
185
186
*/
186
187
public function it_throws_when_it_finds_duplicate_resources ()
187
188
{
188
- $ parser = $ this -> getDocumentParser ();
189
+ $ parser = DocumentParser:: create ();
189
190
190
191
$ this ->expectException (ValidationException::class);
191
192
$ this ->expectExceptionMessage ('Resources MUST be unique based on their `type` and `id`, 1 duplicate(s) found. ' );
@@ -221,7 +222,7 @@ public function it_throws_when_it_finds_duplicate_resources()
221
222
*/
222
223
public function it_parses_a_resource_document ()
223
224
{
224
- $ parser = $ this -> getDocumentParser ();
225
+ $ parser = DocumentParser:: create ();
225
226
$ document = $ parser ->parse (
226
227
json_encode (
227
228
[
@@ -247,7 +248,7 @@ public function it_parses_a_resource_document()
247
248
*/
248
249
public function it_parses_a_resource_collection_document ()
249
250
{
250
- $ parser = $ this -> getDocumentParser ();
251
+ $ parser = DocumentParser:: create ();
251
252
$ document = $ parser ->parse (
252
253
json_encode (
253
254
[
@@ -276,7 +277,7 @@ public function it_parses_a_resource_collection_document()
276
277
*/
277
278
public function it_parses_a_document_without_data ()
278
279
{
279
- $ parser = $ this -> getDocumentParser ();
280
+ $ parser = DocumentParser:: create ();
280
281
$ document = $ parser ->parse (
281
282
json_encode (
282
283
[
@@ -295,7 +296,7 @@ public function it_parses_a_document_without_data()
295
296
*/
296
297
public function it_parses_included ()
297
298
{
298
- $ parser = $ this -> getDocumentParser ();
299
+ $ parser = DocumentParser:: create ();
299
300
$ document = $ parser ->parse (
300
301
json_encode (
301
302
[
@@ -328,7 +329,7 @@ public function it_links_singular_relations_to_items_from_included()
328
329
$ typeMapper = new TypeMapper ();
329
330
$ typeMapper ->setMapping ('master ' , MasterItem::class);
330
331
$ typeMapper ->setMapping ('child ' , ChildItem::class);
331
- $ parser = $ this -> getDocumentParser ($ typeMapper );
332
+ $ parser = DocumentParser:: create ($ typeMapper );
332
333
333
334
$ document = $ parser ->parse (
334
335
json_encode (
@@ -374,7 +375,7 @@ public function it_does_not_link_empty_singular_relations()
374
375
$ typeMapper = new TypeMapper ();
375
376
$ typeMapper ->setMapping ('master ' , MasterItem::class);
376
377
$ typeMapper ->setMapping ('child ' , ChildItem::class);
377
- $ parser = $ this -> getDocumentParser ($ typeMapper );
378
+ $ parser = DocumentParser:: create ($ typeMapper );
378
379
379
380
$ document = $ parser ->parse (
380
381
json_encode (
@@ -417,7 +418,7 @@ public function it_links_plural_relations_to_items_from_included()
417
418
$ typeMapper = new TypeMapper ();
418
419
$ typeMapper ->setMapping ('master ' , MasterItem::class);
419
420
$ typeMapper ->setMapping ('child ' , ChildItem::class);
420
- $ parser = $ this -> getDocumentParser ($ typeMapper );
421
+ $ parser = DocumentParser:: create ($ typeMapper );
421
422
422
423
$ document = $ parser ->parse (
423
424
json_encode (
@@ -478,7 +479,7 @@ public function it_does_not_link_empty_plural_relations()
478
479
$ typeMapper = new TypeMapper ();
479
480
$ typeMapper ->setMapping ('master ' , MasterItem::class);
480
481
$ typeMapper ->setMapping ('child ' , ChildItem::class);
481
- $ parser = $ this -> getDocumentParser ($ typeMapper );
482
+ $ parser = DocumentParser:: create ($ typeMapper );
482
483
483
484
$ document = $ parser ->parse (
484
485
json_encode (
@@ -527,7 +528,7 @@ public function it_does_not_link_empty_plural_relations()
527
528
*/
528
529
public function it_parses_links ()
529
530
{
530
- $ parser = $ this -> getDocumentParser ();
531
+ $ parser = DocumentParser:: create ();
531
532
532
533
$ document = $ parser ->parse (
533
534
json_encode (
@@ -550,7 +551,7 @@ public function it_parses_links()
550
551
*/
551
552
public function it_parses_errors ()
552
553
{
553
- $ parser = $ this -> getDocumentParser ();
554
+ $ parser = DocumentParser:: create ();
554
555
555
556
$ document = $ parser ->parse (
556
557
json_encode (
@@ -575,7 +576,7 @@ public function it_parses_errors()
575
576
*/
576
577
public function it_parses_meta ()
577
578
{
578
- $ parser = $ this -> getDocumentParser ();
579
+ $ parser = DocumentParser:: create ();
579
580
580
581
$ document = $ parser ->parse (
581
582
json_encode (
@@ -598,7 +599,7 @@ public function it_parses_meta()
598
599
*/
599
600
public function it_parses_jsonapi ()
600
601
{
601
- $ parser = $ this -> getDocumentParser ();
602
+ $ parser = DocumentParser:: create ();
602
603
603
604
$ document = $ parser ->parse (
604
605
json_encode (
@@ -615,20 +616,4 @@ public function it_parses_jsonapi()
615
616
616
617
static ::assertEquals (new Jsonapi ('1.0 ' ), $ document ->getJsonapi ());
617
618
}
618
-
619
- private function getDocumentParser (TypeMapper $ typeMapper = null ): DocumentParser
620
- {
621
- $ metaParser = new MetaParser ();
622
- $ linksParser = new LinksParser ($ metaParser );
623
- $ itemParser = new ItemParser ($ typeMapper ?? new TypeMapper (), $ linksParser , $ metaParser );
624
-
625
- return new DocumentParser (
626
- $ itemParser ,
627
- new CollectionParser ($ itemParser ),
628
- new ErrorCollectionParser (new ErrorParser ($ linksParser , $ metaParser )),
629
- $ linksParser ,
630
- new JsonapiParser ($ metaParser ),
631
- $ metaParser
632
- );
633
- }
634
619
}
0 commit comments