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 JsonAuthenticationSuccessResponseTest extends TestCase |
||
| 14 | { |
||
| 15 | public function testSetUser() |
||
| 16 | { |
||
| 17 | $resp = new JsonAuthenticationSuccessResponse(); |
||
| 18 | $resp->setUser('test name'); |
||
| 19 | $data = $this->getData($resp); |
||
| 20 | $this->assertEquals(['serviceResponse' => ['authenticationSuccess' => ['user' => 'test name']]], $data); |
||
| 21 | $resp->setUser('test name2'); |
||
| 22 | $data = $this->getData($resp); |
||
| 23 | $this->assertEquals(['serviceResponse' => ['authenticationSuccess' => ['user' => 'test name2']]], $data); |
||
| 24 | } |
||
| 25 | |||
| 26 | public function testSetProxyGrantingTicket() |
||
| 27 | { |
||
| 28 | $resp = new JsonAuthenticationSuccessResponse(); |
||
| 29 | $resp->setProxyGrantingTicket('ticket1'); |
||
| 30 | $data = $this->getData($resp); |
||
| 31 | $this->assertEquals( |
||
| 32 | ['serviceResponse' => ['authenticationSuccess' => ['proxyGrantingTicket' => 'ticket1']]], |
||
| 33 | $data |
||
| 34 | ); |
||
| 35 | $resp->setProxyGrantingTicket('ticket2'); |
||
| 36 | $data = $this->getData($resp); |
||
| 37 | $this->assertEquals( |
||
| 38 | ['serviceResponse' => ['authenticationSuccess' => ['proxyGrantingTicket' => 'ticket2']]], |
||
| 39 | $data |
||
| 40 | ); |
||
| 41 | } |
||
| 42 | |||
| 43 | public function testSetProxies() |
||
| 44 | { |
||
| 45 | $resp = new JsonAuthenticationSuccessResponse(); |
||
| 46 | $proxies1 = ['http://proxy1.com', 'http://proxy2.com']; |
||
| 47 | $resp->setProxies($proxies1); |
||
| 48 | $data = $this->getData($resp); |
||
| 49 | $this->assertEquals(['serviceResponse' => ['authenticationSuccess' => ['proxies' => $proxies1]]], $data); |
||
| 50 | |||
| 51 | $proxies2 = ['http://proxy3.com', 'http://proxy4.com']; |
||
| 52 | $resp->setProxies($proxies2); |
||
| 53 | $data = $this->getData($resp); |
||
| 54 | $this->assertEquals(['serviceResponse' => ['authenticationSuccess' => ['proxies' => $proxies2]]], $data); |
||
| 55 | } |
||
| 56 | |||
| 57 | public function testSetAttributes() |
||
| 58 | { |
||
| 59 | $resp = new JsonAuthenticationSuccessResponse(); |
||
| 60 | $attr1 = ['key1' => 'value1', 'key2' => 'value2']; |
||
| 61 | $resp->setAttributes($attr1); |
||
| 62 | $data = $this->getData($resp); |
||
| 63 | $this->assertEquals(['serviceResponse' => ['authenticationSuccess' => ['attributes' => $attr1]]], $data); |
||
| 64 | |||
| 65 | $attr2 = ['key3' => 'value3', 'key4' => 'value4']; |
||
| 66 | $resp->setAttributes($attr2); |
||
| 67 | $data = $this->getData($resp); |
||
| 68 | $this->assertEquals(['serviceResponse' => ['authenticationSuccess' => ['attributes' => $attr2]]], $data); |
||
| 69 | } |
||
| 70 | |||
| 71 | protected function getData(JsonAuthenticationSuccessResponse $resp) |
||
| 72 | { |
||
| 73 | $property = self::getNonPublicProperty($resp, 'data'); |
||
| 74 | |||
| 75 | return $property->getValue($resp); |
||
| 76 | } |
||
| 77 | } |
||
| 78 |