55use Faker \Factory ;
66use Illuminate \Pagination \LengthAwarePaginator ;
77use Illuminate \Pagination \Paginator ;
8+ use Napp \Core \Api \Tests \Models \Category ;
89use Napp \Core \Api \Transformers \ApiTransformer ;
910use Napp \Core \Api \Tests \TestCase ;
1011
@@ -109,4 +110,40 @@ public function test_transform_length_aware_paginated_output_data()
109110 }
110111 }
111112
113+ public function test_transform_length_aware_paginated_with_relationships ()
114+ {
115+ $ category = Category::create (['title ' => 'Electronics ' ]);
116+ $ category ->products ()->create (['name ' => 'iPhone ' , 'price ' => 100.0 ]);
117+ $ category ->products ()->create (['name ' => 'Google Pixel ' , 'price ' => 80.0 ]);
118+ $ category ->products ()->create (['name ' => 'Samsung Galaxy 9 ' , 'price ' => 110.0 ]);
119+
120+ $ category2 = Category::create (['title ' => 'Computers ' ]);
121+ $ category2 ->products ()->create (['name ' => 'Mac ' , 'price ' => 28860.0 ]);
122+ $ category2 ->products ()->create (['name ' => 'Windows ' , 'price ' => 11000.0 ]);
123+
124+ $ input = Category::with ('products ' )->get ();
125+
126+ $ paginatedInput = new LengthAwarePaginator ($ input , count ($ input ) * 4 , count ($ input ));
127+
128+ $ transformedOutput = $ category ->getTransformer ()->transformOutput ($ paginatedInput );
129+
130+ $ this ->assertArrayHasKey ('current_page ' , $ transformedOutput );
131+ $ this ->assertArrayHasKey ('data ' , $ transformedOutput );
132+ $ this ->assertArrayHasKey ('first_page_url ' , $ transformedOutput );
133+ $ this ->assertArrayHasKey ('from ' , $ transformedOutput );
134+ $ this ->assertArrayHasKey ('last_page ' , $ transformedOutput );
135+ $ this ->assertArrayHasKey ('last_page_url ' , $ transformedOutput );
136+ $ this ->assertArrayHasKey ('next_page_url ' , $ transformedOutput );
137+ $ this ->assertArrayHasKey ('path ' , $ transformedOutput );
138+ $ this ->assertArrayHasKey ('per_page ' , $ transformedOutput );
139+ $ this ->assertArrayHasKey ('prev_page_url ' , $ transformedOutput );
140+ $ this ->assertArrayHasKey ('to ' , $ transformedOutput );
141+ $ this ->assertArrayHasKey ('total ' , $ transformedOutput );
142+
143+ $ this ->assertEquals ('iPhone ' , $ transformedOutput ['data ' ][0 ]['products ' ][0 ]['title ' ]);
144+ $ this ->assertEquals ('Google Pixel ' , $ transformedOutput ['data ' ][0 ]['products ' ][1 ]['title ' ]);
145+ $ this ->assertEquals ('Mac ' , $ transformedOutput ['data ' ][1 ]['products ' ][0 ]['title ' ]);
146+ $ this ->assertEquals ('Windows ' , $ transformedOutput ['data ' ][1 ]['products ' ][1 ]['title ' ]);
147+ }
148+
112149}
0 commit comments