Completed
Push — master ( e04158...79cbbc )
by Jeroen
46:45 queued 33:57
created

VersionCheckTest::testCheck()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 62

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 62
rs 8.829
c 0
b 0
f 0
cc 3
nc 4
nop 3

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Kunstmaan\AdminBundle\Tests\Helper;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Handler\MockHandler;
7
use GuzzleHttp\HandlerStack;
8
use GuzzleHttp\Psr7\Response;
9
use Kunstmaan\AdminBundle\Helper\VersionCheck\Exception\ParseException;
10
use Kunstmaan\AdminBundle\Helper\VersionCheck\VersionChecker;
11
use PHPUnit\Framework\TestCase;
12
use Doctrine\Common\Cache\Cache;
13
use Symfony\Component\DependencyInjection\ContainerInterface;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpFoundation\RequestStack;
16
use Symfony\Component\HttpKernel\Kernel;
17
use Symfony\Component\Translation\Translator;
18
19
class VersionCheckTest extends TestCase
20
{
21
    /** @var ContainerInterface (mock) */
22
    private $container;
23
24
    /** @var Cache (mock) */
25
    private $cache;
26
27
    public function setUp()
28
    {
29
        /* @var ContainerInterface $container */
30
        $this->container = $this->createMock(ContainerInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Symfo...tainerInterface::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Symfony\Component...ion\ContainerInterface> of property $container.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
31
32
        /* @var Cache $cache */
33
        $this->cache = $this->createMock(Cache::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Doctr...mon\Cache\Cache::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Doctrine\Common\Cache\Cache> of property $cache.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
34
    }
35
36
    /**
37
     * @param array|null $methods
38
     *
39
     * @return \PHPUnit\Framework\MockObject\MockObject|VersionChecker
40
     */
41
    public function setUpVersionCheckerMock(?array $methods)
42
    {
43
        $versionCheckerMock = $this->getMockBuilder(VersionChecker::class)
44
            ->setConstructorArgs([$this->container, $this->cache])
45
            ->setMethods($methods)
46
            ->getMock()
47
        ;
48
49
        return $versionCheckerMock;
50
    }
51
52
    public function testIsEnabled()
53
    {
54
        $this->container
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Symfony\Component...ion\ContainerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
55
            ->expects($this->exactly(3))
56
            ->method('getParameter')
57
            ->will($this->onConsecutiveCalls('url', 300, true))
58
        ;
59
60
        $versionCheckerMock = $this->setUpVersionCheckerMock(null);
61
        $this->assertIsBool($versionCheckerMock->isEnabled());
0 ignored issues
show
Bug introduced by
The method isEnabled does only exist in Kunstmaan\AdminBundle\He...ionCheck\VersionChecker, but not in PHPUnit\Framework\MockObject\MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
62
    }
63
64
    public function testPeriodicallyCheck()
65
    {
66
        $this->container
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Symfony\Component...ion\ContainerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
67
            ->expects($this->exactly(3))
68
            ->method('getParameter')
69
            ->will($this->onConsecutiveCalls('url', 300, true))
70
        ;
71
72
        $this->cache
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Doctrine\Common\Cache\Cache>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
73
            ->expects($this->once())
74
            ->method('fetch')
75
            ->willReturn([])
76
        ;
77
        $versionCheckerMock = $this->setUpVersionCheckerMock(null);
78
        $versionCheckerMock->periodicallyCheck();
0 ignored issues
show
Bug introduced by
The method periodicallyCheck does only exist in Kunstmaan\AdminBundle\He...ionCheck\VersionChecker, but not in PHPUnit\Framework\MockObject\MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
79
    }
80
81
    public function testCheckWithInvalidResponse()
82
    {
83
        $this->container
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Symfony\Component...ion\ContainerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
84
            ->expects($this->exactly(4))
85
            ->method('getParameter')
86
            ->will($this->onConsecutiveCalls('url', 300, true, 'title'))
87
        ;
88
89
        $requestMock = $this->createMock(Request::class);
90
91
        $stackMock = $this->createMock(RequestStack::class);
92
        $stackMock
93
            ->expects($this->once())
94
            ->method('getCurrentRequest')
95
            ->willReturn($requestMock)
96
        ;
97
        $kernelMock = $this->createMock(Kernel::class);
98
99
        $this->container
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Symfony\Component...ion\ContainerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
100
            ->expects($this->exactly(2))
101
            ->method('get')
102
            ->will($this->onConsecutiveCalls($stackMock, $kernelMock))
103
        ;
104
105
        $versionCheckerMock = $this->setUpVersionCheckerMock(['parseComposer']);
106
        $versionCheckerMock
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit\Framework\MockObject\MockObject, but not in Kunstmaan\AdminBundle\He...ionCheck\VersionChecker.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
107
            ->expects($this->once())
108
            ->method('parseComposer')
109
            ->willReturn(['name' => 'box/spout'])
110
        ;
111
        $this->assertFalse($versionCheckerMock->check());
0 ignored issues
show
Bug introduced by
The method check does only exist in Kunstmaan\AdminBundle\He...ionCheck\VersionChecker, but not in PHPUnit\Framework\MockObject\MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
112
    }
113
114
    /**
115
     * @dataProvider provider
116
     */
117
    public function testCheck(string $lockPath, string $expectedType, string $expected)
118
    {
119
        if ('exception' === $expectedType) {
120
            $this->expectException(ParseException::class);
121
            $this->expectExceptionMessage($expected);
122
        }
123
124
        $this->container
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Symfony\Component...ion\ContainerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
125
            ->expects($this->any())
126
            ->method('getParameter')
127
            ->will($this->onConsecutiveCalls('url', 300, true, 'title'))
128
        ;
129
130
        $requestMock = $this->createMock(Request::class);
131
132
        $stackMock = $this->createMock(RequestStack::class);
133
        $stackMock
134
            ->expects($this->once())
135
            ->method('getCurrentRequest')
136
            ->willReturn($requestMock)
137
        ;
138
139
        $translatorMock = $this->createMock(Translator::class);
140
        $translatorMock
141
            ->expects($this->any())
142
            ->method('trans')
143
            ->willReturn('translated')
144
        ;
145
146
        $kernelMock = $this->createMock(Kernel::class);
147
148
        $this->container
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Symfony\Component...ion\ContainerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
149
            ->expects($this->exactly(3))
150
            ->method('get')
151
            ->will($this->onConsecutiveCalls($stackMock, $kernelMock, $translatorMock))
152
        ;
153
154
        $mock = new MockHandler([
155
            new Response(200, ['X-Foo' => 'Bar'], \json_encode(['foo' => 'bar'])),
156
        ]);
157
158
        $handler = HandlerStack::create($mock);
159
        $client = new Client(['handler' => $handler]);
160
161
        $versionCheckerMock = $this->setUpVersionCheckerMock(['getClient', 'getLockPath']);
162
        $versionCheckerMock
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit\Framework\MockObject\MockObject, but not in Kunstmaan\AdminBundle\He...ionCheck\VersionChecker.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
163
            ->expects($this->any())
164
            ->method('getClient')
165
            ->willReturn($client)
166
        ;
167
        $versionCheckerMock
168
            ->expects($this->once())
169
            ->method('getLockPath')
170
            ->willReturn($lockPath)
171
        ;
172
173
        if ('instanceOf' === $expectedType) {
174
            $this->assertInstanceOf($expected, $versionCheckerMock->check());
0 ignored issues
show
Bug introduced by
The method check does only exist in Kunstmaan\AdminBundle\He...ionCheck\VersionChecker, but not in PHPUnit\Framework\MockObject\MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
175
        } else {
176
            $versionCheckerMock->check();
177
        }
178
    }
179
180
    public function provider()
181
    {
182
        $baseDir = __DIR__ . '/testdata';
183
184
        return [
185
            'composer.lock ok' => [$baseDir.'/composer_ok.lock', 'instanceOf', \stdClass::class],
186
            'composer.lock broken' => [$baseDir.'/composer_broken.lock', 'exception', 'translated (#4)'],
187
            'composer.lock bundleless' => [$baseDir.'/composer_bundleless.lock', 'exception', 'translated'],
188
            'composer.lock not found' => [$baseDir.'/composer_not_there.lock', 'exception', 'translated'],
189
        ];
190
    }
191
}
192