1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the MIT license. For more information, see |
17
|
|
|
* <http://www.doctrine-project.org>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace BaleenTest\Cli\CommandBus; |
21
|
|
|
|
22
|
|
|
use Baleen\Cli\CommandBus\AbstractMessage; |
23
|
|
|
use Baleen\Cli\CommandBus\Storage\LatestMessage; |
24
|
|
|
use Baleen\Migrations\Storage\StorageInterface; |
25
|
|
|
use Baleen\Migrations\Version; |
26
|
|
|
use Baleen\Migrations\Version\Collection\MigratedVersions; |
27
|
|
|
use BaleenTest\Cli\BaseTestCase; |
28
|
|
|
use Mockery as m; |
29
|
|
|
use Mockery\Matcher\MatcherAbstract; |
30
|
|
|
use Symfony\Component\Console\Command\Command; |
31
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
32
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Class HandlerTestCase |
36
|
|
|
* @author Gabriel Somoza <[email protected]> |
37
|
|
|
*/ |
38
|
|
|
class HandlerTestCase extends BaseTestCase |
39
|
|
|
{ |
40
|
|
|
/** @var m\Mock|InputInterface */ |
41
|
|
|
protected $input; |
42
|
|
|
|
43
|
|
|
/** @var m\Mock|OutputInterface */ |
44
|
|
|
protected $output; |
45
|
|
|
|
46
|
|
|
/** @var m\Mock|LatestMessage */ |
47
|
|
|
protected $instance; |
48
|
|
|
|
49
|
|
|
/** @var m\Mock|StorageInterface */ |
50
|
|
|
protected $storage; |
51
|
|
|
|
52
|
|
|
/** @var m\Mock|AbstractMessage */ |
53
|
|
|
protected $command; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* setUp |
57
|
|
|
*/ |
58
|
|
|
public function setUp() |
59
|
|
|
{ |
60
|
|
|
parent::setUp(); |
61
|
|
|
$this->input = m::mock(InputInterface::class); |
62
|
|
|
$this->output = m::mock(OutputInterface::class); |
63
|
|
|
$this->storage = m::mock(StorageInterface::class); |
64
|
|
|
if ($this->command) { |
65
|
|
|
$this->command->shouldReceive('getInput')->zeroOrMoreTimes()->andReturn($this->input); |
66
|
|
|
$this->command->shouldReceive('getOutput')->zeroOrMoreTimes()->andReturn($this->output); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* tearDown |
72
|
|
|
*/ |
73
|
|
|
public function tearDown() |
74
|
|
|
{ |
75
|
|
|
parent::tearDown(); |
76
|
|
|
$this->instance = null; |
77
|
|
|
$this->input = null; |
78
|
|
|
$this->output = null; |
79
|
|
|
$this->storage = null; |
80
|
|
|
$this->command = null; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Assers that the command is named after its overwritten COMMAND_NAME constant |
85
|
|
|
* @param Command $command |
86
|
|
|
*/ |
87
|
|
|
protected function assertCommandIsNamedProperly(Command $command) |
88
|
|
|
{ |
89
|
|
|
$this->assertNotEmpty(LatestMessage::COMMAND_NAME); |
90
|
|
|
$this->assertContains(LatestMessage::COMMAND_NAME, $command->getName()); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Calls handle() on the current instance, passing the current input and output mocks |
95
|
|
|
*/ |
96
|
|
|
protected function handle() |
97
|
|
|
{ |
98
|
|
|
$this->instance->handle($this->command); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param $versions |
103
|
|
|
* @return MigratedVersions |
104
|
|
|
*/ |
105
|
|
|
protected function getMigratedCollection(array $versions) |
106
|
|
|
{ |
107
|
|
|
if (!count($versions)) { |
108
|
|
|
return $versions; |
109
|
|
|
} |
110
|
|
|
foreach ($versions as $version) { |
111
|
|
|
/** @var Version $version */ |
112
|
|
|
$version->setMigrated(true); |
113
|
|
|
} |
114
|
|
|
return new MigratedVersions($versions); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param Command $instance |
119
|
|
|
* @param $name |
120
|
|
|
*/ |
121
|
|
|
protected function assertHasArgument(Command $instance, $name) |
122
|
|
|
{ |
123
|
|
|
$this->assertTrue( |
124
|
|
|
$instance->getDefinition()->hasArgument($name), |
125
|
|
|
sprintf("Expected command to have an argument named '%s'.", $name) |
126
|
|
|
); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param Command $instance |
131
|
|
|
* @param $name |
132
|
|
|
*/ |
133
|
|
|
protected function assertHasOption(Command $instance, $name) |
134
|
|
|
{ |
135
|
|
|
$this->assertTrue( |
136
|
|
|
$instance->getDefinition()->hasOption($name), |
137
|
|
|
sprintf("Expected command to have an argument named '%s'.", $name) |
138
|
|
|
); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param Command $instance |
143
|
|
|
* @param $alias |
144
|
|
|
*/ |
145
|
|
|
protected function assertHasAlias(Command $instance, $alias) |
146
|
|
|
{ |
147
|
|
|
$this->assertContains($alias, $instance->getAliases()); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param mixed $result |
152
|
|
|
* @param MatcherAbstract $validator |
153
|
|
|
*/ |
154
|
|
|
protected function assertQuestionAsked($result = null, MatcherAbstract $validator = null) |
155
|
|
|
{ |
156
|
|
|
$helper = m::mock(); |
157
|
|
|
$helper->shouldReceive('ask')->with($this->input, $this->output, m::on(function($param) use ($validator) { |
158
|
|
|
return null !== $validator ? $validator->match($param) : true; |
159
|
|
|
}))->once()->andReturn($result); |
160
|
|
|
$this->command->shouldReceive('getCliCommand->getHelper')->with('question')->andReturn($helper); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|