GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 17e380...c87318 )
by Bruno
06:52
created

YumlCommandTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 5
c 2
b 1
f 1
lcom 1
cbo 6
dl 0
loc 83
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B setUp() 0 45 4
A testExecute() 0 9 1
1
<?php
2
namespace OnurbTest\Bundle\YumlBundle\Command;
3
4
use Onurb\Bundle\YumlBundle\Command\YumlCommand;
5
use PHPUnit\Framework\TestCase;
6
use Symfony\Component\Console\Application;
7
use Symfony\Component\DependencyInjection\ContainerInterface;
8
use Symfony\Component\Console\Tester\CommandTester;
9
10
class YumlCommandTest extends TestCase
11
{
12
    const YUML_LINK = 'https://yuml.me/15a98c92.png';
13
14
    /**
15
     * @var Application
16
     */
17
    private $application;
18
19
    /**
20
     * @var YumlCommand
21
     */
22
    private $command;
23
24
    /**
25
     * @var ContainerInterface
26
     */
27
    private $container;
28
29
    /**
30
     * @var CommandTester
31
     */
32
    private $commandTester;
33
34
    public function setUp()
35
    {
36
        parent::setUp();
37
38
        $this->application = new Application();
39
        $this->application->add(new YumlCommand());
40
        $this->command = $this->application->find('yuml:mappings');
41
42
        $yumlClient = $this->createMock('Onurb\\Bundle\\YumlBundle\\Yuml\\YumlClientInterface');
43
44
        $yumlClient->expects($this->once())
45
            ->method('makeDslText')
46
            ->will($this->returnValue('[Simple.Entity|+a;b;c]'));
47
48
        $yumlClient->expects($this->once())
49
            ->method('getGraphUrl')
50
            ->will($this->returnValue(self::YUML_LINK));
51
52
        $this->container = $this->createMock('Symfony\\Component\\DependencyInjection\\ContainerInterface');
0 ignored issues
show
Documentation Bug introduced by HERON
It seems like $this->createMock('Symfo...n\\ContainerInterface') of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Symfony\Component...ion\ContainerInterface> of property $container.

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...
53
54
        $this->container->expects($this->once())->method('get')
55
            ->with($this->matches('onurb_yuml.client'))
56
                    ->will($this->returnValue($yumlClient));
57
58
        $this->container->expects($this->any())->method('getParameter')
59
            ->will(
60
                $this->returnCallback(
61
                    function ($arg) {
62
                        switch ($arg) {
63
                            case 'onurb_yuml.show_fields_description':
64
                                return false;
65
                            case 'onurb_yuml.colors':
66
                            case 'onurb_yuml.notes':
67
                                return array();
68
                            default:
69
                                return false;
70
                        }
71
                    }
72
                )
73
            );
74
75
        $this->command->setContainer($this->container);
0 ignored issues
show
Bug introduced by HERON
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Console\Command\Command as the method setContainer() does only exist in the following sub-classes of Symfony\Component\Console\Command\Command: Onurb\Bundle\YumlBundle\Command\YumlCommand, Symfony\Bundle\Framework...d\ContainerAwareCommand. 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...
76
77
        $this->commandTester = new CommandTester($this->command);
78
    }
79
80
    /**
81
     * @covers \Onurb\Bundle\YumlBundle\Command\YumlCommand
82
     */
83
    public function testExecute()
84
    {
85
        $this->commandTester->execute(array(
86
            'command'   => $this->command->getName()
87
        ));
88
89
        $this->assertRegExp('/.../', $this->commandTester->getDisplay());
90
        $this->assertSame('Downloaded', explode(' ', $this->commandTester->getDisplay())[0]);
91
    }
92
}
93