testRunPassesPackagesToUpdateChecker()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
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;
0 ignored issues
show
Bug introduced by
The type PHPUnit_Framework_TestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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(): void
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
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
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