Skip to content

Commit

Permalink
Confinguration: check if migration class exists added
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Oct 27, 2015
1 parent 68cc7bf commit a53d7c8
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
19 changes: 19 additions & 0 deletions lib/Doctrine/DBAL/Migrations/Configuration/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Migrations\Finder\MigrationDeepFinderInterface;
use Doctrine\DBAL\Migrations\MigrationException;
use Doctrine\DBAL\Migrations\MigrationClassNotFoundException;
use Doctrine\DBAL\Migrations\OutputWriter;
use Doctrine\DBAL\Migrations\Version;
use Doctrine\DBAL\Migrations\Finder\MigrationFinderInterface;
Expand Down Expand Up @@ -371,6 +372,8 @@ public function registerMigrationsFromDirectory($path)
*/
public function registerMigration($version, $class)
{
$this->ensureMigrationClassExists($class);

$version = (string) $version;
$class = (string) $class;
if (isset($this->migrations[$version])) {
Expand Down Expand Up @@ -779,4 +782,20 @@ private function shouldExecuteMigration($direction, Version $version, $to, $migr
return $version->getVersion() <= $to;
}
}

/**
* @param string $class
*/
private function ensureMigrationClassExists($class)
{
if ( ! class_exists($class)) {
throw new MigrationClassNotFoundException(
sprintf(
'Migration class "%s" was not found. Is it placed in "%s" namespace?',
$class,
$this->getMigrationsNamespace()
)
);
}
}
}
26 changes: 26 additions & 0 deletions lib/Doctrine/DBAL/Migrations/MigrationClassNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?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 LGPL. For more information, see
* <http://www.doctrine-project.org>.
*/

namespace Doctrine\DBAL\Migrations;

use Exception;

class MigrationClassNotFoundException extends Exception
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@ public function testOutputWriterCanBeSet()
$this->assertSame($outputWriter, $configuration->getOutputWriter());
}

public function testRegisterMigrationsClassExistCheck()
{
$migrationsDir = __DIR__ . '/ConfigurationTestSource/Migrations';

$configuration = new Configuration($this->getConnectionMock());
$configuration->setMigrationsNamespace('Migrations');
$configuration->setMigrationsDirectory($migrationsDir);

$this->setExpectedException(
'Doctrine\DBAL\Migrations\MigrationClassNotFoundException',
'Migration class "Migrations\Version123" was not found. Is it placed in "Migrations" namespace?');
$configuration->registerMigrationsFromDirectory($migrationsDir);
}

/**
* @return \PHPUnit_Framework_MockObject_MockObject|Connection
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Migrations\IncorrectNamespace;

class Version123
{

}

0 comments on commit a53d7c8

Please sign in to comment.