Failed Conditions
Push — issue#751 ( b2d4ca...99e0c9 )
by Guilherme
08:29
created

VersionServiceTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 120
rs 10
c 1
b 0
f 1
wmc 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetNonExistentVersion() 0 7 1
A getVersionService() 0 3 1
A getRequestStack() 0 14 1
A getSupportedVersions() 0 13 1
A testGetLatestVersionPartials() 0 16 1
A testGetLatestVersionWithInvalidInput() 0 7 1
A testGetLatestVersion() 0 9 1
A testGetString() 0 7 1
A getExpectedVersion() 0 6 1
A testGetVersionFromRequest() 0 10 1
1
<?php
2
/**
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LoginCidadao\APIBundle\Tests\Service;
12
13
use LoginCidadao\APIBundle\Service\VersionService;
14
use Symfony\Component\HttpFoundation\ParameterBag;
15
use Symfony\Component\HttpFoundation\RequestStack;
16
17
class VersionServiceTest extends \PHPUnit_Framework_TestCase
18
{
19
    private function getSupportedVersions()
20
    {
21
        return [
22
            '1' => [
23
                '0' => ['0', '1', '2', '9'],
24
                '1' => ['0', '10'],
25
            ],
26
            '2' => [
27
                '0' => ['0', '1', '37'],
28
                '3' => ['20', '3', '0'],
29
            ],
30
            '3' => [
31
                '0' => ['0', '1', '5'],
32
            ],
33
        ];
34
    }
35
36
    private function getExpectedVersion($major, $minor, $patch)
37
    {
38
        return [
39
            'major' => $major,
40
            'minor' => $minor,
41
            'patch' => $patch,
42
        ];
43
    }
44
45
    /**
46
     * @param mixed $version
47
     * @return \PHPUnit_Framework_MockObject_MockObject|RequestStack
48
     */
49
    private function getRequestStack($version = null)
50
    {
51
        $request = $this->getMock('Symfony\Component\HttpFoundation\Request');
52
53
        $attributes = $this->getMock('Symfony\Component\HttpFoundation\ParameterBag');
54
        $attributes->expects($this->any())
55
            ->method('get')->with('version')
56
            ->willReturn($version);
57
        $request->attributes = $attributes;
0 ignored issues
show
Bug introduced by
Accessing attributes on the interface PHPUnit_Framework_MockObject_MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
58
59
        $requestStack = $this->getMock('Symfony\Component\HttpFoundation\RequestStack');
60
        $requestStack->expects($this->any())->method('getCurrentRequest')->willReturn($request);
61
62
        return $requestStack;
63
    }
64
65
    private function getVersionService($version = null)
66
    {
67
        return new VersionService($this->getRequestStack($version), $this->getSupportedVersions());
68
    }
69
70
    public function testGetLatestVersion()
71
    {
72
        $expected = $this->getExpectedVersion(3, 0, 5);
73
74
        /** @var RequestStack $requestStack */
75
        $requestStack = $this->getMock('Symfony\Component\HttpFoundation\RequestStack');
76
77
        $versionService = new VersionService($requestStack, $this->getSupportedVersions());
78
        $this->assertEquals($expected, $versionService->getLatestVersion());
79
    }
80
81
    public function testGetLatestVersionPartials()
82
    {
83
        /** @var RequestStack $requestStack */
84
        $requestStack = $this->getMock('Symfony\Component\HttpFoundation\RequestStack');
85
86
        $versionService = new VersionService($requestStack, $this->getSupportedVersions());
87
88
        $this->assertEquals(
89
            $this->getExpectedVersion(2, 3, 20),
90
            $versionService->getLatestVersion(2),
91
            'Expected 2.3.20'
92
        );
93
        $this->assertEquals(
94
            $this->getExpectedVersion(1, 1, 10),
95
            $versionService->getLatestVersion(1, 1),
96
            'Expected 1.1.10'
97
        );
98
    }
99
100
    public function testGetNonExistentVersion()
101
    {
102
        $this->setExpectedException('\InvalidArgumentException');
103
104
        $versionService = $this->getVersionService();
105
106
        $versionService->getLatestVersion(150);
107
    }
108
109
    public function testGetLatestVersionWithInvalidInput()
110
    {
111
        $this->setExpectedException('\InvalidArgumentException');
112
113
        $versionService = $this->getVersionService();
114
115
        $versionService->getLatestVersion('a');
0 ignored issues
show
Bug introduced by
'a' of type string is incompatible with the type null|integer expected by parameter $major of LoginCidadao\APIBundle\S...ice::getLatestVersion(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

115
        $versionService->getLatestVersion(/** @scrutinizer ignore-type */ 'a');
Loading history...
116
    }
117
118
    public function testGetVersionFromRequest()
119
    {
120
        $versionService = $this->getVersionService('2');
121
        $this->assertEquals($this->getExpectedVersion(2, 3, 20), $versionService->getVersionFromRequest());
122
123
        $versionService = $this->getVersionService('2.0');
124
        $this->assertEquals($this->getExpectedVersion(2, 0, 37), $versionService->getVersionFromRequest());
125
126
        $versionService = $this->getVersionService('2.0.37');
127
        $this->assertEquals($this->getExpectedVersion(2, 0, 37), $versionService->getVersionFromRequest());
128
    }
129
130
    public function testGetString()
131
    {
132
        $versionService = $this->getVersionService();
133
134
        $this->assertEquals('1.0.0', $versionService->getString($this->getExpectedVersion(1, 0, 0)));
135
        $this->assertEquals('1.0', $versionService->getString([1, 0]));
136
        $this->assertEquals('4.9999999.8', $versionService->getString([4, 9999999, 8]));
137
    }
138
}
139