|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\HttpClient\Tests\Response; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Component\HttpClient\Response\MockResponse; |
| 16 | + |
| 17 | +class ResponseStatusCodeTraitTest extends TestCase |
| 18 | +{ |
| 19 | + public function testIsOk() |
| 20 | + { |
| 21 | + $response = self::createResponse(200); |
| 22 | + $this->assertTrue($response->isOk()); |
| 23 | + } |
| 24 | + |
| 25 | + public function testIsCreated() |
| 26 | + { |
| 27 | + $response = self::createResponse(201); |
| 28 | + $this->assertTrue($response->isCreated()); |
| 29 | + } |
| 30 | + |
| 31 | + public function testIsAccepted() |
| 32 | + { |
| 33 | + $response = self::createResponse(202); |
| 34 | + $this->assertTrue($response->isAccepted()); |
| 35 | + } |
| 36 | + |
| 37 | + public function testIsNoContent() |
| 38 | + { |
| 39 | + $response = self::createEmptyContentResponse(); |
| 40 | + $this->assertTrue($response->isNoContent()); |
| 41 | + |
| 42 | + $response = self::createEmptyContentResponse(204, json_encode(['foo' => 'bar', 'bar' => 'baz'])); |
| 43 | + $this->assertFalse($response->isNoContent()); |
| 44 | + |
| 45 | + $fakeStatusCode = 999; |
| 46 | + $response = self::createEmptyContentResponse($fakeStatusCode); |
| 47 | + $this->assertTrue($response->isNoContent($fakeStatusCode)); |
| 48 | + |
| 49 | + $response = self::createEmptyContentResponse($fakeStatusCode); |
| 50 | + // we don't pass any status code, so the default status code (204) is used |
| 51 | + $this->assertFalse($response->isNoContent()); |
| 52 | + } |
| 53 | + |
| 54 | + public function testIsMovedPermanently() |
| 55 | + { |
| 56 | + $response = self::createResponse(301); |
| 57 | + $this->assertTrue($response->isMovedPermanently()); |
| 58 | + } |
| 59 | + |
| 60 | + public function testIsFound() |
| 61 | + { |
| 62 | + $response = self::createResponse(302); |
| 63 | + $this->assertTrue($response->isFound()); |
| 64 | + } |
| 65 | + |
| 66 | + public function testIsNotModified() |
| 67 | + { |
| 68 | + $response = self::createResponse(304); |
| 69 | + $this->assertTrue($response->isNotModified()); |
| 70 | + } |
| 71 | + |
| 72 | + public function testIsBadRequest() |
| 73 | + { |
| 74 | + $response = self::createResponse(400); |
| 75 | + $this->assertTrue($response->isBadRequest()); |
| 76 | + } |
| 77 | + |
| 78 | + public function testIsUnauthorized() |
| 79 | + { |
| 80 | + $response = self::createResponse(401); |
| 81 | + $this->assertTrue($response->isUnauthorized()); |
| 82 | + } |
| 83 | + |
| 84 | + public function testIsPaymentRequired() |
| 85 | + { |
| 86 | + $response = self::createResponse(402); |
| 87 | + $this->assertTrue($response->isPaymentRequired()); |
| 88 | + } |
| 89 | + |
| 90 | + public function testIsForbidden() |
| 91 | + { |
| 92 | + $response = self::createResponse(403); |
| 93 | + $this->assertTrue($response->isForbidden()); |
| 94 | + } |
| 95 | + |
| 96 | + public function testIsNotFound() |
| 97 | + { |
| 98 | + $response = self::createResponse(404); |
| 99 | + $this->assertTrue($response->isNotFound()); |
| 100 | + } |
| 101 | + |
| 102 | + public function testIsMethodNotAllowed() |
| 103 | + { |
| 104 | + $response = self::createResponse(405); |
| 105 | + $this->assertTrue($response->isMethodNotAllowed()); |
| 106 | + } |
| 107 | + |
| 108 | + public function testIsNotAcceptable() |
| 109 | + { |
| 110 | + $response = self::createResponse(406); |
| 111 | + $this->assertTrue($response->isNotAcceptable()); |
| 112 | + } |
| 113 | + |
| 114 | + public function testIsRequestTimeout() |
| 115 | + { |
| 116 | + $response = self::createResponse(408); |
| 117 | + $this->assertTrue($response->isRequestTimeout()); |
| 118 | + } |
| 119 | + |
| 120 | + public function testIsConflict() |
| 121 | + { |
| 122 | + $response = self::createResponse(409); |
| 123 | + $this->assertTrue($response->isConflict()); |
| 124 | + } |
| 125 | + |
| 126 | + public function testIsGone() |
| 127 | + { |
| 128 | + $response = self::createResponse(410); |
| 129 | + $this->assertTrue($response->isGone()); |
| 130 | + } |
| 131 | + |
| 132 | + public function testIsUnprocessableEntity() |
| 133 | + { |
| 134 | + $response = self::createResponse(422); |
| 135 | + $this->assertTrue($response->isUnprocessableEntity()); |
| 136 | + } |
| 137 | + |
| 138 | + public function testIsTooManyRequests() |
| 139 | + { |
| 140 | + $response = self::createResponse(429); |
| 141 | + $this->assertTrue($response->isTooManyRequests()); |
| 142 | + } |
| 143 | + |
| 144 | + private static function createResponse(int $statusCode, array $content = ['foo' => 'bar']): MockResponse |
| 145 | + { |
| 146 | + $responseMock = new MockResponse(json_encode($content), [ |
| 147 | + 'http_code' => $statusCode, |
| 148 | + ]); |
| 149 | + |
| 150 | + $response = MockResponse::fromRequest('GET', 'https://example.com/some-endpoint', [], $responseMock); |
| 151 | + $response->toArray(false); |
| 152 | + |
| 153 | + return $response; |
| 154 | + } |
| 155 | + |
| 156 | + private static function createEmptyContentResponse(int $statusCode = 204, string $content = ''): MockResponse |
| 157 | + { |
| 158 | + $responseMock = new MockResponse($content, [ |
| 159 | + 'http_code' => $statusCode, |
| 160 | + ]); |
| 161 | + |
| 162 | + $response = MockResponse::fromRequest('GET', 'https://example.com/some-endpoint', [], $responseMock); |
| 163 | + $response->getContent(false); |
| 164 | + |
| 165 | + return $response; |
| 166 | + } |
| 167 | +} |
0 commit comments