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 SignatureParameterHandlerTest extends \PHPUnit_Framework_TestCase |
||
| 14 | { |
||
| 15 | use MockeryTrait; |
||
| 16 | |||
| 17 | const ID = 1; |
||
| 18 | const WIDTH = 400; |
||
| 19 | const HEIGHT = 300; |
||
| 20 | const SIGNATURE = 'f725b76a0982ac2a1c6d101f34a3917afe5b52d18fecb6b4abac82ee33b00bbc'; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @return SignatureParameterHandler |
||
| 24 | */ |
||
| 25 | private function getHandler() |
||
| 26 | { |
||
| 27 | return new SignatureParameterHandler('key', 'sha256'); |
||
| 28 | } |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @return MediaInterface |
||
| 32 | */ |
||
| 33 | private function getMediaMock() |
||
| 34 | { |
||
| 35 | $media = m::mock(MediaInterface::class); |
||
| 36 | $media->shouldReceive('getId')->andReturn(self::ID); |
||
| 37 | $media->shouldReceive('getFocalPoint')->andReturn('50-50'); |
||
| 38 | |||
| 39 | return $media; |
||
| 40 | } |
||
| 41 | |||
| 42 | public function testGetRouteParameters() |
||
| 43 | { |
||
| 44 | $this->assertEquals( |
||
| 45 | [ |
||
| 46 | 'id' => self::ID, |
||
| 47 | 'width' => self::WIDTH, |
||
| 48 | 'height' => self::HEIGHT, |
||
| 49 | 's' => self::SIGNATURE, |
||
| 50 | ], |
||
| 51 | $this->getHandler()->getRouteParameters( |
||
| 52 | $this->getMediaMock(), |
||
| 53 | new ImageParameterBag(self::WIDTH, self::HEIGHT) |
||
| 54 | ) |
||
| 55 | ); |
||
| 56 | } |
||
| 57 | |||
| 58 | public function testGetPayload() |
||
| 59 | { |
||
| 60 | $parameterBag = new ImageParameterBag(self::WIDTH, self::HEIGHT, [ |
||
| 61 | SignatureParameterHandler::PARAMETER_SIGNATURE => self::SIGNATURE, |
||
| 62 | 'id' => self::ID |
||
| 63 | ]); |
||
| 64 | |||
| 65 | $this->assertEquals($this->getHandler()->validateParameterBag($this->getMediaMock(), $parameterBag), $parameterBag); |
||
| 66 | } |
||
| 67 | |||
| 68 | public function testGetPayloadWithExtra() |
||
| 69 | { |
||
| 70 | $parameterBag = new ImageParameterBag(self::WIDTH, self::HEIGHT, [ |
||
| 71 | 'id' => self::ID, |
||
| 72 | SignatureParameterHandler::PARAMETER_SIGNATURE => 'b11d65fb09d95ee462ea945943708d69b794eb71cf08090bff858cbe5fe9c6a3', |
||
| 73 | 'foo' => 'bar' |
||
| 74 | ]); |
||
| 75 | |||
| 76 | $this->assertEquals($this->getHandler()->validateParameterBag($this->getMediaMock(), $parameterBag), $parameterBag); |
||
| 77 | } |
||
| 78 | |||
| 79 | public function testGetPayloadWithoutSignature() |
||
| 80 | { |
||
| 81 | $this->setExpectedException(SignatureInvalidException::class); |
||
| 82 | |||
| 83 | $media = $this->getMediaMock(); |
||
| 84 | $parameterBag = new ImageParameterBag(self::WIDTH, self::HEIGHT); |
||
| 85 | $this->getHandler()->validateParameterBag($media, $parameterBag); |
||
| 86 | } |
||
| 87 | |||
| 88 | public function testGetPayloadWithInvalidSignature() |
||
| 89 | { |
||
| 90 | $this->setExpectedException(SignatureInvalidException::class); |
||
| 91 | |||
| 92 | $parameterBag = new ImageParameterBag(self::WIDTH, self::HEIGHT); |
||
| 93 | $parameterBag->addExtra(SignatureParameterHandler::PARAMETER_SIGNATURE, 'foobar'); |
||
| 94 | |||
| 95 | $this->getHandler()->validateParameterBag($this->getMediaMock(), $parameterBag); |
||
| 96 | } |
||
| 97 | } |
||
| 98 |