|
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
|
|
|
namespace BaleenTest\Cli\CommandBus\Storage; |
|
20
|
|
|
|
|
21
|
|
|
use Baleen\Cli\CommandBus\Storage\LatestMessage; |
|
22
|
|
|
use Baleen\Cli\CommandBus\Storage\LatestHandler; |
|
23
|
|
|
use Baleen\Cli\Exception\CliException; |
|
24
|
|
|
use Baleen\Migrations\Version; |
|
25
|
|
|
use Baleen\Migrations\Version\Comparator\DefaultComparator; |
|
26
|
|
|
use BaleenTest\Cli\CommandBus\HandlerTestCase; |
|
27
|
|
|
use Mockery as m; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Class LatestHandlerTest |
|
31
|
|
|
* @author Gabriel Somoza <[email protected]> |
|
32
|
|
|
*/ |
|
33
|
|
|
class LatestHandlerTest extends HandlerTestCase |
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
36
|
|
|
* setUp |
|
37
|
|
|
*/ |
|
38
|
|
|
public function setUp() |
|
39
|
|
|
{ |
|
40
|
|
|
$this->instance = m::mock(LatestHandler::class) |
|
41
|
|
|
->shouldAllowMockingProtectedMethods() |
|
42
|
|
|
->makePartial(); |
|
43
|
|
|
$this->command = m::mock(LatestMessage::class); |
|
44
|
|
|
parent::setUp(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* testHandle |
|
49
|
|
|
* @param $versions |
|
50
|
|
|
* @param $lastId |
|
51
|
|
|
* @param callable $comparator |
|
52
|
|
|
* @dataProvider executeProvider |
|
53
|
|
|
*/ |
|
54
|
|
|
public function testHandle($versions, $lastId, callable $comparator = null) |
|
55
|
|
|
{ |
|
56
|
|
|
$this->command->shouldReceive('getComparator')->once()->andReturn($comparator ?: new DefaultComparator()); |
|
57
|
|
|
if (count($versions) > 0) { |
|
58
|
|
|
$migrated = $this->getMigratedCollection($versions); |
|
59
|
|
|
$this->output->shouldReceive('writeln')->with($lastId)->once(); |
|
60
|
|
|
} else { |
|
61
|
|
|
$migrated = $versions; |
|
62
|
|
|
$this->output->shouldReceive('writeln')->with(m::type('string'))->once(); |
|
63
|
|
|
} |
|
64
|
|
|
$this->command->shouldReceive('getStorage')->once()->andReturn($this->storage); |
|
65
|
|
|
$this->storage->shouldReceive('fetchAll')->once()->andReturn($migrated); |
|
66
|
|
|
$this->handle(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* testHandleWithInvalidComparator |
|
71
|
|
|
*/ |
|
72
|
|
|
public function testHandleWithInvalidComparator() |
|
73
|
|
|
{ |
|
74
|
|
|
$this->command->shouldReceive('getComparator')->once()->andReturn('notCallable'); |
|
75
|
|
|
$this->command->shouldNotReceive('getStorage'); |
|
76
|
|
|
$this->setExpectedException(CliException::class, 'comparator'); |
|
77
|
|
|
$this->handle(); |
|
78
|
|
|
} |
|
79
|
|
|
/** |
|
80
|
|
|
* @return array |
|
81
|
|
|
*/ |
|
82
|
|
|
public function executeProvider() |
|
83
|
|
|
{ |
|
84
|
|
|
return [ |
|
85
|
|
|
[ [], 5, new DefaultComparator()], |
|
86
|
|
|
[ Version::fromArray(1, 2, 3, 4, 5), 5], // simple |
|
87
|
|
|
[ Version::fromArray(1, 2, 3, 4, 5, -6), -6], // last item is -6 |
|
88
|
|
|
[ Version::fromArray(3, 5, 1, 6, 7, 2), 7], // default order |
|
89
|
|
|
[ Version::fromArray(3, 5, 1, 6, 7, 2), 1, function(Version $v1, Version $v2) { // reverse order |
|
90
|
|
|
return (int) $v2->getId() - (int) $v1->getId(); |
|
91
|
|
|
}], |
|
92
|
|
|
]; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|