|
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 |
|
|
|
|
|
|
41
|
|
|
{ |
|
42
|
|
|
/** |
|
43
|
|
|
* @return SymfonyDiConsoleInterface |
|
44
|
|
|
*/ |
|
45
|
|
|
public static function getInstance() |
|
46
|
|
|
{ |
|
47
|
|
|
return new ACommand('hola'); |
|
|
|
|
|
|
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 |
|
|
|
|
|
|
64
|
|
|
{ |
|
65
|
|
|
/** |
|
66
|
|
|
* @return SymfonyDiConsoleInterface |
|
67
|
|
|
*/ |
|
68
|
|
|
public static function getInstance() |
|
69
|
|
|
{ |
|
70
|
|
|
return new ACommand('hola'); |
|
|
|
|
|
|
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 |
|
|
|
|
|
|
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
|
|
|
|
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.