|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Longman\LaravelLodash\Testing; |
| 6 | + |
| 7 | +use Arr; |
| 8 | +use Illuminate\Testing\Assert; |
| 9 | +use Illuminate\Testing\TestResponse; |
| 10 | +use PHPUnit\Framework\Assert as PHPUnit; |
| 11 | + |
| 12 | +use function __; |
| 13 | +use function json_decode; |
| 14 | + |
| 15 | +class Response extends TestResponse |
| 16 | +{ |
| 17 | + protected static array $successResponseStructure = []; |
| 18 | + protected static array $errorResponseStructure = []; |
| 19 | + protected static array $pagerMetaStructure = [ |
| 20 | + 'total', |
| 21 | + 'count', |
| 22 | + 'perPage', |
| 23 | + 'currentPage', |
| 24 | + 'totalPages', |
| 25 | + 'links', |
| 26 | + ]; |
| 27 | + |
| 28 | + public static function setSuccessResponseStructure(array $structure): void |
| 29 | + { |
| 30 | + self::$successResponseStructure = $structure; |
| 31 | + } |
| 32 | + |
| 33 | + public static function setErrorResponseStructure(array $structure): void |
| 34 | + { |
| 35 | + self::$errorResponseStructure = $structure; |
| 36 | + } |
| 37 | + |
| 38 | + public function assertJsonDataCount(int $count): self |
| 39 | + { |
| 40 | + $response = $this->getDecodedContent(); |
| 41 | + |
| 42 | + PHPUnit::assertCount($count, $response['data'] ?? []); |
| 43 | + |
| 44 | + return $this; |
| 45 | + } |
| 46 | + |
| 47 | + public function assertJsonDataPagination(array $data): self |
| 48 | + { |
| 49 | + $response = $this->getDecodedContent(); |
| 50 | + |
| 51 | + PHPUnit::assertEquals($data['page'], $response['meta']['pagination']['currentPage']); |
| 52 | + PHPUnit::assertEquals($data['perPage'], $response['meta']['pagination']['perPage']); |
| 53 | + PHPUnit::assertEquals($data['count'], $response['meta']['pagination']['count']); |
| 54 | + PHPUnit::assertEquals($data['total'], $response['meta']['pagination']['total']); |
| 55 | + |
| 56 | + return $this; |
| 57 | + } |
| 58 | + |
| 59 | + public function assertJsonDataCollectionStructure(array $data, bool $includePagerMeta = true): self |
| 60 | + { |
| 61 | + $struct = self::$successResponseStructure; |
| 62 | + $struct['data'] = [$data]; |
| 63 | + |
| 64 | + if ($includePagerMeta) { |
| 65 | + $struct['meta'] = [ |
| 66 | + 'pagination' => self::$pagerMetaStructure, |
| 67 | + ]; |
| 68 | + } |
| 69 | + |
| 70 | + $this->assertJsonStructure($struct); |
| 71 | + |
| 72 | + return $this; |
| 73 | + } |
| 74 | + |
| 75 | + public function assertJsonDataItemStructure(array $data): self |
| 76 | + { |
| 77 | + $struct = ['data' => $data]; |
| 78 | + |
| 79 | + $this->assertJsonStructure($struct); |
| 80 | + |
| 81 | + return $this; |
| 82 | + } |
| 83 | + |
| 84 | + public function assertJsonErrorStructure(): self |
| 85 | + { |
| 86 | + $this->assertJsonStructure(self::$errorResponseStructure); |
| 87 | + |
| 88 | + return $this; |
| 89 | + } |
| 90 | + |
| 91 | + public function assertJsonSuccessStructure(string $message = 'ok'): self |
| 92 | + { |
| 93 | + $this->assertJsonStructure(self::$successResponseStructure); |
| 94 | + $this->assertJson(['status' => $message]); |
| 95 | + |
| 96 | + return $this; |
| 97 | + } |
| 98 | + |
| 99 | + public function getDecodedContent(): array |
| 100 | + { |
| 101 | + $content = $this->getContent(); |
| 102 | + |
| 103 | + return json_decode($content, true); |
| 104 | + } |
| 105 | + |
| 106 | + public function assertForbidden(): Response |
| 107 | + { |
| 108 | + parent::assertForbidden(); |
| 109 | + |
| 110 | + //$this->assertJsonErrorStructure(); |
| 111 | + //$this->assertJson(['message' => 'This action is unauthorized.']); |
| 112 | + |
| 113 | + return $this; |
| 114 | + } |
| 115 | + |
| 116 | + public function assertNotFound(): Response |
| 117 | + { |
| 118 | + parent::assertNotFound(); |
| 119 | + |
| 120 | + //$this->assertJsonErrorStructure(); |
| 121 | + $this->assertJson(['status' => 'error', 'message' => __('app.item_not_found')]); |
| 122 | + |
| 123 | + return $this; |
| 124 | + } |
| 125 | + |
| 126 | + public function assertIsInvalidItem(): Response |
| 127 | + { |
| 128 | + Assert::assertTrue( |
| 129 | + $this->isInvalidData(), |
| 130 | + 'Response status code [' . $this->getStatusCode() . '] is not a invalid data status code.', |
| 131 | + ); |
| 132 | + |
| 133 | + return $this; |
| 134 | + } |
| 135 | + |
| 136 | + public function assertInvalidData(): Response |
| 137 | + { |
| 138 | + Assert::assertTrue( |
| 139 | + $this->isInvalidData(), |
| 140 | + 'Response status code [' . $this->getStatusCode() . '] is not a invalid data status code.', |
| 141 | + ); |
| 142 | + $this->assertJsonErrorStructure(); |
| 143 | + |
| 144 | + return $this; |
| 145 | + } |
| 146 | + |
| 147 | + public function isInvalidData(): bool |
| 148 | + { |
| 149 | + return $this->getStatusCode() === 422; |
| 150 | + } |
| 151 | + |
| 152 | + public function assertIsError(): void |
| 153 | + { |
| 154 | + $this->assertJsonStructure(self::$errorResponseStructure); |
| 155 | + $this->assertJson(['status' => 'error']); |
| 156 | + } |
| 157 | + |
| 158 | + public function assertIsOk(string $message = 'ok', bool $includeMeta = false): void |
| 159 | + { |
| 160 | + $structure = self::$successResponseStructure; |
| 161 | + if (! $includeMeta) { |
| 162 | + $structure = Arr::except($structure, 'meta'); |
| 163 | + } |
| 164 | + $this->assertJsonStructure($structure); |
| 165 | + $this->assertJson(['status' => $message]); |
| 166 | + } |
| 167 | + |
| 168 | + public function assertOk(): Response |
| 169 | + { |
| 170 | + parent::assertOk(); |
| 171 | + |
| 172 | + $this->assertJsonSuccessStructure(); |
| 173 | + |
| 174 | + return $this; |
| 175 | + } |
| 176 | + |
| 177 | + public function assertCreated(): Response |
| 178 | + { |
| 179 | + parent::assertCreated(); |
| 180 | + |
| 181 | + $this->assertJsonSuccessStructure(); |
| 182 | + |
| 183 | + return $this; |
| 184 | + } |
| 185 | +} |
0 commit comments