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

Commit

Permalink
Merge pull request #7 from jeremygiberson/master
Browse files Browse the repository at this point in the history
Adding unit tests
  • Loading branch information
vgarvardt committed Oct 12, 2015
2 parents 5336eca + 7d759bc commit 6ef5104
Show file tree
Hide file tree
Showing 23 changed files with 781 additions and 19 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.idea/
vendor/
composer.lock
tests/integration/data/ignore/
29 changes: 29 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
language: php
php:
- 5.4
- 5.5
- 5.6
env:
- DB=sqlite
- DB=mysql
- DB=mysqli
- DB=postgresql

matrix:
allow_failures:
- php: 5.4
env: DB=postgresql
- php: 5.5
env: DB=postgresql
- php: 5.6
env: DB=postgresql

services:
- postgresql

before_script:
- composer install
- mysql -e "create database IF NOT EXISTS test;" -uroot
- psql -c 'create database test;' -U postgres

script: phpunit -c phpunit.${DB}.xml
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

Simple Migrations for Zend Framework 2. Project originally based on [ZendDbMigrations](https://github.com/vadim-knyzev/ZendDbMigrations) but module author did not response for issues and pull-requests so fork became independent project.

## Supported Drivers
The following DB adapter drivers are supported by this module.

* Pdo_Sqlite
* Pdo_Mysql
* Mysqli _only if you configure the driver options with `'buffer_results' => true`_


## Installation

### Using composer
Expand Down Expand Up @@ -51,6 +59,22 @@ class Version20130403165433 extends AbstractMigration
}
```

#### Multi-statement sql
While this module supports execution of multiple SQL statements it does not have way to detect if any other statement than the first contained an error. It is *highly* recommended you only provide single SQL statements to `addSql` at a time.
I.e instead of

```
$this->addSql('SELECT NOW(); SELECT NOW(); SELECT NOW();');
```

You should use

```
$this->addSql('SELECT NOW();');
$this->addSql('SELECT NOW();');
$this->addSql('SELECT NOW();');
```

### Accessing ServiceLocator In Migration Class

By implementing the `Zend\ServiceManager\ServiceLocatorAwareInterface` in your migration class you get access to the
Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@
}
],
"require": {
"php": ">=5.3.3",
"php": ">=5.4",
"zendframework/zendframework": ">=2.2.0"
},
"require-dev": {
"phpunit/phpunit": "4.6.*"
},
"autoload": {
"psr-0": {
"ZfSimpleMigrations": "src/"
Expand Down
19 changes: 19 additions & 0 deletions phpunit.mysql.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.6/phpunit.xsd"
bootstrap="vendor/autoload.php"
>
<testsuites>
<testsuite name="Integration Tests">
<directory>tests/integration</directory>
</testsuite>
</testsuites>
<php>
<env name="db_type" value="Pdo_Mysql"/>
<env name="db_host" value="localhost" />
<env name="db_username" value="travis" />
<env name="db_password" value="" />
<env name="db_name" value="test" />
<env name="db_port" value=""/>
</php>
</phpunit>
19 changes: 19 additions & 0 deletions phpunit.mysqli.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.6/phpunit.xsd"
bootstrap="vendor/autoload.php"
>
<testsuites>
<testsuite name="Integration Tests">
<directory>tests/integration</directory>
</testsuite>
</testsuites>
<php>
<env name="db_type" value="Mysqli"/>
<env name="db_host" value="localhost" />
<env name="db_username" value="travis" />
<env name="db_password" value="" />
<env name="db_name" value="test" />
<env name="db_port" value=""/>
</php>
</phpunit>
19 changes: 19 additions & 0 deletions phpunit.postgresql.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.6/phpunit.xsd"
bootstrap="vendor/autoload.php"
>
<testsuites>
<testsuite name="Integration Tests">
<directory>tests/integration</directory>
</testsuite>
</testsuites>
<php>
<env name="db_type" value="Pdo_Pgsql"/>
<env name="db_host" value="localhost" />
<env name="db_username" value="postgres" />
<env name="db_password" value="" />
<env name="db_name" value="test" />
<env name="db_port" value="5432"/>
</php>
</phpunit>
19 changes: 19 additions & 0 deletions phpunit.sqlite.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.6/phpunit.xsd"
bootstrap="vendor/autoload.php"
>
<testsuites>
<testsuite name="Integration Tests">
<directory>tests/integration</directory>
</testsuite>
</testsuites>
<php>
<env name="db_type" value="Pdo_Sqlite"/>
<env name="db_host" value="" />
<env name="db_username" value="" />
<env name="db_password" value="" />
<env name="db_name" value="%BASE_DIR%/tests/integration/data/ignore/db.sqlite" />
<env name="db_port" value=""/>
</php>
</phpunit>
11 changes: 11 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.6/phpunit.xsd"
bootstrap="vendor/autoload.php"
>
<testsuites>
<testsuite name="Unit Tests">
<directory>tests/unit</directory>
</testsuite>
</testsuites>
</phpunit>
22 changes: 13 additions & 9 deletions src/ZfSimpleMigrations/Library/Migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ class Migration implements ServiceLocatorAwareInterface
*/
protected $serviceLocator;

/**
* @return \ZfSimpleMigrations\Library\OutputWriter
*/
public function getOutputWriter()
{
return $this->outputWriter;
}

/**
* @param \Zend\Db\Adapter\Adapter $adapter
* @param array $config
Expand Down Expand Up @@ -288,12 +296,7 @@ protected function applyMigration(array $migration, $down = false, $fake = false
$sqlList = $down ? $migrationObject->getDownSql() : $migrationObject->getUpSql();
foreach ($sqlList as $sql) {
$this->outputWriter->writeLine("Execute query:\n\n" . $sql);
/** @var \Zend\Db\Adapter\Driver\Pdo\Statement $statement */
$statement = $this->connection->prepare($sql);
$statement->execute();
/** @var \PDOStatement $statement */
$statement = $statement->getResource();
while ($statement->nextRowset()) {/* https://bugs.php.net/bug.php?id=61613 */}
$this->connection->execute($sql);
}
}

Expand All @@ -305,12 +308,13 @@ protected function applyMigration(array $migration, $down = false, $fake = false
$this->connection->commit();
} catch (InvalidQueryException $e) {
$this->connection->rollback();
$msg = sprintf('%s: "%s"; File: %s; Line #%d', $e->getMessage(), $e->getPrevious()->getMessage(), $e->getFile(), $e->getLine());
throw new MigrationException($msg);
$previousMessage = $e->getPrevious() ? $e->getPrevious()->getMessage() : null;
$msg = sprintf('%s: "%s"; File: %s; Line #%d', $e->getMessage(), $previousMessage, $e->getFile(), $e->getLine());
throw new MigrationException($msg, $e->getCode(), $e);
} catch (\Exception $e) {
$this->connection->rollback();
$msg = sprintf('%s; File: %s; Line #%d', $e->getMessage(), $e->getFile(), $e->getLine());
throw new MigrationException($msg);
throw new MigrationException($msg, $e->getCode(), $e);
}
}

Expand Down
8 changes: 5 additions & 3 deletions src/ZfSimpleMigrations/Library/MigrationAbstractFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class MigrationAbstractFactory implements AbstractFactoryInterface
*/
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
{
return preg_match(self::FACTORY_PATTERN, $name);
return preg_match(self::FACTORY_PATTERN, $name) || preg_match(self::FACTORY_PATTERN, $requestedName);
}

/**
Expand All @@ -31,7 +31,7 @@ public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator
* @param ServiceLocatorInterface $serviceLocator
* @param $name
* @param $requestedName
* @return mixed
* @return Migration
*/
public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
{
Expand All @@ -41,7 +41,9 @@ public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $

$config = $serviceLocator->get('Config');

preg_match(self::FACTORY_PATTERN, $name, $matches);
preg_match(self::FACTORY_PATTERN, $name, $matches)
|| preg_match(self::FACTORY_PATTERN, $requestedName, $matches);

$name = $matches[1];

if (! isset($config['migrations'][$name])) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class MigrationSkeletonGeneratorAbstractFactory implements AbstractFactoryInterf
*/
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
{
return preg_match(self::FACTORY_PATTERN, $name);
return preg_match(self::FACTORY_PATTERN, $name)
|| preg_match(self::FACTORY_PATTERN, $requestedName);
}

/**
Expand All @@ -39,7 +40,8 @@ public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $
$serviceLocator = $serviceLocator->getServiceLocator();
}

preg_match(self::FACTORY_PATTERN, $name, $matches);
preg_match(self::FACTORY_PATTERN, $name, $matches)
|| preg_match(self::FACTORY_PATTERN, $requestedName, $matches);
$migration_name = $matches[1];


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class MigrationVersionTableAbstractFactory implements AbstractFactoryInterface
*/
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
{
return preg_match(self::FACTORY_PATTERN, $name);
return preg_match(self::FACTORY_PATTERN, $name) || preg_match(self::FACTORY_PATTERN, $requestedName);
}

/**
Expand All @@ -34,7 +34,10 @@ public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator
*/
public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
{
preg_match(self::FACTORY_PATTERN, $name, $matches);
// $matches will be set by first preg_match if it matches, or second preg_match if it doesnt
preg_match(self::FACTORY_PATTERN, $name, $matches)
|| preg_match(self::FACTORY_PATTERN, $requestedName, $matches);

$adapter_name = $matches[1];

/** @var $tableGateway TableGateway */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class MigrationVersionTableGatewayAbstractFactory implements AbstractFactoryInte
*/
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
{
return preg_match(self::FACTORY_PATTERN, $name);
return preg_match(self::FACTORY_PATTERN, $name)
|| preg_match(self::FACTORY_PATTERN, $requestedName);
}

/**
Expand All @@ -35,7 +36,8 @@ public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator
*/
public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
{
preg_match(self::FACTORY_PATTERN, $name, $matches);
preg_match(self::FACTORY_PATTERN, $name, $matches)
|| preg_match(self::FACTORY_PATTERN, $requestedName, $matches);
$adapter_name = $matches[1];

/** @var $dbAdapter \Zend\Db\Adapter\Adapter */
Expand Down
Loading

0 comments on commit 6ef5104

Please sign in to comment.