-
Notifications
You must be signed in to change notification settings - Fork 8
/
HasHttpAssertions.php
807 lines (721 loc) · 20.8 KB
/
HasHttpAssertions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
<?php
/**
* Copyright 2019 Cloud Creativity Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace CloudCreativity\JsonApi\Testing\Concerns;
use CloudCreativity\JsonApi\Testing\Compare;
use CloudCreativity\JsonApi\Testing\Document;
use CloudCreativity\JsonApi\Testing\HttpAssert;
use Illuminate\Contracts\Routing\UrlRoutable;
/**
* Trait HasHttpAssertions
*
* @package CloudCreativity\JsonApi\Testing
*/
trait HasHttpAssertions
{
/**
* @var Document|null
*/
protected $document;
/**
* @var string|null
*/
private $expectedType;
/**
* Get the JSON API document.
*
* @return Document
*/
public function getDocument(): Document
{
if (!$this->document) {
$this->document = HttpAssert::assertContent(
$this->getContentType(),
$this->getContent()
);
}
return $this->document;
}
/**
* @return string
*/
public function getExpectedType(): string
{
if (!$this->expectedType) {
throw new \LogicException('An expected resource type must be set.');
}
return $this->expectedType;
}
/**
* @param string $type
* @return $this
*/
public function willSeeResourceType(string $type): self
{
return $this->willSeeType($type);
}
/**
* Set the expected resource type for the data member of the JSON document.
*
* @param string $type
* @return $this
*/
public function willSeeType(string $type): self
{
if (empty($type)) {
throw new \InvalidArgumentException('Expected type must be a non-empty string.');
}
$this->expectedType = $type;
return $this;
}
/**
* @param $status
* @return $this
*/
public function assertStatusCode($status): self
{
HttpAssert::assertStatusCode($this->getStatusCode(), $status, $this->getContent());
return $this;
}
/**
* Assert that a resource was fetched.
*
* @param UrlRoutable|string|int|array $expected
* the expected resource, or a subset of the expected resource.
* @param bool $strict
* @return $this
*/
public function assertFetchedOne($expected, bool $strict = true): self
{
$this->document = HttpAssert::assertFetchedOne(
$this->getStatusCode(),
$this->getContentType(),
$this->getContent(),
$this->identifier($expected),
$strict
);
return $this;
}
/**
* Assert that an exact resource was fetched.
*
* @param UrlRoutable|string|int|array $expected
* the expected resource.
* @param bool $strict
* @return $this
*/
public function assertFetchedOneExact($expected, bool $strict = true): self
{
$this->document = HttpAssert::assertFetchedExact(
$this->getStatusCode(),
$this->getContentType(),
$this->getContent(),
$this->identifier($expected),
$strict
);
return $this;
}
/**
* Assert that the fetched data is null.
*
* @return $this
*/
public function assertFetchedNull(): self
{
$this->document = HttpAssert::assertFetchedNull(
$this->getStatusCode(),
$this->getContentType(),
$this->getContent()
);
return $this;
}
/**
* Assert that a resource collection was fetched.
*
* @param UrlRoutable|string|int|array $expected
* @param bool $strict
* @return $this
*/
public function assertFetchedMany($expected, bool $strict = true): self
{
$this->document = HttpAssert::assertFetchedMany(
$this->getStatusCode(),
$this->getContentType(),
$this->getContent(),
$this->identifiers($expected),
$strict
);
return $this;
}
/**
* Assert that an exact resource collection was fetched.
*
* @param UrlRoutable|string|int|array $expected
* @param bool $strict
* @return $this
*/
public function assertFetchedManyExact($expected, bool $strict = true): self
{
$this->document = HttpAssert::assertFetchedExact(
$this->getStatusCode(),
$this->getContentType(),
$this->getContent(),
$this->identifiers($expected),
$strict
);
return $this;
}
/**
* Assert that a resource collection was fetched in the expected order.
*
* @param UrlRoutable|string|int|array $expected
* @param bool $strict
* @return $this
*/
public function assertFetchedManyInOrder($expected, bool $strict = true): self
{
$this->document = HttpAssert::assertFetchedManyInOrder(
$this->getStatusCode(),
$this->getContentType(),
$this->getContent(),
$this->identifiers($expected),
$strict
);
return $this;
}
/**
* Assert that an empty resource collection was fetched.
*
* @return $this
*/
public function assertFetchedNone(): self
{
$this->document = HttpAssert::assertFetchedNone(
$this->getStatusCode(),
$this->getContentType(),
$this->getContent()
);
return $this;
}
/**
* Assert that a to-one relationship was fetched.
*
* If either type or id are null, then it will be asserted that the data member of the content
* is null.
*
* Prov
*
* @param UrlRoutable|string|int $id
* @return $this
*/
public function assertFetchedToOne($id): self
{
$identifier = $this->identifier($id);
$this->document = HttpAssert::assertFetchedToOne(
$this->getStatusCode(),
$this->getContentType(),
$this->getContent(),
$identifier['type'],
$identifier['id']
);
return $this;
}
/**
* Assert that a to-many relationship was fetched.
*
* @param UrlRoutable|string|int|array $expected
* @param bool $strict
* @return $this
*/
public function assertFetchedToMany($expected, bool $strict = true): self
{
$this->document = HttpAssert::assertFetchedToMany(
$this->getStatusCode(),
$this->getContentType(),
$this->getContent(),
$this->identifiers($expected),
$strict
);
return $this;
}
/**
* Assert that a to-many relationship was fetched in the expected order.
*
* @param UrlRoutable|string|int|array $expected
* @param bool $strict
* @return $this
*/
public function assertFetchedToManyInOrder($expected, bool $strict = true): self
{
$this->document = HttpAssert::assertFetchedToManyInOrder(
$this->getStatusCode(),
$this->getContentType(),
$this->getContent(),
$this->identifiers($expected),
$strict
);
return $this;
}
/**
* Assert that a resource was created with a server generated id.
*
* @param string $expectedLocation
* the expected location without the id.
* @param UrlRoutable|string|int|array $expected
* @param bool $strict
* @return $this
*/
public function assertCreatedWithServerId(string $expectedLocation, $expected, bool $strict = true): self
{
$this->document = HttpAssert::assertCreatedWithServerId(
$this->getStatusCode(),
$this->getContentType(),
$this->getContent(),
$this->getLocation(),
$expectedLocation,
$this->identifier($expected),
$strict
);
return $this;
}
/**
* Assert that a resource was created with a client generated id.
*
* @param string $expectedLocation
* @param UrlRoutable|string|int|array $expected
* @param bool $strict
* @return $this
*/
public function assertCreatedWithClientId(string $expectedLocation, $expected, bool $strict = true): self
{
$this->document = HttpAssert::assertCreatedWithClientId(
$this->getStatusCode(),
$this->getContentType(),
$this->getContent(),
$this->getLocation(),
$expectedLocation,
$this->identifier($expected),
$strict
);
return $this;
}
/**
* Assert that a resource was created with a no content response.
*
* @param $expectedLocation
* @return $this
*/
public function assertCreatedNoContent(string $expectedLocation): self
{
HttpAssert::assertCreatedNoContent(
$this->getStatusCode(),
$this->getLocation(),
$expectedLocation
);
return $this;
}
/**
* Assert response is a JSON API resource updated response.
*
* For a resource update, we typically expect either:
*
* - 200 OK with resource content; or
* - 204 No Content
*
* Alternatively a top-level meta only response is acceptable. If this is expected,
* it can be asserted using `assertMetaWithoutData`.
*
* @param array $expected
* array representation of the expected resource, or null for a no-content response
* @param bool $strict
* @return $this
*/
public function assertUpdated(array $expected = null, bool $strict = true): self
{
if (is_null($expected)) {
HttpAssert::assertNoContent($this->getStatusCode(), $this->getContent());
} else {
$this->assertFetchedOne($expected, $strict);
}
return $this;
}
/**
* Assert response is a JSON API resource deleted response.
*
* The JSON API spec says that:
*
* - A server MUST return a 204 No Content status code if a deletion request is successful
* and no content is returned.
* - A server MUST return a 200 OK status code if a deletion request is successful and the server responds
* with only top-level meta data.
*
* @param array|null $expected
* the expected top-level meta, or null for no content response.
* @param bool $strict
* @return $this
*/
public function assertDeleted(array $expected = null, bool $strict = true): self
{
if (is_null($expected)) {
HttpAssert::assertNoContent($this->getStatusCode(), $this->getContent());
} else {
$this->assertMetaWithoutData($expected, $strict);
}
return $this;
}
/**
* Assert that an asynchronous process was accepted with a server id.
*
* @param string $expectedLocation
* @param UrlRoutable|string|int|array $expected
* @param bool $strict
* @return $this
*/
public function assertAcceptedWithId(string $expectedLocation, $expected, bool $strict = true): self
{
$this->document = HttpAssert::assertAcceptedWithId(
$this->getStatusCode(),
$this->getContentType(),
$this->getContent(),
$this->getContentLocation(),
$expectedLocation,
$this->identifier($expected),
$strict
);
return $this;
}
/**
* Assert that the expected resource is included in the document.
*
* @param string $type
* @param UrlRoutable|string|int|null $id
* @return $this
*/
public function assertIsIncluded(string $type, $id): self
{
$identifier = $this->identifier(compact('type', 'id'));
$this->getDocument()->assertIncludedContainsResource(
$identifier['type'],
$identifier['id']
);
return $this;
}
/**
* Assert that the included member contains the supplied resource.
*
* @param UrlRoutable|string|int|array $expected
* @param bool $strict
* @return $this
*/
public function assertIncludes($expected, bool $strict = true): self
{
$this->getDocument()->assertIncludedContainsHash(
$this->identifier($expected),
$strict
);
return $this;
}
/**
* Assert that the document's included member matches the expected array.
*
* This does not assert the order of the included member because there is no significance to
* the order of resources in the included member.
*
* @param array $expected
* @param bool $strict
* @return $this
*/
public function assertIncluded(array $expected, bool $strict = true): self
{
$this->getDocument()->assertIncluded(
$this->identifiers($expected),
$strict
);
return $this;
}
/**
* Assert a top-level meta response without data.
*
* @param array $expected
* @param bool $strict
* @return $this
*/
public function assertMetaWithoutData(array $expected, bool $strict = true): self
{
$this->document = HttpAssert::assertMetaWithoutData(
$this->getStatusCode(),
$this->getContentType(),
$this->getContent(),
$expected,
$strict
);
return $this;
}
/**
* Assert an exact top-level meta response without data.
*
* @param array $expected
* @param bool $strict
* @return $this
*/
public function assertExactMetaWithoutData(array $expected, bool $strict = true): self
{
$this->document = HttpAssert::assertExactMetaWithoutData(
$this->getStatusCode(),
$this->getContentType(),
$this->getContent(),
$expected,
$strict
);
return $this;
}
/**
* Assert that the top-level meta matches the expected values.
*
* @param array $expected
* @param bool $strict
* @return $this
*/
public function assertMeta(array $expected, bool $strict = true): self
{
$this->getDocument()->assertMeta($expected, $strict);
return $this;
}
/**
* Assert that the top-level meta is exactly the expected meta.
*
* @param array $expected
* @param bool $strict
* @return $this
*/
public function assertExactMeta(array $expected, bool $strict = true): self
{
$this->getDocument()->assertExactMeta($expected, $strict);
return $this;
}
/**
* Assert that the top-level links match the expected values.
*
* @param array $expected
* @param bool $strict
* @return $this
*/
public function assertLinks(array $expected, bool $strict = true): self
{
$this->getDocument()->assertLinks($expected, $strict);
return $this;
}
/**
* Assert that the top-level links are exactly the expected links.
*
* @param array $expected
* @param bool $strict
* @return $this
*/
public function assertExactLinks(array $expected, bool $strict = true): self
{
$this->getDocument()->assertExactLinks($expected, $strict);
return $this;
}
/**
* Assert the document contains a single error that matches the supplied error.
*
* @param int $status
* @param array $error
* @param bool $strict
* @return $this
*/
public function assertError(int $status, array $error = [], bool $strict = true): self
{
$this->document = HttpAssert::assertError(
$this->getStatusCode(),
$this->getContentType(),
$this->getContent(),
$status,
$error,
$strict
);
return $this;
}
/**
* Assert the document contains a single error that exactly matches the supplied error.
*
* @param int $status
* @param array $error
* @param bool $strict
* @return $this
*/
public function assertExactError(int $status, array $error, bool $strict = true): self
{
$this->document = HttpAssert::assertExactError(
$this->getStatusCode(),
$this->getContentType(),
$this->getContent(),
$status,
$error,
$strict
);
return $this;
}
/**
* Assert the document contains a single error that matches the supplied error and has a status member.
*
* @param array $error
* @param bool $strict
* @return $this
*/
public function assertErrorStatus(array $error, bool $strict = true): self
{
$this->document = HttpAssert::assertErrorStatus(
$this->getStatusCode(),
$this->getContentType(),
$this->getContent(),
$error,
$strict
);
return $this;
}
/**
* Assert the document contains a single error that exactly matches the supplied error and has a status member.
*
* @param array $error
* @param bool $strict
* @return $this
*/
public function assertExactErrorStatus(array $error, bool $strict = true): self
{
$this->document = HttpAssert::assertExactErrorStatus(
$this->getStatusCode(),
$this->getContentType(),
$this->getContent(),
$error,
$strict
);
return $this;
}
/**
* Assert the HTTP message contains the supplied error within its errors member.
*
* @param int $status
* @param array $error
* @param bool $strict
* @return $this
*/
public function assertHasError(int $status, array $error = [], bool $strict = true): self
{
$this->document = HttpAssert::assertHasError(
$this->getStatusCode(),
$this->getContentType(),
$this->getContent(),
$status,
$error,
$strict
);
return $this;
}
/**
* Assert the HTTP message contains the exact supplied error within its errors member.
*
* @param int $status
* @param array $error
* @param bool $strict
* @return $this
*/
public function assertHasExactError(int $status, array $error, bool $strict = true): self
{
$this->document = HttpAssert::assertHasExactError(
$this->getStatusCode(),
$this->getContentType(),
$this->getContent(),
$status,
$error,
$strict
);
return $this;
}
/**
* Assert the HTTP status contains the supplied errors.
*
* @param int $status
* @param array $errors
* @param bool $strict
* @return $this
*/
public function assertErrors(int $status, array $errors, bool $strict = true): self
{
$this->document = HttpAssert::assertErrors(
$this->getStatusCode(),
$this->getContentType(),
$this->getContent(),
$status,
$errors,
$strict
);
return $this;
}
/**
* Assert the HTTP status contains the supplied exact errors.
*
* @param int $status
* @param array $errors
* @param bool $strict
* @return $this
*/
public function assertExactErrors(int $status, array $errors, bool $strict = true): self
{
$this->document = HttpAssert::assertExactErrors(
$this->getStatusCode(),
$this->getContentType(),
$this->getContent(),
$status,
$errors,
$strict
);
return $this;
}
/**
* Ensure the value is an array of identifiers.
*
* @param UrlRoutable|string|int|iterable $ids
* @param string|null $type
* @return array
*/
protected function identifiers($ids, string $type = null): array
{
return Compare::identifiers($ids, $type ?: $this->getExpectedType());
}
/**
* Ensure the value is a resource identifier.
*
* @param UrlRoutable|string|int|array $id
* @param string|null $type
* @return array
*/
protected function identifier($id, string $type = null): array
{
return Compare::identifier($id, $type ?: $this->getExpectedType());
}
/**
* Does the value identify a resource?
*
* @param $id
* @return bool
*/
protected function identifiable($id): bool
{
return Compare::identifiable($id);
}
}