FindDirectoryCommandTest::getMockContainer()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 1
eloc 9
nc 1
nop 0
1
<?php
2
/**
3
 * Class FindDirectoryCommandTest
4
 *
5
 * @author Mauro Moreno <[email protected]>
6
 */
7
namespace MauroMoreno\FindBundle\Tests\Command;
8
9
use MauroMoreno\FindBundle\Command\FindDirectoryCommand;
10
use MauroMoreno\FindBundle\Service\FindDirectoryService;
11
use MauroMoreno\FindBundle\Service\Finder;
12
use MauroMoreno\FindBundle\Service\Lister;
13
use Symfony\Component\Console\Application;
14
use Symfony\Component\Console\Tester\CommandTester;
15
16
/**
17
 * Class FindDirectoryCommandTest
18
 * @package MauroMoreno\FindBundle\Tests\Command
19
 */
20
class FindDirectoryCommandTest extends \PHPUnit_Framework_TestCase
21
{
22
23
    /**
24
     * {@inheritdoc}
25
     */
26
    protected function setUp()
27
    {
28
        $application = new Application();
29
        $application->add(new FindDirectoryCommand());
30
31
        $this->command = $application->find('find:dir');
0 ignored issues
show
Bug introduced by
The property command does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
32
        $this->command->setContainer($this->getMockContainer());
0 ignored issues
show
Bug introduced by
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: MauroMoreno\FindBundle\C...nd\FindDirectoryCommand, Symfony\Bundle\Framework...d\AbstractConfigCommand, Symfony\Bundle\Framework...nd\AssetsInstallCommand, Symfony\Bundle\Framework...mmand\CacheClearCommand, Symfony\Bundle\Framework...mand\CacheWarmupCommand, Symfony\Bundle\Framework...mand\ConfigDebugCommand, Symfony\Bundle\Framework...figDumpReferenceCommand, Symfony\Bundle\Framework...d\ContainerAwareCommand, Symfony\Bundle\Framework...d\ContainerDebugCommand, Symfony\Bundle\Framework...tDispatcherDebugCommand, Symfony\Bundle\Framework...mand\RouterDebugCommand, Symfony\Bundle\Framework...mand\RouterMatchCommand, Symfony\Bundle\Framework...e\Command\ServerCommand, Symfony\Bundle\Framework...ommand\ServerRunCommand, Symfony\Bundle\Framework...mand\ServerStartCommand, Symfony\Bundle\Framework...and\ServerStatusCommand, Symfony\Bundle\Framework...mmand\ServerStopCommand, Symfony\Bundle\Framework...TranslationDebugCommand, Symfony\Bundle\Framework...ranslationUpdateCommand, Symfony\Bundle\SecurityB...\Command\InitAclCommand, Symfony\Bundle\SecurityB...e\Command\SetAclCommand, Symfony\Bundle\SecurityB...rPasswordEncoderCommand, Symfony\Bundle\TwigBundle\Command\DebugCommand, Symfony\Bundle\TwigBundle\Command\LintCommand. 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...
33
        $this->commandTester = new CommandTester($this->command);
0 ignored issues
show
Bug introduced by
The property commandTester does not seem to exist. Did you mean command?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
34
        mkdir(__DIR__ . '/empty_directory');
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    protected function tearDown()
41
    {
42
        rmdir(__DIR__ . '/empty_directory');
43
        parent::tearDown();
44
    }
45
46
    /**
47
     * Test Command execute, empty directory
48
     */
49 View Code Duplication
    public function testExecuteEmptyDirectory()
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...
50
    {
51
        $this->commandTester->execute([
0 ignored issues
show
Bug introduced by
The property commandTester does not seem to exist. Did you mean command?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
52
            'command' => $this->command->getName(),
53
            'pattern' => 'pattern',
54
            'directory' => __DIR__ . '/empty_directory'
55
        ]);
56
57
        $this->assertEquals(
58
            "No results where found.\n",
59
            $this->commandTester->getDisplay()
0 ignored issues
show
Bug introduced by
The property commandTester does not seem to exist. Did you mean command?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
60
        );
61
    }
62
63
    /**
64
     * Test Command execute
65
     */
66 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...
67
    {
68
        $this->commandTester->execute([
0 ignored issues
show
Bug introduced by
The property commandTester does not seem to exist. Did you mean command?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
69
            'command' => $this->command->getName(),
70
            'pattern' => 'pattern',
71
            'directory' => __DIR__ . '/../Fixtures/directory'
72
        ]);
73
        $this->assertRegExp(
74
            '/file_1|file_3/',
75
            $this->commandTester->getDisplay()
0 ignored issues
show
Bug introduced by
The property commandTester does not seem to exist. Did you mean command?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
76
        );
77
    }
78
79
    /**
80
     * Test Command execute, Extension set
81
     */
82 View Code Duplication
    public function testExecuteExtensionSet()
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...
83
    {
84
        $this->commandTester->execute([
0 ignored issues
show
Bug introduced by
The property commandTester does not seem to exist. Did you mean command?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
85
            'command' => $this->command->getName(),
86
            'pattern' => 'pattern',
87
            'directory' => __DIR__ . '/../Fixtures/directory',
88
            '--extension' => 'txt'
89
        ]);
90
        $this->assertEquals("file_3.txt\n", $this->commandTester->getDisplay());
0 ignored issues
show
Bug introduced by
The property commandTester does not seem to exist. Did you mean command?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
91
    }
92
93
    /**
94
     * Get Mock Container
95
     * @return \PHPUnit_Framework_MockObject_MockObject
96
     */
97
    protected function getMockContainer()
98
    {
99
        $mockContainer = $this->getMock(
100
            'Symfony\Component\DependencyInjection\Container'
101
        );
102
        $mockContainer
103
            ->expects($this->once())
104
            ->method('get')
105
            ->with('mauro_moreno_find.find_directory_service')
106
            ->willReturn(new FindDirectoryService(new Finder, new Lister));
107
        return $mockContainer;
108
    }
109
110
}