|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace SkoreLabs\JsonApi\Testing; |
| 4 | + |
| 5 | +use Illuminate\Contracts\Support\Arrayable; |
| 6 | +use Illuminate\Support\Traits\Macroable; |
| 7 | +use PHPUnit\Framework\Assert as PHPUnit; |
| 8 | +use PHPUnit\Framework\AssertionFailedError; |
| 9 | +use SkoreLabs\JsonApi\Testing\Concerns\HasRelationships; |
| 10 | +use SkoreLabs\JsonApi\Testing\Concerns\HasAttributes; |
| 11 | +use SkoreLabs\JsonApi\Testing\Concerns\HasCollections; |
| 12 | +use SkoreLabs\JsonApi\Testing\Concerns\HasIdentifications; |
| 13 | + |
| 14 | +class Assert implements Arrayable |
| 15 | +{ |
| 16 | + use HasIdentifications, |
| 17 | + HasAttributes, |
| 18 | + HasRelationships, |
| 19 | + HasCollections, |
| 20 | + Macroable; |
| 21 | + |
| 22 | + /** |
| 23 | + * @var string |
| 24 | + */ |
| 25 | + protected $id; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var string |
| 29 | + */ |
| 30 | + protected $type; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var array |
| 34 | + */ |
| 35 | + protected $attributes; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var array |
| 39 | + */ |
| 40 | + protected $relationships; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var array |
| 44 | + */ |
| 45 | + protected $includeds; |
| 46 | + |
| 47 | + /** |
| 48 | + * @var array |
| 49 | + */ |
| 50 | + protected $collection; |
| 51 | + |
| 52 | + protected function __construct($id = '', $type = '', array $attributes = [], array $relationships = [], array $includeds = [], array $collection = []) |
| 53 | + { |
| 54 | + $this->id = $id; |
| 55 | + $this->type = $type; |
| 56 | + |
| 57 | + $this->attributes = $attributes; |
| 58 | + $this->relationships = $relationships; |
| 59 | + $this->includeds = $includeds; |
| 60 | + |
| 61 | + $this->collection = $collection; |
| 62 | + } |
| 63 | + |
| 64 | + public static function fromTestResponse($response) |
| 65 | + { |
| 66 | + try { |
| 67 | + $content = json_decode($response->getContent(), true); |
| 68 | + $data = $content['data']; |
| 69 | + $collection = []; |
| 70 | + |
| 71 | + if (static::isCollection($data)) { |
| 72 | + $collection = $data; |
| 73 | + $data = head($data); |
| 74 | + } |
| 75 | + |
| 76 | + PHPUnit::assertIsArray($data); |
| 77 | + PHPUnit::assertArrayHasKey('id', $data); |
| 78 | + PHPUnit::assertArrayHasKey('type', $data); |
| 79 | + PHPUnit::assertArrayHasKey('attributes', $data); |
| 80 | + PHPUnit::assertIsArray($data['attributes']); |
| 81 | + } catch (AssertionFailedError $e) { |
| 82 | + PHPUnit::fail('Not a valid JSON:API response.'); |
| 83 | + } |
| 84 | + |
| 85 | + return new self($data['id'], $data['type'], $data['attributes'], $data['relationships'] ?? [], $content['included'] ?? [], $collection); |
| 86 | + } |
| 87 | + |
| 88 | + public function toArray(): array |
| 89 | + { |
| 90 | + return $this->attributes; |
| 91 | + } |
| 92 | + |
| 93 | + public static function isCollection(array $data = []) |
| 94 | + { |
| 95 | + return !array_key_exists('attributes', $data); |
| 96 | + } |
| 97 | +} |
0 commit comments