CheckComposerUpdatesExtensionTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 66
rs 10
c 0
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testRunPassesPackagesToUpdateChecker() 0 8 1
A testOnlyAllowedPackageTypesAreProcessed() 0 10 1
A setUp() 0 18 1
A runTask() 0 5 1
1
<?php
2
3
namespace BringYourOwnIdeas\UpdateChecker\Tests\Extensions;
4
5
use BringYourOwnIdeas\Maintenance\Tasks\UpdatePackageInfoTask;
6
use BringYourOwnIdeas\UpdateChecker\Extensions\CheckComposerUpdatesExtension;
7
use BringYourOwnIdeas\UpdateChecker\Extensions\ComposerLoaderExtension;
8
use BringYourOwnIdeas\UpdateChecker\Tests\Stubs\ComposerLoaderExtensionStub;
9
use BringYourOwnIdeas\UpdateChecker\UpdateChecker;
10
use Composer\Package\PackageInterface;
11
use PHPUnit_Framework_TestCase;
12
use SilverStripe\Core\Config\Config;
13
use SilverStripe\Core\Injector\Injector;
14
use SilverStripe\Dev\SapphireTest;
15
16
/**
17
 * @mixin PHPUnit_Framework_TestCase
18
 */
19
class CheckComposerUpdatesExtensionTest extends SapphireTest
20
{
21
    protected $usesDatabase = true;
22
23
    /**
24
     * @var UpdatePackageInfoTask|CheckComposerUpdatesExtension
25
     */
26
    protected $task;
27
28
    /**
29
     * @var string[]
30
     */
31
    protected $allowedTypes;
32
33
    protected function setUp()
34
    {
35
        parent::setUp();
36
37
        // Register the extension stub for unit testing to avoid loading Composer
38
        Config::modify()->merge(Injector::class, ComposerLoaderExtension::class, [
39
            'class' => ComposerLoaderExtensionStub::class,
40
        ]);
41
42
        // Create the task for testing
43
        $this->task = UpdatePackageInfoTask::create();
44
45
        // Create a partial mock of the update checker
46
        $updateCheckerMock = $this->getMockBuilder(UpdateChecker::class)->setMethods(['checkForUpdates'])->getMock();
47
        $this->task->setUpdateChecker($updateCheckerMock);
48
49
        $this->allowedTypes = ['silverstripe-module', 'silverstripe-vendormodule', 'silverstripe-theme'];
50
        Config::modify()->set(UpdatePackageInfoTask::class, 'allowed_types', $this->allowedTypes);
51
    }
52
53
    public function testRunPassesPackagesToUpdateChecker()
54
    {
55
        $this->task->getUpdateChecker()->expects($this->atLeastOnce())
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

55
        $this->task->getUpdateChecker()->/** @scrutinizer ignore-call */ expects($this->atLeastOnce())

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...
56
            ->method('checkForUpdates')
57
            ->with($this->isInstanceOf(PackageInterface::class), $this->isType('string'))
58
            ->will($this->returnValue([]));
59
60
        $this->runTask();
61
    }
62
63
    public function testOnlyAllowedPackageTypesAreProcessed()
64
    {
65
        $this->task->getUpdateChecker()->expects($this->atLeastOnce())
66
            ->method('checkForUpdates')
67
            ->with($this->callback(function ($argument) {
68
                return in_array($argument->getType(), $this->allowedTypes);
69
            }))
70
            ->will($this->returnValue([]));
71
72
        $this->runTask();
73
    }
74
75
    /**
76
     * Runs the task and buffers the output (tasks output directly)
77
     *
78
     * @return string Task output
79
     */
80
    protected function runTask()
81
    {
82
        ob_start();
83
        $this->task->run(null);
0 ignored issues
show
Bug introduced by Robbie Averill
The method run() does not exist on BringYourOwnIdeas\Update...omposerUpdatesExtension. ( Ignorable by Annotation )

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

83
        $this->task->/** @scrutinizer ignore-call */ 
84
                     run(null);

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
        return ob_get_clean();
85
    }
86
}
87