Completed
Pull Request — master (#40)
by
unknown
13:57
created

CheckComposerUpdatesExtensionTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 62
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 13 1
A testRunPassesPackagesToUpdateChecker() 0 9 1
A testOnlyAllowedPackageTypesAreProcessed() 0 11 1
A runTask() 0 6 1
1
<?php
2
3
namespace BringYourOwnIdeas\UpdateChecker\Tests\Extensions;
4
5
use BringYourOwnIdeas\Maintenance\Util\ComposerLoader;
6
use BringYourOwnIdeas\UpdateChecker\Extensions\CheckComposerUpdatesExtension;
7
use BringYourOwnIdeas\UpdateChecker\UpdateChecker;
8
use Composer\Composer;
9
use Composer\Package\PackageInterface;
10
use Composer\Package\RootPackage;
11
use Config;
12
use Injector;
13
use Package;
14
use PHPUnit_Framework_TestCase;
15
use SapphireTest;
16
use UpdatePackageInfoTask;
17
18
/**
19
 * @mixin PHPUnit_Framework_TestCase
20
 */
21
class CheckComposerUpdatesExtensionTest extends SapphireTest
22
{
23
    protected $usesDatabase = true;
24
25
    /**
26
     * @var UpdatePackageInfoTask|CheckComposerUpdatesExtension
27
     */
28
    protected $task;
29
30
    /**
31
     * @var string[]
32
     */
33
    protected $allowedTypes;
34
35
    public function setUp()
36
    {
37
        parent::setUp();
38
39
        $this->task = UpdatePackageInfoTask::create();
40
41
        // Create a partial mock of the update checker
42
        $updateCheckerMock = $this->getMockBuilder(UpdateChecker::class)->setMethods(['checkForUpdates'])->getMock();
0 ignored issues
show
Bug introduced by
The method getMockBuilder() does not seem to exist on object<BringYourOwnIdeas...erUpdatesExtensionTest>.

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...
43
        $this->task->setUpdateChecker($updateCheckerMock);
44
45
        $this->allowedTypes = ['silverstripe-module', 'silverstripe-vendormodule', 'silverstripe-theme'];
46
        Config::inst()->update(UpdatePackageInfoTask::class, 'allowed_types', $this->allowedTypes);
47
    }
48
49
    public function testRunPassesPackagesToUpdateChecker()
50
    {
51
        $this->task->getUpdateChecker()->expects($this->atLeastOnce())
0 ignored issues
show
Bug introduced by
The method getUpdateChecker does only exist in BringYourOwnIdeas\Update...omposerUpdatesExtension, but not in UpdatePackageInfoTask.

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...
Bug introduced by
The method atLeastOnce() does not seem to exist on object<BringYourOwnIdeas...erUpdatesExtensionTest>.

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...
52
            ->method('checkForUpdates')
53
            ->with($this->isInstanceOf(PackageInterface::class), $this->isType('string'))
0 ignored issues
show
Bug introduced by
The method isInstanceOf() does not seem to exist on object<BringYourOwnIdeas...erUpdatesExtensionTest>.

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...
Bug introduced by
The method isType() does not seem to exist on object<BringYourOwnIdeas...erUpdatesExtensionTest>.

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...
54
            ->will($this->returnValue([]));
0 ignored issues
show
Bug introduced by
The method returnValue() does not seem to exist on object<BringYourOwnIdeas...erUpdatesExtensionTest>.

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
56
        $this->runTask();
57
    }
58
59
    public function testOnlyAllowedPackageTypesAreProcessed()
60
    {
61
        $this->task->getUpdateChecker()->expects($this->atLeastOnce())
0 ignored issues
show
Bug introduced by
The method getUpdateChecker does only exist in BringYourOwnIdeas\Update...omposerUpdatesExtension, but not in UpdatePackageInfoTask.

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...
Bug introduced by
The method atLeastOnce() does not seem to exist on object<BringYourOwnIdeas...erUpdatesExtensionTest>.

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...
62
            ->method('checkForUpdates')
63
            ->with($this->callback(function ($argument) {
0 ignored issues
show
Bug introduced by
The method callback() does not seem to exist on object<BringYourOwnIdeas...erUpdatesExtensionTest>.

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...
64
                return in_array($argument->getType(), $this->allowedTypes);
65
            }))
66
            ->will($this->returnValue([]));
0 ignored issues
show
Bug introduced by
The method returnValue() does not seem to exist on object<BringYourOwnIdeas...erUpdatesExtensionTest>.

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
68
        $this->runTask();
69
    }
70
71
    /**
72
     * Runs the task and buffers the output (tasks output directly)
73
     *
74
     * @return string Task output
75
     */
76
    protected function runTask()
77
    {
78
        ob_start();
79
        $this->task->run(null);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a object<SS_HTTPRequest>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Bug introduced by
The method run does only exist in UpdatePackageInfoTask, but not in BringYourOwnIdeas\Update...omposerUpdatesExtension.

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...
80
        return ob_get_clean();
81
    }
82
}
83