Passed
Push — master ( f2e14b...2db21b )
by Robbie
04:12
created

ExtensionsCommandTest::headerProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace SilverLeague\Console\Tests\Command\Object;
4
5
use SilverLeague\Console\Tests\Command\AbstractCommandTest;
6
use SilverStripe\Assets\AssetControlExtension;
7
use SilverStripe\Forms\GridField\GridFieldDetailForm;
8
use SilverStripe\Versioned\VersionedGridFieldDetailForm;
9
10
/**
11
 * @coversDefaultClass \SilverLeague\Console\Command\Object\ExtensionsCommand
12
 * @package silverstripe-console
13
 * @author  Robbie Averill <[email protected]>
14
 */
15
class ExtensionsCommandTest extends AbstractCommandTest
16
{
17
    protected function getTestCommand()
18
    {
19
        return 'object:extensions';
20
    }
21
22
    /**
23
     * Ensure that the Injector's class resolution is returned for a given Object
24
     *
25
     * @covers ::execute
26
     */
27 View Code Duplication
    public function testExecute()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
28
    {
29
        $tester = $this->executeTest(['object'  => GridFieldDetailForm::class]);
30
        $output = $tester->getDisplay();
31
        $this->assertContains(VersionedGridFieldDetailForm::class, $output);
32
        $this->assertContains('silverstripe/versioned', $output);
33
    }
34
35
    /**
36
     * Ensure that the InputArgument for the object is added
37
     *
38
     * @covers ::configure
39
     */
40
    public function testConfigure()
41
    {
42
        $this->assertTrue($this->command->getDefinition()->hasArgument('object'));
43
    }
44
45
    /**
46
     * Ensure that extra headers are added for CMS pages
47
     *
48
     * @covers ::getRows
49
     * @dataProvider rowsProvider
50
     *
51
     * @param array    $extensions
52
     * @param string[] $expected
53
     */
54
    public function testGetRows($extensions, $expected)
55
    {
56
        $this->assertSame($expected, $this->command->getRows($extensions));
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class SilverLeague\Console\Command\SilverStripeCommand as the method getRows() does only exist in the following sub-classes of SilverLeague\Console\Command\SilverStripeCommand: SilverLeague\Console\Com...bject\ExtensionsCommand. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
57
    }
58
59
    /**
60
     * @return array[]
61
     */
62
    public function rowsProvider()
63
    {
64
        $extensions = [
65
            AssetControlExtension::class,
66
        ];
67
68
        return [
69
            [
70
                $extensions,
71
                [array_merge($extensions, ['silverstripe/assets', 0])],
72
            ],
73
        ];
74
    }
75
}
76