Completed
Push — master ( 413676...7d6334 )
by Demonchaux
03:57
created

CommandBuilderTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 70
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testOk() 0 15 1
A testOkWithDefinitionSetter() 0 15 1
A testOkWithOption() 0 15 1
A failClassProvider() 0 9 1
A testFail() 0 6 1
1
<?php
2
/**
3
 * This file is part of the Symfony Console DI package.
4
 *
5
 * (c) Stéphane Demonchaux <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace SymfonyDiConsole\Tests;
11
12
use SymfonyDiConsole\SymfonyDiConsoleInterface;
13
use SymfonyDiConsole\SymfonyConsoleInterface;
14
use SymfonyDiConsole\SymfonyConsoleDiDto;
15
use SymfonyDiConsole\CommandBuilder;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Input\ArrayInput;
18
use Symfony\Component\Console\Output\OutputInterface;
19
use Symfony\Component\Console\Output\NullOutput;
20
21
class ACommand implements SymfonyConsoleInterface
22
{
23
    private $adependency;
24
25
    public function __construct($adependency)
26
    {
27
        $this->adependency = $adependency;
28
    }
29
30
    /**
31
     * @param InputInterface $input
32
     * @param OutputInterface $ouput
33
     */
34
    public function execute(InputInterface $input, OutputInterface $output)
35
    {
36
        $output->write($this->adependency . 'hola' . $input->getArgument('hi') . $input->getOption('anoption'));
37
    }
38
}
39
40
class TestCommand implements SymfonyDiConsoleInterface
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
41
{
42
    /**
43
     * @return SymfonyDiConsoleInterface
44
     */
45
    public static function getInstance()
46
    {
47
        return new ACommand('hola');
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \SymfonyDiCon...Tests\ACommand('hola'); (SymfonyDiConsole\Tests\ACommand) is incompatible with the return type declared by the interface SymfonyDiConsole\Symfony...eInterface::getInstance of type SymfonyDiConsole\SymfonyDiConsoleInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
48
    }
49
50
    /**
51
     * @return SymfonyConsoleDiDto
52
    */
53
    public static function configure()
54
    {
55
        $dto = new SymfonyConsoleDiDto('test', 'this is a sample');
56
        $dto->addArgument('hi');
57
        $dto->addOption('anoption');
58
59
        return $dto;
60
    }
61
}
62
63
class TestCommandTwo implements SymfonyDiConsoleInterface
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
64
{
65
    /**
66
     * @return SymfonyDiConsoleInterface
67
     */
68
    public static function getInstance()
69
    {
70
        return new ACommand('hola');
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \SymfonyDiCon...Tests\ACommand('hola'); (SymfonyDiConsole\Tests\ACommand) is incompatible with the return type declared by the interface SymfonyDiConsole\Symfony...eInterface::getInstance of type SymfonyDiConsole\SymfonyDiConsoleInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
71
    }
72
73
    /**
74
     * @return SymfonyConsoleDiDto
75
     */
76
    public static function configure()
77
    {
78
        $dto = new SymfonyConsoleDiDto();
79
        $dto->setName('test');
80
        $dto->setDescription('this is a sample');
81
        $dto->addArgument('hi');
82
        $dto->addOption('anoption');
83
84
        return $dto;
85
    }
86
}
87
88
89
class CommandBuilderTest extends \PHPUnit_Framework_TestCase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
90
{
91
    public function testOk()
92
    {
93
        $sUT = CommandBuilder::build('\SymfonyDiConsole\Tests\TestCommand');
94
95
        $this->assertInstanceOf('Symfony\Component\Console\Command\Command', $sUT);
96
97
        $this->assertEquals('this is a sample', $sUT->getDescription());
98
99
        $output = $this->getMock('\Symfony\Component\Console\Output\NullOutput');
100
        $output->expects($this->once())
101
        ->method('write')
102
        ->with('holaholanot');
103
104
        $sUT->execute(new ArrayInput(array('hi' => 'not'), $sUT->getDefinition()), $output);
105
    }
106
107
    public function testOkWithDefinitionSetter()
108
    {
109
        $sUT = CommandBuilder::build('\SymfonyDiConsole\Tests\TestCommandTwo');
110
111
        $this->assertInstanceOf('Symfony\Component\Console\Command\Command', $sUT);
112
113
        $this->assertEquals('this is a sample', $sUT->getDescription());
114
115
        $output = $this->getMock('\Symfony\Component\Console\Output\NullOutput');
116
        $output->expects($this->once())
117
        ->method('write')
118
        ->with('holaholanot');
119
120
        $sUT->execute(new ArrayInput(array('hi' => 'not'), $sUT->getDefinition()), $output);
121
    }
122
123
    public function testOkWithOption()
124
    {
125
        $sUT = CommandBuilder::build('\SymfonyDiConsole\Tests\TestCommand');
126
127
        $this->assertInstanceOf('Symfony\Component\Console\Command\Command', $sUT);
128
129
        $this->assertEquals('this is a sample', $sUT->getDescription());
130
131
        $output = $this->getMock('\Symfony\Component\Console\Output\NullOutput');
132
        $output->expects($this->once())
133
        ->method('write')
134
        ->with('holaholanotnot');
135
136
        $sUT->execute(new ArrayInput(array('hi' => 'not', '--anoption' => 'not'), $sUT->getDefinition()), $output);
137
    }
138
139
    public function failClassProvider()
140
    {
141
        return array(
142
            array('\SymfonyDiConsole\Tests\ACommand'),
143
            array('fezfez'),
144
            array(true),
145
            array(new \stdClass())
146
        );
147
    }
148
149
    /**
150
     * @dataProvider failClassProvider
151
     */
152
    public function testFail($className)
153
    {
154
        $this->setExpectedException('\SymfonyDiConsole\InvalidCommandException');
155
156
        CommandBuilder::build($className);
157
    }
158
}
159