Skip to content
Browse files

Added PR 194 from Propel 1.6. Fixes #148

  • Loading branch information...
1 parent 80f03ab commit 08be56a47feb9e00f1528f85909e1f6de1468d76 @willdurand willdurand committed Mar 7, 2012
View
1 .gitignore
@@ -17,5 +17,6 @@ tests/Fixtures/bookstore/build.properties
tests/Fixtures/namespaced/build.properties
tests/Fixtures/reverse/mysql/build.properties
tests/Fixtures/reverse/mysql/runtime-conf.xml
+tests/Fixtures/reverse/pgsql/runtime-conf.xml
tests/Fixtures/schemas/build.properties
vendor/
View
4 src/Propel/Generator/Command/TestPrepare.php
@@ -47,6 +47,7 @@ class TestPrepare extends AbstractCommand
'bookstore-packaged' => array('bookstore-packaged', 'bookstore-log'),
'namespaced' => array('bookstore_namespaced'),
'reverse/mysql' => array('reverse-bookstore'),
+ 'reverse/pgsql' => array('reverse-bookstore'),
'schemas' => array('bookstore-schemas'),
);
@@ -105,8 +106,8 @@ protected function buildFixtures($fixturesDir, $connections, InputInterface $inp
chdir($fixturesDir);
$distributionFiles = array(
- 'build.properties.dist' => 'build.properties',
'runtime-conf.xml.dist' => 'runtime-conf.xml',
+ 'build.properties.dist' => 'build.properties',
);
foreach ($distributionFiles as $sourceFile => $targetFile) {
@@ -121,6 +122,7 @@ protected function buildFixtures($fixturesDir, $connections, InputInterface $inp
file_put_contents($targetFile, $content);
} else {
$output->writeln(sprintf('<error>No "%s" file found, skipped.</error>', $sourceFile));
+ chdir($this->root);
return;
}
View
11 src/Propel/Generator/Reverse/PgsqlSchemaParser.php
@@ -14,13 +14,18 @@
require_once 'phing/Task.php';
use Task;
-use PDO;
use Propel\Generator\Model\Column;
+use Propel\Generator\Model\ColumnDefaultValue;
use Propel\Generator\Model\Database;
-use Propel\Generator\Model\Table;
+use Propel\Generator\Model\ForeignKey;
+use Propel\Generator\Model\Index;
use Propel\Generator\Model\PropelTypes;
+use Propel\Generator\Model\Table;
+use Propel\Generator\Model\Unique;
use Propel\Generator\Reverse\AbstractSchemaParser;
+use PDO;
+
/**
* Postgresql database schema parser.
*
@@ -132,7 +137,7 @@ public function parse(Database $database, Task $task = null)
$database->addTable($table);
// Create a wrapper to hold these tables and their associated OID
- $wrap = new stdClass;
+ $wrap = new \stdClass;
$wrap->table = $table;
$wrap->oid = $oid;
$tableWraps[] = $wrap;
View
25 tests/Fixtures/reverse/pgsql/runtime-conf.xml.dist
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<config>
+ <propel>
+ <datasources default="reverse-bookstore">
+ <datasource id="reverse-bookstore">
+ <adapter>##DATABASE_VENDOR##</adapter>
+ <connection>
+ <classname>\Propel\Runtime\Connection\DebugPDO</classname>
+ <dsn>##DATABASE_URL##</dsn>
+ <user>##DATABASE_USER##</user>
+ <password>##DATABASE_PASSWORD##</password>
+ <options>
+ <option id="ATTR_PERSISTENT">false</option>
+ </options>
+ <attributes>
+ <option id="ATTR_EMULATE_PREPARES">true</option>
+ </attributes>
+ <settings>
+ <setting id="charset">utf8</setting>
+ </settings>
+ </connection>
+ </datasource>
+ </datasources>
+ </propel>
+</config>
View
104 tests/Propel/Tests/Generator/Reverse/PgsqlSchemaParserTest.php
@@ -0,0 +1,104 @@
+<?php
+
+/**
+ * This file is part of the Propel package.
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ *
+ * @license MIT License
+ */
+
+namespace Propel\Tests\Generator\Reverse;
+
+use Propel\Generator\Config\QuickGeneratorConfig;
+use Propel\Generator\Manager\ConfigManager;
+use Propel\Generator\Model\ColumnDefaultValue;
+use Propel\Generator\Model\Database;
+use Propel\Generator\Model\PropelTypes;
+use Propel\Generator\Platform\DefaultPlatform;
+use Propel\Generator\Reverse\PgsqlSchemaParser;
+use Propel\Runtime\Propel;
+use Propel\Tests\TestCase;
+
+use \DOMDocument;
+
+/**
+ * Tests for Pgsql database schema parser.
+ *
+ * @author Alan Pinstein
+ */
+class PgsqlSchemaParserTest extends TestCase
+{
+ protected function setUp()
+ {
+ parent::setUp();
+
+ $xmlDom = new DOMDocument();
+ $xmlDom->load(__DIR__ . '/../../../../Fixtures/reverse/pgsql/runtime-conf.xml');
+ $xml = simplexml_load_string($xmlDom->saveXML());
+ $phpconf = TestableConfigManager::simpleXmlToArray($xml);
+
+ Propel::setConfiguration($phpconf['propel']);
+
+ $this->con = Propel::getConnection('reverse-bookstore');
+ $this->con->beginTransaction();
+
+ if ('pgsql' !== $this->con->getAttribute(\PDO::ATTR_DRIVER_NAME)) {
+ $this->markTestSkipped('This test is designed for PostgreSQL');
+ }
+ }
+
+ protected function tearDown()
+ {
+ $this->con->rollback();
+
+ parent::tearDown();
+ Propel::init(__DIR__ . '/../../../../Fixtures/bookstore/build/conf/bookstore-conf.php');
+ }
+
+ function parseDataProvider()
+ {
+ return array(
+ // columnDDL, expectedColumnPhpName, expectedColumnDefaultType, expectedColumnDefaultValue
+ array("my_column varchar(20) default null", "MyColumn", ColumnDefaultValue::TYPE_VALUE, "NULL"),
+ array("my_column varchar(20) default ''", "MyColumn", ColumnDefaultValue::TYPE_VALUE, ""),
+ );
+ }
+
+ /**
+ * @dataProvider parseDataProvider
+ */
+ public function testParse($columnDDL, $expectedColumnPhpName, $expectedColumnDefaultType, $expectedColumnDefaultValue)
+ {
+ $this->markTestSkipped('Skipped as we now use one database for the whole test suite');
+
+ $this->con->query("create table foo ( {$columnDDL} );");
+ $parser = new PgsqlSchemaParser($this->con);
+ $parser->setGeneratorConfig(new QuickGeneratorConfig());
+
+ $database = new Database();
+ $database->setPlatform(new DefaultPlatform());
+
+ // make sure our DDL insert produced exactly the SQL we inserted
+ $this->assertEquals(1, $parser->parse($database), 'One table and one view defined should return one as we exclude views');
+ $tables = $database->getTables();
+ $this->assertEquals(1, count($tables));
+ $table = $tables[0];
+ $columns = $table->getColumns();
+ $this->assertEquals(1, count($columns));
+
+ // check out our rev-eng column info
+ $defaultValue = $columns[0]->getDefaultValue();
+ $this->assertEquals($expectedColumnPhpName, $columns[0]->getPhpName());
+ $this->assertEquals($expectedColumnDefaultType, $defaultValue->getType());
+ $this->assertEquals($expectedColumnDefaultValue, $defaultValue->getValue());
+ }
+}
+
+class TestableConfigManager extends ConfigManager
+{
+ public static function simpleXmlToArray($xml)
+ {
+ return parent::simpleXmlToArray($xml);
+ }
+}

0 comments on commit 08be56a

Please sign in to comment.
Something went wrong with that request. Please try again.