Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 13 | class ResponseTest extends TestCase |
||
| 14 | { |
||
| 15 | public function testDefaultConstructor() |
||
| 16 | { |
||
| 17 | $r = new Response(); |
||
| 18 | $this->assertSame(200, $r->getStatusCode()); |
||
| 19 | $this->assertSame('1.1', $r->getProtocolVersion()); |
||
| 20 | $this->assertSame('OK', $r->getReasonPhrase()); |
||
| 21 | $this->assertSame([], $r->getHeaders()); |
||
| 22 | $this->assertInstanceOf(StreamInterface::class, $r->getBody()); |
||
| 23 | $this->assertSame('', (string) $r->getBody()); |
||
| 24 | } |
||
| 25 | |||
| 26 | public function testCanConstructWithStatusCode() |
||
| 27 | { |
||
| 28 | $r = new Response(404); |
||
| 29 | $this->assertSame(404, $r->getStatusCode()); |
||
| 30 | $this->assertSame('Not Found', $r->getReasonPhrase()); |
||
| 31 | } |
||
| 32 | |||
| 33 | public function testConstructorDoesNotReadStreamBody() |
||
| 34 | { |
||
| 35 | $body = $this->getMockBuilder(StreamInterface::class)->getMock(); |
||
| 36 | $body->expects($this->never()) |
||
| 37 | ->method('__toString'); |
||
| 38 | |||
| 39 | $r = new Response(200, [], $body); |
||
| 40 | $this->assertSame($body, $r->getBody()); |
||
| 41 | } |
||
| 42 | |||
| 43 | public function testStatusCanBeNumericString() |
||
| 44 | { |
||
| 45 | $r = new Response('404'); |
||
| 46 | $r2 = $r->withStatus('201'); |
||
| 47 | $this->assertSame(404, $r->getStatusCode()); |
||
| 48 | $this->assertSame('Not Found', $r->getReasonPhrase()); |
||
| 49 | $this->assertSame(201, $r2->getStatusCode()); |
||
| 50 | $this->assertSame('Created', $r2->getReasonPhrase()); |
||
| 51 | } |
||
| 52 | |||
| 53 | public function testCanConstructWithHeaders() |
||
| 54 | { |
||
| 55 | $r = new Response(200, ['Foo' => 'Bar']); |
||
| 56 | $this->assertSame(['Foo' => ['Bar']], $r->getHeaders()); |
||
| 57 | $this->assertSame('Bar', $r->getHeaderLine('Foo')); |
||
| 58 | $this->assertSame(['Bar'], $r->getHeader('Foo')); |
||
| 59 | } |
||
| 60 | |||
| 61 | public function testCanConstructWithHeadersAsArray() |
||
| 62 | { |
||
| 63 | $r = new Response(200, [ |
||
| 64 | 'Foo' => ['baz', 'bar'], |
||
| 65 | ]); |
||
| 66 | $this->assertSame(['Foo' => ['baz', 'bar']], $r->getHeaders()); |
||
| 67 | $this->assertSame('baz, bar', $r->getHeaderLine('Foo')); |
||
| 68 | $this->assertSame(['baz', 'bar'], $r->getHeader('Foo')); |
||
| 69 | } |
||
| 70 | |||
| 71 | public function testCanConstructWithBody() |
||
| 72 | { |
||
| 73 | $r = new Response(200, [], 'baz'); |
||
| 74 | $this->assertInstanceOf(StreamInterface::class, $r->getBody()); |
||
| 75 | $this->assertSame('baz', (string) $r->getBody()); |
||
| 76 | } |
||
| 77 | |||
| 78 | public function testNullBody() |
||
| 79 | { |
||
| 80 | $r = new Response(200, [], null); |
||
| 81 | $this->assertInstanceOf(StreamInterface::class, $r->getBody()); |
||
| 82 | $this->assertSame('', (string) $r->getBody()); |
||
| 83 | } |
||
| 84 | |||
| 85 | public function testFalseyBody() |
||
| 86 | { |
||
| 87 | $r = new Response(200, [], '0'); |
||
| 88 | $this->assertInstanceOf(StreamInterface::class, $r->getBody()); |
||
| 89 | $this->assertSame('0', (string) $r->getBody()); |
||
| 90 | } |
||
| 91 | |||
| 92 | public function testCanConstructWithReason() |
||
| 93 | { |
||
| 94 | $r = new Response(200, [], null, '1.1', 'bar'); |
||
| 95 | $this->assertSame('bar', $r->getReasonPhrase()); |
||
| 96 | |||
| 97 | $r = new Response(200, [], null, '1.1', '0'); |
||
| 98 | $this->assertSame('0', $r->getReasonPhrase(), 'Falsey reason works'); |
||
| 99 | } |
||
| 100 | |||
| 101 | public function testCanConstructWithProtocolVersion() |
||
| 102 | { |
||
| 103 | $r = new Response(200, [], null, '1000'); |
||
| 104 | $this->assertSame('1000', $r->getProtocolVersion()); |
||
| 105 | } |
||
| 106 | |||
| 107 | public function testWithStatusCodeAndNoReason() |
||
| 108 | { |
||
| 109 | $r = (new Response())->withStatus(201); |
||
| 110 | $this->assertSame(201, $r->getStatusCode()); |
||
| 111 | $this->assertSame('Created', $r->getReasonPhrase()); |
||
| 112 | } |
||
| 113 | |||
| 114 | public function testWithStatusCodeAndReason() |
||
| 115 | { |
||
| 116 | $r = (new Response())->withStatus(201, 'Foo'); |
||
| 117 | $this->assertSame(201, $r->getStatusCode()); |
||
| 118 | $this->assertSame('Foo', $r->getReasonPhrase()); |
||
| 119 | |||
| 120 | $r = (new Response())->withStatus(201, '0'); |
||
| 121 | $this->assertSame(201, $r->getStatusCode()); |
||
| 122 | $this->assertSame('0', $r->getReasonPhrase(), 'Falsey reason works'); |
||
| 123 | } |
||
| 124 | |||
| 125 | public function testWithProtocolVersion() |
||
| 126 | { |
||
| 127 | $r = (new Response())->withProtocolVersion('1000'); |
||
| 128 | $this->assertSame('1000', $r->getProtocolVersion()); |
||
| 129 | } |
||
| 130 | |||
| 131 | public function testSameInstanceWhenSameProtocol() |
||
| 132 | { |
||
| 133 | $r = new Response(); |
||
| 134 | $this->assertSame($r, $r->withProtocolVersion('1.1')); |
||
| 135 | } |
||
| 136 | |||
| 137 | public function testWithBody() |
||
| 138 | { |
||
| 139 | $b = (new \Nyholm\Psr7\Factory\Psr17Factory())->createStream('0'); |
||
| 140 | $r = (new Response())->withBody($b); |
||
| 141 | $this->assertInstanceOf(StreamInterface::class, $r->getBody()); |
||
| 142 | $this->assertSame('0', (string) $r->getBody()); |
||
| 143 | } |
||
| 144 | |||
| 145 | public function testSameInstanceWhenSameBody() |
||
| 146 | { |
||
| 147 | $r = new Response(); |
||
| 148 | $b = $r->getBody(); |
||
| 149 | $this->assertSame($r, $r->withBody($b)); |
||
| 150 | } |
||
| 151 | |||
| 152 | public function testWithHeader() |
||
| 153 | { |
||
| 154 | $r = new Response(200, ['Foo' => 'Bar']); |
||
| 155 | $r2 = $r->withHeader('baZ', 'Bam'); |
||
| 156 | $this->assertSame(['Foo' => ['Bar']], $r->getHeaders()); |
||
| 157 | $this->assertSame(['Foo' => ['Bar'], 'baZ' => ['Bam']], $r2->getHeaders()); |
||
| 158 | $this->assertSame('Bam', $r2->getHeaderLine('baz')); |
||
| 159 | $this->assertSame(['Bam'], $r2->getHeader('baz')); |
||
| 160 | } |
||
| 161 | |||
| 162 | public function testWithHeaderAsArray() |
||
| 163 | { |
||
| 164 | $r = new Response(200, ['Foo' => 'Bar']); |
||
| 165 | $r2 = $r->withHeader('baZ', ['Bam', 'Bar']); |
||
| 166 | $this->assertSame(['Foo' => ['Bar']], $r->getHeaders()); |
||
| 167 | $this->assertSame(['Foo' => ['Bar'], 'baZ' => ['Bam', 'Bar']], $r2->getHeaders()); |
||
| 168 | $this->assertSame('Bam, Bar', $r2->getHeaderLine('baz')); |
||
| 169 | $this->assertSame(['Bam', 'Bar'], $r2->getHeader('baz')); |
||
| 170 | } |
||
| 171 | |||
| 172 | public function testWithHeaderReplacesDifferentCase() |
||
| 173 | { |
||
| 174 | $r = new Response(200, ['Foo' => 'Bar']); |
||
| 175 | $r2 = $r->withHeader('foO', 'Bam'); |
||
| 176 | $this->assertSame(['Foo' => ['Bar']], $r->getHeaders()); |
||
| 177 | $this->assertSame(['foO' => ['Bam']], $r2->getHeaders()); |
||
| 178 | $this->assertSame('Bam', $r2->getHeaderLine('foo')); |
||
| 179 | $this->assertSame(['Bam'], $r2->getHeader('foo')); |
||
| 180 | } |
||
| 181 | |||
| 182 | public function testWithAddedHeader() |
||
| 183 | { |
||
| 184 | $r = new Response(200, ['Foo' => 'Bar']); |
||
| 185 | $r2 = $r->withAddedHeader('foO', 'Baz'); |
||
| 186 | $this->assertSame(['Foo' => ['Bar']], $r->getHeaders()); |
||
| 187 | $this->assertSame(['Foo' => ['Bar', 'Baz']], $r2->getHeaders()); |
||
| 188 | $this->assertSame('Bar, Baz', $r2->getHeaderLine('foo')); |
||
| 189 | $this->assertSame(['Bar', 'Baz'], $r2->getHeader('foo')); |
||
| 190 | } |
||
| 191 | |||
| 192 | public function testWithAddedHeaderAsArray() |
||
| 193 | { |
||
| 194 | $r = new Response(200, ['Foo' => 'Bar']); |
||
| 195 | $r2 = $r->withAddedHeader('foO', ['Baz', 'Bam']); |
||
| 196 | $this->assertSame(['Foo' => ['Bar']], $r->getHeaders()); |
||
| 197 | $this->assertSame(['Foo' => ['Bar', 'Baz', 'Bam']], $r2->getHeaders()); |
||
| 198 | $this->assertSame('Bar, Baz, Bam', $r2->getHeaderLine('foo')); |
||
| 199 | $this->assertSame(['Bar', 'Baz', 'Bam'], $r2->getHeader('foo')); |
||
| 200 | } |
||
| 201 | |||
| 202 | public function testWithAddedHeaderThatDoesNotExist() |
||
| 203 | { |
||
| 204 | $r = new Response(200, ['Foo' => 'Bar']); |
||
| 205 | $r2 = $r->withAddedHeader('nEw', 'Baz'); |
||
| 206 | $this->assertSame(['Foo' => ['Bar']], $r->getHeaders()); |
||
| 207 | $this->assertSame(['Foo' => ['Bar'], 'nEw' => ['Baz']], $r2->getHeaders()); |
||
| 208 | $this->assertSame('Baz', $r2->getHeaderLine('new')); |
||
| 209 | $this->assertSame(['Baz'], $r2->getHeader('new')); |
||
| 210 | } |
||
| 211 | |||
| 212 | public function testWithoutHeaderThatExists() |
||
| 213 | { |
||
| 214 | $r = new Response(200, ['Foo' => 'Bar', 'Baz' => 'Bam']); |
||
| 215 | $r2 = $r->withoutHeader('foO'); |
||
| 216 | $this->assertTrue($r->hasHeader('foo')); |
||
| 217 | $this->assertSame(['Foo' => ['Bar'], 'Baz' => ['Bam']], $r->getHeaders()); |
||
| 218 | $this->assertFalse($r2->hasHeader('foo')); |
||
| 219 | $this->assertSame(['Baz' => ['Bam']], $r2->getHeaders()); |
||
| 220 | } |
||
| 221 | |||
| 222 | public function testWithoutHeaderThatDoesNotExist() |
||
| 223 | { |
||
| 224 | $r = new Response(200, ['Baz' => 'Bam']); |
||
| 225 | $r2 = $r->withoutHeader('foO'); |
||
| 226 | $this->assertSame($r, $r2); |
||
| 227 | $this->assertFalse($r2->hasHeader('foo')); |
||
| 228 | $this->assertSame(['Baz' => ['Bam']], $r2->getHeaders()); |
||
| 229 | } |
||
| 230 | |||
| 231 | public function testSameInstanceWhenRemovingMissingHeader() |
||
| 232 | { |
||
| 233 | $r = new Response(); |
||
| 234 | $this->assertSame($r, $r->withoutHeader('foo')); |
||
| 235 | } |
||
| 236 | |||
| 237 | public function trimmedHeaderValues() |
||
| 238 | { |
||
| 239 | return [ |
||
| 240 | [new Response(200, ['OWS' => " \t \tFoo\t \t "])], |
||
| 241 | [(new Response())->withHeader('OWS', " \t \tFoo\t \t ")], |
||
| 242 | [(new Response())->withAddedHeader('OWS', " \t \tFoo\t \t ")], |
||
| 243 | ]; |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @dataProvider trimmedHeaderValues |
||
| 248 | */ |
||
| 249 | public function testHeaderValuesAreTrimmed($r) |
||
| 250 | { |
||
| 251 | $this->assertSame(['OWS' => ['Foo']], $r->getHeaders()); |
||
| 252 | $this->assertSame('Foo', $r->getHeaderLine('OWS')); |
||
| 253 | $this->assertSame(['Foo'], $r->getHeader('OWS')); |
||
| 254 | } |
||
| 255 | } |
||
| 256 |