File tree 2 files changed +28
-1
lines changed
2 files changed +28
-1
lines changed Original file line number Diff line number Diff line change @@ -61,7 +61,7 @@ public function format($response)
61
61
62
62
$ apiDocument = $ response ->data ;
63
63
64
- if (!$ response ->isEmpty ) {
64
+ if (!$ response ->isEmpty && empty ( $ apiDocument ) ) {
65
65
$ apiDocument = ['data ' => $ response ->data ];
66
66
if (\Yii::$ app ->controller ) {
67
67
$ apiDocument ['links ' ] = Link::serialize ([
Original file line number Diff line number Diff line change @@ -70,6 +70,33 @@ public function testFormModelError()
70
70
]), $ response ->content );
71
71
}
72
72
73
+ public function testSuccessModel ()
74
+ {
75
+ $ formatter = new JsonApiResponseFormatter ();
76
+ $ response = new Response ();
77
+ $ serializer = new Serializer ();
78
+ $ model = new ResourceModel ();
79
+ $ response ->data = $ serializer ->serialize ($ model );
80
+ $ response ->setStatusCode (200 );
81
+ $ formatter ->format ($ response );
82
+ $ this ->assertJson ($ response ->content );
83
+ $ this ->assertSame (Json::encode ([
84
+ 'data ' => [
85
+ 'id ' => '123 ' ,
86
+ 'type ' => 'resource-models ' ,
87
+ 'attributes ' => [
88
+ 'field1 ' => 'test ' ,
89
+ 'field2 ' => 2 ,
90
+ ],
91
+ 'links ' => [
92
+ 'self ' => [
93
+ 'href ' => 'http://example.com/resource/123 '
94
+ ]
95
+ ]
96
+ ]
97
+ ]), $ response ->content );
98
+ }
99
+
73
100
public function testEmptyData ()
74
101
{
75
102
$ formatter = new JsonApiResponseFormatter ();
You can’t perform that action at this time.
0 commit comments