Skip to content

Commit

Permalink
define test schema in doctrine dbal schema for multiple db vendor sup…
Browse files Browse the repository at this point in the history
…port
  • Loading branch information
chriskapp committed Jan 24, 2015
1 parent 2eb9a39 commit f72db24
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 151 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ before_script:
- composer require doctrine/orm ~2.4
- composer require twig/twig ~1.16
- mysql -e 'create database psx;'
- mysql -u root psx < tests/psx.sql
- sh -c '( if [ "$TRAVIS_PHP_VERSION" != "hhvm" ]; then php -S 127.0.0.1:8000 tests/PSX/Http/Server.php; fi; ) &'
- sleep 4
script:
Expand Down
3 changes: 3 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@
<directory suffix=".php">./library/PSX</directory>
</whitelist>
</filter>
<php>
<!-- <env name="DB" value="sqlite" /> -->
</php>
</phpunit>
98 changes: 98 additions & 0 deletions tests/PSX/Sql/TestSchema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php
/*
* psx
* A object oriented and modular based PHP framework for developing
* dynamic web applications. For the current version and informations
* visit <http://phpsx.org>
*
* Copyright (c) 2010-2015 Christoph Kappestein <[email protected]>
*
* This file is part of psx. psx is free software: you can
* redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or any later version.
*
* psx is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with psx. If not, see <http://www.gnu.org/licenses/>.
*/

namespace PSX\Sql;

use Doctrine\DBAL\Schema\Schema;

/**
* TestSchema
*
* @author Christoph Kappestein <[email protected]>
* @license http://www.gnu.org/licenses/gpl.html GPLv3
* @link http://phpsx.org
*/
class TestSchema
{
public static function getSchema()
{
$schema = new Schema();

self::addTableCacheHandlerSqlTest($schema);
self::addTableHandlerComment($schema);
self::addTableSqlTableTest($schema);
self::addTableCommandTest($schema);

return $schema;
}

protected static function addTableCacheHandlerSqlTest(Schema $schema)
{
$table = $schema->createTable('psx_cache_handler_sql_test');
$table->addColumn('id', 'string', array('length' => 32, 'autoincrement' => true));
$table->addColumn('content', 'blob');
$table->addColumn('date', 'datetime', array('notnull' => false, 'default' => null));
$table->setPrimaryKey(array('id'));
}

protected static function addTableHandlerComment(Schema $schema)
{
$table = $schema->createTable('psx_handler_comment');
$table->addColumn('id', 'integer', array('length' => 10, 'autoincrement' => true));
$table->addColumn('userId', 'integer', array('length' => 10));
$table->addColumn('title', 'string', array('length' => 32));
$table->addColumn('date', 'datetime');
$table->setPrimaryKey(array('id'));
}

protected static function addTableSqlTableTest(Schema $schema)
{
$table = $schema->createTable('psx_sql_table_test');
$table->addColumn('id', 'integer', array('length' => 10, 'autoincrement' => true));
$table->addColumn('title', 'string', array('length' => 32));
$table->addColumn('date', 'datetime');
$table->setPrimaryKey(array('id'));
}

protected static function addTableCommandTest(Schema $schema)
{
$table = $schema->createTable('psx_table_command_test');
$table->addColumn('id', 'integer', array('length' => 10, 'autoincrement' => true));
$table->addColumn('col_bigint', 'bigint');
$table->addColumn('col_blob', 'blob');
$table->addColumn('col_boolean', 'boolean');
$table->addColumn('col_datetime', 'datetime');
$table->addColumn('col_datetimetz', 'datetimetz');
$table->addColumn('col_date', 'date');
$table->addColumn('col_decimal', 'decimal');
$table->addColumn('col_float', 'float');
$table->addColumn('col_integer', 'integer');
$table->addColumn('col_smallint', 'smallint');
$table->addColumn('col_text', 'text');
$table->addColumn('col_time', 'time');
$table->addColumn('col_string', 'string');
$table->addColumn('col_array', 'array');
$table->addColumn('col_object', 'object');
$table->setPrimaryKey(array('id'));
}
}
78 changes: 65 additions & 13 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,81 @@ function getContainer()
{
$container = require_once(__DIR__ . '/../container.php');

$config = $container->get('config');
$config['psx_url'] = 'http://127.0.0.1';
$config['psx_dispatch'] = '';
$config['psx_path_cache'] = 'cache';
$config['psx_path_library'] = 'library';
setUpConnection($container);
setUpConfig($container);
}

return $container;
}

function hasConnection()
{
return PSX_CONNECTION === true;
}

function setUpConnection($container)
{
$params = null;
switch(getenv('DB'))
{
case 'mysql':
$params = array(
'dbname' => $container->get('config')->get('psx_sql_db'),
'user' => $container->get('config')->get('psx_sql_user'),
'password' => $container->get('config')->get('psx_sql_pw'),
'host' => $container->get('config')->get('psx_sql_host'),
'driver' => 'pdo_mysql',
);
break;

// check whether an SQL connection is available
case 'none':
$params = null;
break;

default:
case 'sqlite':
$params = array(
'url' => 'sqlite:///:memory:'
);
break;
}

if(!empty($params))
{
try
{
$container->get('connection')->query('SELECT PI()');
$config = new Doctrine\DBAL\Configuration();
$config->setSQLLogger(new PSX\Sql\Logger($container->get('logger')));

$connection = Doctrine\DBAL\DriverManager::getConnection($params, $config);
$fromSchema = $connection->getSchemaManager()->createSchema();
$toSchema = PSX\Sql\TestSchema::getSchema();
$queries = $fromSchema->getMigrateToSql($toSchema, $connection->getDatabasePlatform());

foreach($queries as $query)
{
$connection->query($query);
}

$container->set('connection', $connection);

define('PSX_CONNECTION', true);

return;
}
catch(\Exception $e)
catch(Doctrine\DBAL\DBALException $e)
{
define('PSX_CONNECTION', false);
}
}

return $container;
define('PSX_CONNECTION', false);
}

function hasConnection()
function setUpConfig($container)
{
return PSX_CONNECTION === true;
}
$config = $container->get('config');
$config['psx_url'] = 'http://127.0.0.1';
$config['psx_dispatch'] = '';
$config['psx_path_cache'] = 'cache';
$config['psx_path_library'] = 'library';
}
137 changes: 0 additions & 137 deletions tests/psx.sql

This file was deleted.

0 comments on commit f72db24

Please sign in to comment.