Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Commit

Permalink
Check for doctrine migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
glaubinix committed Apr 17, 2015
1 parent 813842f commit e75ceee
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 2 deletions.
16 changes: 16 additions & 0 deletions README.md
Expand Up @@ -26,6 +26,7 @@ It currently ships with the following Diagnostic Checks:
* [Redis](#redis) - Validate that a Redis service is running,
* [SecurityAdvisory](#securityadvisory) - check installed composer dependencies against SensioLabs SA database,
* [StreamWrapperExists](#streamwrapperexists) - make sure given stream wrapper is available.
* [DoctrineMigration](#doctrinemigration) - make sure all migrations are applied.

File validation checks:

Expand Down Expand Up @@ -578,6 +579,21 @@ $checkCompression = new StreamWrapperExists(array(
));
````

### DoctrineMigration

Make sure all migrations are applied:

````php
<?php
use Doctrine\DBAL\Migrations\Configuration\Configuration;
use Doctrine\ORM\EntityManager;
use ZendDiagnostics\Check\DoctrineMigration;

$em = EntityManager::create(/** config */);
$migrationConfig = new Configuration($em);
$check = new DoctrineMigration($migrationConfig);
````

### IniFile

Read an INI-file from the given path and try to parse it.
Expand Down
6 changes: 4 additions & 2 deletions composer.json
Expand Up @@ -24,15 +24,17 @@
"guzzle/http" : "3.*",
"guzzle/plugin-mock" : "3.*",
"videlalvaro/php-amqplib" : "2.*",
"predis/predis" : "0.8.*"
"predis/predis" : "0.8.*",
"doctrine/migrations" : "~1.0@dev"
},
"suggest" : {
"ext-bcmath" : "Required by Check\\CpuPerformance",
"sensiolabs/security-checker": "Required by Check\\SecurityAdvisory",
"symfony/yaml": "Required by Check\\YamlFile",
"guzzle/http": "Required by Check\\GuzzleHttpService",
"predis/predis": "Required by Check\\Redis",
"videlalvaro/php-amqplib": "Required by Check\\RabbitMQ"
"videlalvaro/php-amqplib": "Required by Check\\RabbitMQ",
"doctrine/migrations": "Required by Check\\DoctrineMigration"
},
"extra": {
"branch-alias": {
Expand Down
50 changes: 50 additions & 0 deletions src/ZendDiagnostics/Check/DoctrineMigration.php
@@ -0,0 +1,50 @@
<?php
/**
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendDiagnostics\Check;

use Doctrine\DBAL\Migrations\Configuration\Configuration;
use ZendDiagnostics\Result\Failure;
use ZendDiagnostics\Result\ResultInterface;
use ZendDiagnostics\Result\Success;

class DoctrineMigration extends AbstractCheck
{
/**
* @var Configuration
*/
private $migrationConfiguration;

/**
* @param Configuration $migrationConfiguration
*/
public function __construct(Configuration $migrationConfiguration)
{
$this->migrationConfiguration = $migrationConfiguration;
}

/**
* Perform the actual check and return a ResultInterface
*
* @return ResultInterface
*/
public function check()
{
$availableVersions = $this->migrationConfiguration->getAvailableVersions();
$migratedVersions = $this->migrationConfiguration->getMigratedVersions();

$notMigratedVersions = array_diff($availableVersions, $migratedVersions);
if (!empty($notMigratedVersions)) {
return new Failure('Not all migrations applied', $notMigratedVersions);
}

$notAvailableVersion = array_diff($migratedVersions, $availableVersions);
if (!empty($notAvailableVersion)) {
return new Failure('Migrations applied which are not available', $notMigratedVersions);
}

return new Success();
}
}
74 changes: 74 additions & 0 deletions tests/ZendDiagnosticsTest/DoctrineMigrationTest.php
@@ -0,0 +1,74 @@
<?php

namespace ZendDiagnosticsTest;

use ZendDiagnostics\Check\DoctrineMigration;

class DoctrineMigrationTest extends \PHPUnit_Framework_TestCase
{
public function testEverythingMigrated()
{
$configuration = $this->getMockBuilder('Doctrine\DBAL\Migrations\Configuration\Configuration')
->disableOriginalConstructor()
->getMock();

$configuration
->expects($this->once())
->method('getAvailableVersions')
->will($this->returnValue(array('Version1', 'Version2')));

$configuration
->expects($this->once())
->method('getMigratedVersions')
->will($this->returnValue(array('Version1', 'Version2')));

$check = new DoctrineMigration($configuration);
$result = $check->check();

$this->assertInstanceof('ZendDiagnostics\Result\SuccessInterface', $result);
}

public function testNotAllMigrationsMigrated()
{
$configuration = $this->getMockBuilder('Doctrine\DBAL\Migrations\Configuration\Configuration')
->disableOriginalConstructor()
->getMock();

$configuration
->expects($this->once())
->method('getAvailableVersions')
->will($this->returnValue(array('Version1', 'Version2')));

$configuration
->expects($this->once())
->method('getMigratedVersions')
->will($this->returnValue(array('Version1')));

$check = new DoctrineMigration($configuration);
$result = $check->check();

$this->assertInstanceof('ZendDiagnostics\Result\FailureInterface', $result);
}

public function testNoExistingMigrationMigrated()
{
$configuration = $this->getMockBuilder('Doctrine\DBAL\Migrations\Configuration\Configuration')
->disableOriginalConstructor()
->getMock();

$configuration
->expects($this->once())
->method('getAvailableVersions')
->will($this->returnValue(array('Version1')));

$configuration
->expects($this->once())
->method('getMigratedVersions')
->will($this->returnValue(array('Version1', 'Version2')));

$check = new DoctrineMigration($configuration);
$result = $check->check();

$this->assertInstanceof('ZendDiagnostics\Result\FailureInterface', $result);
}
}

0 comments on commit e75ceee

Please sign in to comment.