Completed
Push — master ( 8e1573...db6321 )
by
unknown
16:08 queued 08:39
created

SignatureParameterHandlerTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 85
Duplicated Lines 20 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 7
dl 17
loc 85
rs 10
c 0
b 0
f 0

How to fix   Duplicated Code   

Duplicated Code

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
2
3
namespace MediaMonks\SonataMediaBundle\Tests\Unit\Handler;
4
5
use MediaMonks\SonataMediaBundle\Exception\InvalidQueryParameterException;
6
use MediaMonks\SonataMediaBundle\Exception\SignatureInvalidException;
7
use MediaMonks\SonataMediaBundle\ParameterBag\ImageParameterBag;
8
use MediaMonks\SonataMediaBundle\Handler\SignatureParameterHandler;
9
use MediaMonks\SonataMediaBundle\Model\MediaInterface;
10
use MediaMonks\SonataMediaBundle\Tests\Unit\MockeryTrait;
11
use Mockery as m;
12
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