Failed Conditions
Push — issue#764 ( 39afc8 )
by Guilherme
11:43
created

testGetLatestVersionWithInvalidInput()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
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\Request;
16
use Symfony\Component\HttpFoundation\RequestStack;
17
18
class VersionServiceTest extends \PHPUnit_Framework_TestCase
19
{
20
    private function getSupportedVersions()
21
    {
22
        return [
23
            '1' => [
24
                '0' => ['0', '1', '2', '9'],
25
                '1' => ['0', '10'],
26
            ],
27
            '2' => [
28
                '0' => ['0', '1', '37'],
29
                '3' => ['20', '3', '0'],
30
            ],
31
            '3' => [
32
                '0' => ['0', '1', '5'],
33
            ],
34
        ];
35
    }
36
37
    private function getExpectedVersion($major, $minor, $patch)
38
    {
39
        return [
40
            'major' => $major,
41
            'minor' => $minor,
42
            'patch' => $patch,
43
        ];
44
    }
45
46
    /**
47
     * @param mixed $version
48
     * @return \PHPUnit_Framework_MockObject_MockObject|RequestStack
49
     */
50
    private function getRequestStack($version = null)
51
    {
52
        $request = $this->getMock('Symfony\Component\HttpFoundation\Request');
53
54
        /** @var \PHPUnit_Framework_MockObject_MockObject|ParameterBag $attributes */
55
        $attributes = $this->getMock('Symfony\Component\HttpFoundation\ParameterBag');
56
        $attributes->expects($this->any())
57
            ->method('get')->with('version')
58
            ->willReturn($version);
59
60
        if ($request instanceof Request) {
61
            $request->attributes = $attributes;
62
        }
63
64
        $requestStack = $this->getMock('Symfony\Component\HttpFoundation\RequestStack');
65
        $requestStack->expects($this->any())->method('getCurrentRequest')->willReturn($request);
66
67
        return $requestStack;
68
    }
69
70
    private function getVersionService($version = null)
71
    {
72
        return new VersionService($this->getRequestStack($version), $this->getSupportedVersions());
73
    }
74
75
    public function testGetLatestVersion()
76
    {
77
        $expected = $this->getExpectedVersion(3, 0, 5);
78
79
        /** @var RequestStack $requestStack */
80
        $requestStack = $this->getMock('Symfony\Component\HttpFoundation\RequestStack');
81
82
        $versionService = new VersionService($requestStack, $this->getSupportedVersions());
83
        $this->assertEquals($expected, $versionService->getLatestVersion());
84
    }
85
86
    public function testGetLatestVersionPartials()
87
    {
88
        /** @var RequestStack $requestStack */
89
        $requestStack = $this->getMock('Symfony\Component\HttpFoundation\RequestStack');
90
91
        $versionService = new VersionService($requestStack, $this->getSupportedVersions());
92
93
        $this->assertEquals(
94
            $this->getExpectedVersion(2, 3, 20),
95
            $versionService->getLatestVersion(2),
96
            'Expected 2.3.20'
97
        );
98
        $this->assertEquals(
99
            $this->getExpectedVersion(1, 1, 10),
100
            $versionService->getLatestVersion(1, 1),
101
            'Expected 1.1.10'
102
        );
103
    }
104
105
    public function testGetNonExistentVersion()
106
    {
107
        $this->setExpectedException('\InvalidArgumentException');
108
109
        $versionService = $this->getVersionService();
110
111
        $versionService->getLatestVersion(150);
112
    }
113
114
    public function testGetLatestVersionWithInvalidInput()
115
    {
116
        $this->setExpectedException('\InvalidArgumentException');
117
118
        $versionService = $this->getVersionService();
119
120
        $versionService->getLatestVersion(/** @scrutinizer ignore-type */'a');
121
    }
122
123
    public function testGetVersionFromRequest()
124
    {
125
        $versionService = $this->getVersionService('2');
126
        $this->assertEquals($this->getExpectedVersion(2, 3, 20), $versionService->getVersionFromRequest());
127
128
        $versionService = $this->getVersionService('2.0');
129
        $this->assertEquals($this->getExpectedVersion(2, 0, 37), $versionService->getVersionFromRequest());
130
131
        $versionService = $this->getVersionService('2.0.37');
132
        $this->assertEquals($this->getExpectedVersion(2, 0, 37), $versionService->getVersionFromRequest());
133
    }
134
135
    public function testGetString()
136
    {
137
        $versionService = $this->getVersionService();
138
139
        $this->assertEquals('1.0.0', $versionService->getString($this->getExpectedVersion(1, 0, 0)));
140
        $this->assertEquals('1.0', $versionService->getString([1, 0]));
141
        $this->assertEquals('4.9999999.8', $versionService->getString([4, 9999999, 8]));
142
    }
143
}
144