12
12
use Illuminate \Support \Collection ;
13
13
use Neomerx \JsonApi \Contracts \Document \DocumentInterface as Keys ;
14
14
use Neomerx \JsonApi \Contracts \Http \Headers \MediaTypeInterface ;
15
- use PHPUnit_Framework_Assert as PHPUnit ;
15
+ use RuntimeException ;
16
16
17
17
class TestResponse extends BaseTestResponse
18
18
{
@@ -47,7 +47,7 @@ public function assertJsonApiResponse(
47
47
$ statusCode = Response::HTTP_OK ,
48
48
$ contentType = MediaTypeInterface::JSON_API_MEDIA_TYPE
49
49
) {
50
- $ this ->assertStatusCode ($ statusCode );
50
+ $ this ->assertStatus ($ statusCode );
51
51
52
52
if ($ contentType ) {
53
53
$ this ->assertHeader ('Content-Type ' , $ contentType );
@@ -213,7 +213,7 @@ public function assertDeleteResponse(
213
213
$ contentType = MediaTypeInterface::JSON_API_MEDIA_TYPE
214
214
) {
215
215
if (Response::HTTP_NO_CONTENT == $ statusCode ) {
216
- $ this ->assertStatusCode (Response::HTTP_NO_CONTENT );
216
+ $ this ->assertStatus (Response::HTTP_NO_CONTENT );
217
217
} else {
218
218
$ this ->assertJsonApiResponse ($ statusCode , $ contentType );
219
219
}
@@ -262,86 +262,37 @@ public function assertHasOneRelationshipResponse(
262
262
*
263
263
* @param $expected
264
264
* @return $this
265
+ * @deprecated use `assertStatus`
265
266
*/
266
267
public function assertStatusCode ($ expected )
267
268
{
268
- $ actual = $ this ->baseResponse ->getStatusCode ();
269
- $ message = "Expected status code {$ expected }, got {$ actual }" ;
270
- $ content = (array ) json_decode ((string ) $ this ->baseResponse ->getContent (), true );
271
-
272
- if (isset ($ content [Keys::KEYWORD_ERRORS ])) {
273
- $ message .= " with errors: \n" . json_encode ($ content , JSON_PRETTY_PRINT );
274
- }
275
-
276
- PHPUnit::assertEquals ($ expected , $ actual , $ message );
269
+ $ this ->assertStatus ($ expected );
277
270
278
271
return $ this ;
279
272
}
280
273
281
- /**
282
- * @param $expected
283
- * @return TestResponse
284
- * @deprecated use `assertStatusCode`
285
- */
286
- public function seeStatusCode ($ expected )
287
- {
288
- return $ this ->assertStatusCode ($ expected );
289
- }
290
-
291
274
/**
292
275
* See that there is a collection of resources as primary data.
293
276
*
294
277
* @param string|string[] $resourceType
295
278
* @param bool $allowEmpty
296
279
* @return $this
297
280
*/
298
- public function assertDataCollection ($ resourceType , $ allowEmpty = true )
281
+ public function assertDataCollection ($ resourceType = null , $ allowEmpty = true )
299
282
{
300
- $ this ->assertJsonStructure ([
301
- Keys::KEYWORD_DATA ,
302
- ]);
303
-
304
- $ collection = $ this ->decodeResponseJson ()[Keys::KEYWORD_DATA ];
283
+ $ resources = $ this ->assertDocument ()->assertResourceCollection ();
305
284
306
285
if (!$ allowEmpty ) {
307
- PHPUnit::assertNotEmpty ($ collection , 'Data collection is empty ' );
308
- } elseif (empty ($ collection )) {
309
- return $ this ;
286
+ $ resources ->assertNotEmpty ();
310
287
}
311
288
312
- $ expected = array_combine ((array ) $ resourceType , (array ) $ resourceType );
313
- $ actual = [];
314
-
315
- /** @var array $resource */
316
- foreach ($ collection as $ resource ) {
317
-
318
- if (!isset ($ resource [Keys::KEYWORD_TYPE ])) {
319
- PHPUnit::fail ('Encountered a resource without a type key. ' );
320
- }
321
-
322
- $ type = $ resource [Keys::KEYWORD_TYPE ];
323
-
324
- if (!isset ($ actual [$ type ])) {
325
- $ actual [$ type ] = $ type ;
326
- }
289
+ if (!$ resources ->isEmpty ()) {
290
+ $ resources ->assertTypes ($ resourceType ?: $ this ->expectedResourceType ());
327
291
}
328
292
329
- PHPUnit::assertEquals ($ expected , $ actual , 'Unexpected resource types in data collection. ' );
330
-
331
293
return $ this ;
332
294
}
333
295
334
- /**
335
- * @param $resourceType
336
- * @param bool $allowEmpty
337
- * @return TestResponse
338
- * @deprecated use `assertDataCollection`
339
- */
340
- public function seeDataCollection ($ resourceType , $ allowEmpty = true )
341
- {
342
- return $ this ->assertDataCollection ($ resourceType , $ allowEmpty );
343
- }
344
-
345
296
/**
346
297
* See that there is a resource object as primary data.
347
298
*
@@ -356,70 +307,11 @@ public function assertDataResource(array $expected)
356
307
$ expected [Keys::KEYWORD_TYPE ] = $ this ->expectedResourceType ();
357
308
}
358
309
359
- $ attributes = isset ($ expected [Keys::KEYWORD_ATTRIBUTES ]) ?
360
- $ expected [Keys::KEYWORD_ATTRIBUTES ] : [];
361
-
362
- $ relationships = isset ($ expected [Keys::KEYWORD_RELATIONSHIPS ]) ?
363
- $ this ->normalizeResourceRelationships ($ expected [Keys::KEYWORD_RELATIONSHIPS ]) : [];
364
-
365
- /** Check the structure is as expected. */
366
- $ structure = [
367
- Keys::KEYWORD_TYPE ,
368
- Keys::KEYWORD_ID ,
369
- ];
370
-
371
- if (!empty ($ attributes )) {
372
- $ structure [Keys::KEYWORD_ATTRIBUTES ] = array_keys ($ attributes );
373
- }
374
-
375
- if (!empty ($ relationships )) {
376
- $ structure [Keys::KEYWORD_RELATIONSHIPS ] = array_keys ($ relationships );
377
- }
378
-
379
- $ this ->assertJsonStructure ([
380
- Keys::KEYWORD_DATA => $ structure ,
381
- ]);
382
-
383
- $ data = $ this ->decodeResponseJson ()[Keys::KEYWORD_DATA ];
384
-
385
- /** Have we got the correct resource type? */
386
- PHPUnit::assertEquals ($ expected [Keys::KEYWORD_TYPE ], $ data [Keys::KEYWORD_TYPE ], 'Unexpected resource type ' );
387
-
388
- /** Have we got the correct resource id? */
389
- if (isset ($ expected [Keys::KEYWORD_ID ])) {
390
- PHPUnit::assertEquals ($ expected [Keys::KEYWORD_ID ], $ data [Keys::KEYWORD_ID ], 'Unexpected resource id ' );
391
- }
392
-
393
- /** Have we got the correct attributes? */
394
- PHPUnit::assertArraySubset (
395
- $ attributes ,
396
- $ data [Keys::KEYWORD_ATTRIBUTES ],
397
- false ,
398
- "Unexpected resource attributes \n" . json_encode ($ data [Keys::KEYWORD_ATTRIBUTES ])
399
- );
400
-
401
- /** Have we got the correct relationships? */
402
- $ actualRelationships = isset ($ data [Keys::KEYWORD_RELATIONSHIPS ]) ? $ data [Keys::KEYWORD_RELATIONSHIPS ] : [];
403
- PHPUnit::assertArraySubset (
404
- $ relationships ,
405
- $ actualRelationships ,
406
- false ,
407
- "Unexpected resource relationships \n" . json_encode ($ actualRelationships )
408
- );
310
+ $ this ->assertDocument ()->assertResource ()->assertMatches ($ expected );
409
311
410
312
return $ this ;
411
313
}
412
314
413
- /**
414
- * @param array $expected
415
- * @return TestResponse
416
- * @deprecated use `assertDataResource`
417
- */
418
- public function seeDataResource (array $ expected )
419
- {
420
- return $ this ->assertDataResource ($ expected );
421
- }
422
-
423
315
/**
424
316
* @param string|string[]|null $resourceType
425
317
* if null, will use the expected resource type.
@@ -429,41 +321,17 @@ public function seeDataResource(array $expected)
429
321
*/
430
322
public function assertDataResourceIdentifier ($ resourceType = null , $ id = null )
431
323
{
432
- if (is_null ($ resourceType )) {
433
- $ this ->expectedResourceType ();
434
- }
435
-
436
- $ this ->assertJsonStructure ([
437
- Keys::KEYWORD_DATA ,
438
- ]);
439
-
440
- $ data = (array ) $ this ->decodeResponseJson ()[Keys::KEYWORD_DATA ];
324
+ $ document = $ this ->assertDocument ();
441
325
442
326
if (is_null ($ id )) {
443
- PHPUnit::assertNull ($ data , 'Expecting data to be null (no identifier present). ' );
444
- return $ this ;
327
+ $ document ->assertDataNull ();
328
+ } else {
329
+ $ document ->assertResourceIdentifier ()->assertIs ($ resourceType ?: $ this ->expectedResourceType (), $ id );
445
330
}
446
331
447
- $ actualType = isset ($ data [Keys::KEYWORD_TYPE ]) ? $ data [Keys::KEYWORD_TYPE ] : null ;
448
- $ actualId = isset ($ data [Keys::KEYWORD_ID ]) ? $ data [Keys::KEYWORD_ID ] : null ;
449
-
450
- PHPUnit::assertContains ($ actualType , (array ) $ resourceType , 'Unexpected resource type in identifier. ' );
451
- PHPUnit::assertEquals ($ id , $ actualId , 'Unexpected resource id. ' );
452
-
453
332
return $ this ;
454
333
}
455
334
456
- /**
457
- * @param string|null $resourceType
458
- * @param string|null $id
459
- * @return $this
460
- * @deprecated use `assertDataResourceIdentifier`
461
- */
462
- public function seeDataResourceIdentifier ($ resourceType = null , $ id = null )
463
- {
464
- return $ this ->assertDataResourceIdentifier ($ resourceType , $ id );
465
- }
466
-
467
335
/**
468
336
* Assert that the response to a search by id(s) request contains the expected ids.
469
337
*
@@ -492,15 +360,6 @@ public function assertDocument()
492
360
return DocumentTester::create ($ this ->baseResponse ->getContent ());
493
361
}
494
362
495
- /**
496
- * @return DocumentTester
497
- * @deprecated use `assertDocument`
498
- */
499
- public function seeDocument ()
500
- {
501
- return $ this ->assertDocument ();
502
- }
503
-
504
363
/**
505
364
* @return ErrorsTester
506
365
*/
@@ -509,22 +368,13 @@ public function assertErrors()
509
368
return $ this ->assertDocument ()->assertErrors ();
510
369
}
511
370
512
- /**
513
- * @return ErrorsTester
514
- * @deprecated use `assertErrors`
515
- */
516
- public function seeErrors ()
517
- {
518
- return $ this ->assertErrors ();
519
- }
520
-
521
371
/**
522
372
* @return string
523
373
*/
524
374
protected function expectedResourceType ()
525
375
{
526
376
if (!$ this ->expectedResourceType ) {
527
- PHPUnit:: fail ( ' You must have provided the expected resource type to the test resposne helper. ' );
377
+ throw new RuntimeException ( ' No expected resource type set on the test response helper. ' );
528
378
}
529
379
530
380
return $ this ->expectedResourceType ;
@@ -547,24 +397,4 @@ protected function normalizeIds($ids)
547
397
})->all ();
548
398
}
549
399
550
- /**
551
- * @param array $relationships
552
- * @return array
553
- */
554
- private function normalizeResourceRelationships (array $ relationships )
555
- {
556
- $ normalized = [];
557
-
558
- foreach ($ relationships as $ key => $ value ) {
559
-
560
- if (is_numeric ($ key )) {
561
- $ key = $ value ;
562
- $ value = [];
563
- }
564
-
565
- $ normalized [$ key ] = $ value ;
566
- }
567
-
568
- return $ normalized ;
569
- }
570
400
}
0 commit comments