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\Repository; |
21
|
|
|
|
22
|
|
|
use Baleen\Cli\CommandBus\Repository\ListMessage; |
23
|
|
|
use Baleen\Cli\CommandBus\Repository\ListHandler; |
24
|
|
|
use Baleen\Migrations\Migration\MigrationInterface; |
25
|
|
|
use Baleen\Migrations\Repository\RepositoryInterface; |
26
|
|
|
use Baleen\Migrations\Version; |
27
|
|
|
use Baleen\Migrations\Version\Collection\LinkedVersions; |
28
|
|
|
use BaleenTest\Cli\CommandBus\HandlerTestCase; |
29
|
|
|
use Mockery as m; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Class ListHandlerTest |
33
|
|
|
* @author Gabriel Somoza <[email protected]> |
34
|
|
|
*/ |
35
|
|
|
class ListHandlerTest extends HandlerTestCase |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* setUp |
39
|
|
|
*/ |
40
|
|
|
public function setUp() |
41
|
|
|
{ |
42
|
|
|
$this->instance = m::mock(ListHandler::class) |
43
|
|
|
->shouldAllowMockingProtectedMethods() |
44
|
|
|
->makePartial(); |
45
|
|
|
$this->command = m::mock(ListMessage::class); |
46
|
|
|
parent::setUp(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* testOutputVersions |
51
|
|
|
* @param LinkedVersions $versions |
52
|
|
|
* @dataProvider versionsProvider |
53
|
|
|
*/ |
54
|
|
|
public function testOutputVersions(LinkedVersions $versions) |
55
|
|
|
{ |
56
|
|
|
foreach ($versions as $version) { |
57
|
|
|
$id = $version->getId(); |
58
|
|
|
$class = str_replace('\\', '\\\\', get_class($version->getMigration())); |
59
|
|
|
$this->output->shouldReceive('writeln')->with("/$id.*$class/")->once(); |
60
|
|
|
} |
61
|
|
|
$this->instance->outputVersions($versions, $this->output); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* testHandle |
66
|
|
|
* @param LinkedVersions $versions |
67
|
|
|
* @param $newestFirst |
68
|
|
|
* @dataProvider versionsProvider |
69
|
|
|
*/ |
70
|
|
|
public function testHandle(LinkedVersions $versions, $newestFirst) |
71
|
|
|
{ |
72
|
|
|
$comparator = function(){}; |
73
|
|
|
$repository = m::mock(RepositoryInterface::class); |
74
|
|
|
$this->command->shouldReceive([ |
75
|
|
|
'getRepository' => $repository, |
76
|
|
|
'getComparator' => $comparator, |
77
|
|
|
])->once(); |
78
|
|
|
|
79
|
|
|
$this->input->shouldReceive('getOption')->with('newest-first')->once()->andReturn($newestFirst); |
80
|
|
|
$this->instance->shouldReceive('getCollection')->once()->andReturn($versions); |
81
|
|
|
|
82
|
|
|
if (count($versions)) { |
83
|
|
|
$firstVersion = $newestFirst ? $versions->getReverse()->current() : $versions->current(); |
84
|
|
|
$this->instance |
85
|
|
|
->shouldReceive('outputVersions') |
86
|
|
|
->with(m::on(function($versions) use ($firstVersion) { |
87
|
|
|
if (!$versions instanceof LinkedVersions) { |
88
|
|
|
return false; |
89
|
|
|
} |
90
|
|
|
$versions->rewind(); |
91
|
|
|
return $firstVersion === $versions->current(); |
92
|
|
|
}), $this->output) |
93
|
|
|
->once(); |
94
|
|
|
} else { |
95
|
|
|
$this->output->shouldReceive('writeln')->once(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$this->handle(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* versionsProvider |
103
|
|
|
* @return array |
104
|
|
|
*/ |
105
|
|
|
public function versionsProvider() |
106
|
|
|
{ |
107
|
|
|
$trueFalse = [true, false]; |
108
|
|
|
$arrayCollections = [ |
109
|
|
|
[], |
110
|
|
|
Version::fromArray(1, 2, 3, 4, 5), |
111
|
|
|
Version::fromArray(1, 2, 'abc', 4, 5) |
112
|
|
|
]; |
113
|
|
|
$collections = []; |
114
|
|
|
foreach ($arrayCollections as $collection) { |
115
|
|
|
foreach ($collection as $version) { |
116
|
|
|
/** @var m\Mock|Version $version */ |
117
|
|
|
/** @var m\Mock|MigrationInterface $migration */ |
118
|
|
|
$migration = m::mock(MigrationInterface::class); |
119
|
|
|
$version->setMigration($migration); |
120
|
|
|
} |
121
|
|
|
$collections[] = new LinkedVersions($collection); |
122
|
|
|
} |
123
|
|
|
return $this->combinations([$collections, $trueFalse]);; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|