UpdateCheckerTest   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 27
c 3
b 0
f 0
dl 0
loc 49
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testCheckForUpdates() 0 21 1
A setUp() 0 17 1
1
<?php
2
3
namespace BringYourOwnIdeas\UpdateChecker\Tests;
4
5
use BringYourOwnIdeas\Maintenance\Util\ComposerLoader;
6
use BringYourOwnIdeas\UpdateChecker\UpdateChecker;
7
use Composer\Composer;
8
use PHPUnit_Framework_TestCase;
9
use SilverStripe\Core\Injector\Injector;
10
use SilverStripe\Dev\SapphireTest;
11
12
/**
13
 * @mixin PHPUnit_Framework_TestCase
14
 */
15
class UpdateCheckerTest extends SapphireTest
16
{
17
    protected $usesDatabase = true;
18
19
    /**
20
     * @var UpdateChecker
21
     */
22
    protected $updateChecker;
23
24
    protected function setUp()
25
    {
26
        parent::setUp();
27
28
        // Mock composer and composer loader
29
        $composer = $this->getMockBuilder(Composer::Class)->getMock();
0 ignored issues
show
Bug introduced by Guy
The constant Composer\Composer::Class was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
30
        $composerLoader = $this->getMockBuilder(ComposerLoader::class)
31
            ->disableOriginalConstructor()
32
            ->setMethods(['getComposer'])
33
            ->getMock();
34
        $composerLoader->expects($this->once())->method('getComposer')->will($this->returnValue($composer));
35
        Injector::inst()->registerService($composerLoader, ComposerLoader::class);
36
37
        // Partially mock UpdateChecker
38
        $this->updateChecker = $this->getMockBuilder(UpdateChecker::class)
0 ignored issues
show
Documentation Bug introduced by Robbie Averill
It seems like $this->getMockBuilder(Br...stPackage'))->getMock() of type PHPUnit_Framework_MockObject_MockObject is incompatible with the declared type BringYourOwnIdeas\UpdateChecker\UpdateChecker of property $updateChecker.

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...
39
            ->setMethods(['findLatestPackage'])
40
            ->getMock();
41
    }
42
43
    public function testCheckForUpdates()
44
    {
45
        $mockPackage = new \Composer\Package\Package('foo/bar', '2.3.4.0', '2.3.4');
46
        $mockPackage->setSourceReference('foobar123');
47
48
        // No available update
49
        $this->updateChecker->expects($this->at(0))
0 ignored issues
show
Bug introduced by Robbie Averill
The method expects() does not exist on BringYourOwnIdeas\UpdateChecker\UpdateChecker. ( Ignorable by Annotation )

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

49
        $this->updateChecker->/** @scrutinizer ignore-call */ 
50
                              expects($this->at(0))

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...
50
            ->method('findLatestPackage')
51
            ->will($this->returnValue(false));
52
53
        // There is latest version though
54
        $this->updateChecker->expects($this->at(1))
55
            ->method('findLatestPackage')
56
            ->will($this->returnValue($mockPackage));
57
58
59
        $result = $this->updateChecker->checkForUpdates($mockPackage, '~1.2.0');
60
        $this->assertArrayNotHasKey('AvailableVersion', $result, 'No available update is recorded');
61
        $this->assertArrayNotHasKey('AvailableHash', $result, 'No available update is recorded');
62
        $this->assertSame('2.3.4', $result['LatestVersion'], 'Latest version is returned');
63
        $this->assertSame('foobar123', $result['LatestHash'], 'Hash of latest version is returned');
64
    }
65
}
66