Skip to content
This repository has been archived by the owner on Apr 18, 2023. It is now read-only.

Commit

Permalink
Made Timeline accept a MigrationBus as argument, to allow users to cu…
Browse files Browse the repository at this point in the history
…stomise how migrations are run.
  • Loading branch information
gsomoza committed Jul 16, 2015
1 parent 62bd64f commit b470cf0
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 5 deletions.
28 changes: 28 additions & 0 deletions lib/Exception/MigrationBusException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <https://github.com/baleen/migrations>.
*/

namespace Baleen\Migrations\Exception;

/**
* Class MigrationBusException
* @author Gabriel Somoza <gabriel@strategery.io>
*/
class MigrationBusException extends BaleenException
{
}
48 changes: 48 additions & 0 deletions lib/Migration/Command/MigrationBus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <https://github.com/baleen/migrations>.
*/

namespace Baleen\Migrations\Migration\Command;


use Baleen\Migrations\Exception\MigrationBusException;
use League\Tactician\CommandBus;

class MigrationBus extends CommandBus
{
/**
* @inheritdoc
*/
public function __construct(array $middleware)
{
$foundHandler = false;
foreach ($middleware as $object) {
if ($object instanceof MigrateHandler) {
$foundHandler = true;
break;
}
}
if (!$foundHandler) {
throw new MigrationBusException(sprintf(
'MigraitonBus must have at least one instance of "%s"',
MigrateHandler::class
));
}
parent::__construct($middleware);
}
}
3 changes: 1 addition & 2 deletions lib/Migration/Command/MigrationBusFactory.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Expand Down Expand Up @@ -33,7 +32,7 @@ class MigrationBusFactory
{
public static function create()
{
return new CommandBus([
return new MigrationBus([
new SetOptionsMiddleware(),
new TransactionMiddleware(),
new MigrateHandler(),
Expand Down
16 changes: 13 additions & 3 deletions lib/Timeline/AbstractTimeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@

use Baleen\Migrations\Event\HasEmitterTrait;
use Baleen\Migrations\Migration\Command\MigrateCommand;
use Baleen\Migrations\Migration\Command\MigrationBus;
use Baleen\Migrations\Migration\Command\MigrationBusFactory;
use Baleen\Migrations\Migration\Factory\FactoryInterface;
use Baleen\Migrations\Migration\MigrationInterface;
use Baleen\Migrations\Migration\Options;
use Baleen\Migrations\Version;
use Baleen\Migrations\Version\Collection\LinkedVersions;
use Baleen\Migrations\Version\Comparator\DefaultComparator;
use League\Tactician\CommandBus;

/**
* Encapsulates the lower-level methods of a Timeline, leaving the actual timeline logic to the extending class.
Expand All @@ -54,11 +57,18 @@ abstract class AbstractTimeline implements TimelineInterface

/**
* @param LinkedVersions $versions
* @param callable $comparator
* @param callable $comparator
* @param MigrationBus $migrationBus A CommandBus that will be used to run each individual migration.
*/
public function __construct(LinkedVersions $versions, callable $comparator = null)
public function __construct(
LinkedVersions $versions,
callable $comparator = null,
MigrationBus $migrationBus = null)
{
$this->migrationBus = MigrationBusFactory::create();
if (null === $migrationBus) {
$migrationBus = MigrationBusFactory::create();
}
$this->migrationBus = $migrationBus;

if (null === $comparator) {
$comparator = new DefaultComparator();
Expand Down
41 changes: 41 additions & 0 deletions test/Migration/Command/MigrationBusTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <https://github.com/baleen/migrations>.
*/

namespace BaleenTest\Migrations\Migration\Command;

use Baleen\Migrations\Exception\MigrationBusException;
use Baleen\Migrations\Migration\Command\MigrateHandler;
use Baleen\Migrations\Migration\Command\MigrationBus;
use BaleenTest\Migrations\BaseTestCase;
use Mockery as m;

/**
* Class MigrationBusTest
* @author Gabriel Somoza <gabriel@strategery.io>
*/
class MigrationBusTest extends BaseTestCase
{

public function testThrowsExceptionIfNoMigrateHandler()
{
$this->setExpectedException(MigrationBusException::class, MigrateHandler::class);
$instance = new MigrationBus([]);
}

}
17 changes: 17 additions & 0 deletions test/TimelineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
use Baleen\Migrations\Event\Timeline\MigrationEvent;
use Baleen\Migrations\Exception\MigrationMissingException;
use Baleen\Migrations\Exception\TimelineException;
use Baleen\Migrations\Migration\Command\MigrateCommand;
use Baleen\Migrations\Migration\Command\MigrationBus;
use Baleen\Migrations\Migration\Options;
use Baleen\Migrations\Migration\MigrationInterface;
use Baleen\Migrations\Timeline;
Expand All @@ -33,6 +35,7 @@
use Baleen\Migrations\Version\Comparator\DefaultComparator;
use Mockery as m;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Zend\Stdlib\Hydrator\ObjectProperty;

/**
* @author Gabriel Somoza <gabriel@strategery.io>
Expand Down Expand Up @@ -356,4 +359,18 @@ public function runSingleProvider()
['2', new Options(Options::DIRECTION_DOWN), 'exception' ], // its already down
];
}

public function testDoRunUsesMigrationBusToMigrate()
{
$migrationBus = m::mock(MigrationBus::class);
$migrationBus->shouldReceive('handle')->with(m::type(MigrateCommand::class))->once();

$collection = new LinkedVersions($this->getNoMigratedVersionsFixture());
$options = new Options(Options::DIRECTION_UP);
$instance = new Timeline($collection, null, $migrationBus);

$method = new \ReflectionMethod($instance, 'doRun');
$method->setAccessible(true);
$method->invoke($instance, $collection->current()->getMigration(), $options);
}
}

0 comments on commit b470cf0

Please sign in to comment.