Commit 8c294aba authored by jhassine's avatar jhassine

* Added some test cases for the importing part. The new tests will fail for...

* Added some test cases for the importing part. The new tests will fail for now because of the lacking implementation.
Refs #11 
parent fd3bffc7
......@@ -109,6 +109,23 @@ class Doctrine_DataDict {
throw new Doctrine_Exception("Unknown column type $type");
endswitch;
}
/**
* Converts native database column type to doctrine data type
*
* @param string $column column type
* @param integer $length column length
* @param string $dbType Database driver name as returned by PDO::getAttribute(PDO::ATTR_DRIVER_NAME)
* @param string $dbVersion Database server version as return by PDO::getAttribute(PDO::ATTR_SERVER_VERSION)
* @return array of doctrine column type and column length. In future may also return a validator name.
* @throws Doctrine_Exception on unknown column type
* @author Jukka Hassinen <Jukka.Hassinen@BrainAlliance.com>
*/
public static function getDoctrineType($colType,$colLength, $dbType = null, $dbVersion = null)
{
return array($colType, $colLength); /* @todo FIXME i am incomplete*/
}
/**
* checks for valid class name (uses camel case and underscores)
*
......@@ -121,4 +138,4 @@ class Doctrine_DataDict {
return true;
}
}
?>
<?php
/*
* $Id$
*
* 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.phpdoctrine.com>.
*/
/**
* @package Doctrine
* @url http://www.phpdoctrine.com
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @author Jukka Hassinen <Jukka.Hassinen@BrainAlliance.com>
* @version $Id$
*/
/**
* class Doctrine_Import
* Main responsible of performing import operation. Delegates database schema
* reading to a reader object and passes the result to a builder object which
* builds a Doctrine data model.
*/
class Doctrine_Import
{
/** Aggregations: */
/** Compositions: */
/*** Attributes: ***/
/**
* @access private
*/
private $reader;
/**
* @access private
*/
private $builder;
/**
*
* @return
* @access public
*/
public function import( ) {
} // end of member function import
/**
*
* @param Doctrine_Import_Reader reader * @return
* @access public
*/
public function setReader( $reader ) {
} // end of member function setReader
/**
*
* @param Doctrine_Import_Builder builder * @return
* @access public
*/
public function setBuilder( $builder ) {
} // end of member function setBuilder
} // end of Doctrine_Import
?>
<?php
/*
* $Id$
*
* 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.phpdoctrine.com>.
*/
/**
* @package Doctrine
* @url http://www.phpdoctrine.com
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @author Jukka Hassinen <Jukka.Hassinen@BrainAlliance.com>
* @version $Id$
*/
/**
* class Doctrine_Import_Builder
* Is responsible of building Doctrine structure based on a database schema.
*/
abstract class Doctrine_Import_Builder
{
/** Aggregations: */
/** Compositions: */
/*** Attributes: ***/
/**
*
* @param Doctrine_Schema schema
* @return
* @abstract
* @access public
*/
abstract public function build(Doctrine_Schema $schema );
} // end of Doctrine_Import_Builder
?>
<?php
/*
* $Id$
*
* 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.phpdoctrine.com>.
*/
/**
* @package Doctrine
* @url http://www.phpdoctrine.com
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @author Jukka Hassinen <Jukka.Hassinen@BrainAlliance.com>
* @version $Id$
*/
/**
* class Doctrine_Import_Builder_BaseClass
* Builds a Doctrine_Record base class definition based on a schema.
*/
class Doctrine_Import_Builder_BaseClass extends Doctrine_Import_Builder
{
/** Aggregations: */
/** Compositions: */
/*** Attributes: ***/
private $path = '';
private $suffix = '.php';
/**
*
* @param string path
* @return
* @access public
*/
public function setOutputPath( $path ) {
$this->path = $path;
} // end of member function setOuputPath
/**
*
* @param string path
* @return
* @access public
*/
public function setFileSuffix( $suffix ) {
$this->suffix = $suffix;
} // end of member function setOuputPath
/**
*
* @param Doctrine_Schema schema
* @return
* @access public
* @throws Doctrine_Import_Exception
*/
public function build(Doctrine_Schema $schema )
{
/* @todo FIXME i am incomplete*/
}
} // end of Doctrine_Import_Builder_BaseClass
<?php
/*
* $Id$
*
* 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.phpdoctrine.com>.
*/
/**
* @package Doctrine
* @url http://www.phpdoctrine.com
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @author Jukka Hassinen <Jukka.Hassinen@BrainAlliance.com>
* @version $Id$
*/
/**
* class Doctrine_Import_Builder_Exception
*/
class Doctrine_Import_Builder_Exception
{
/** Aggregations: */
/** Compositions: */
/*** Attributes: ***/
} // end of Doctrine_Import_Builder_Exception
?>
<?php
/*
* $Id$
*
* 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.phpdoctrine.com>.
*/
/**
* @package Doctrine
* @url http://www.phpdoctrine.com
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @author Jukka Hassinen <Jukka.Hassinen@BrainAlliance.com>
* @version $Id$
*/
/**
* class Doctrine_Import_Reader
* Is responsible of reading a database definitions from a source and costructing a
* database schema
*/
abstract class Doctrine_Import_Reader
{
/** Aggregations: */
/** Compositions: */
/*** Attributes: ***/
/**
*
* @return Doctrine_Schema
* @abstract
* @access public
*/
abstract public function read( );
} // end of Doctrine_Import_Reader
?>
<?php
/*
* $Id$
*
* 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.phpdoctrine.com>.
*/
/**
* @package Doctrine
* @url http://www.phpdoctrine.com
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @author Jukka Hassinen <Jukka.Hassinen@BrainAlliance.com>
* @version $Id$
*/
/**
* class Doctrine_Import_Reader_Db
* Reads a database using the given PDO connection and constructs a database
* schema
*/
class Doctrine_Import_Reader_Db extends Doctrine_Import_Reader
{
/** Aggregations: */
/** Compositions: */
/*** Attributes: ***/
/**
* @access private
*/
private $pdo;
/**
*
* @param object pdo * @return
* @access public
*/
public function setPdo( $pdo ) {
} // end of member function setPdo
/**
*
* @return Doctrine_Schema
* @access public
*/
public function read( )
{
return new Doctrine_Schema(); /* @todo FIXME i am incomplete*/
}
} // end of Doctrine_Import_Reader_Db
?>
<?php
/*
* $Id$
*
* 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.phpdoctrine.com>.
*/
/**
* @package Doctrine
* @url http://www.phpdoctrine.com
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @author Jukka Hassinen <Jukka.Hassinen@BrainAlliance.com>
* @version $Id$
*/
/**
* class Doctrine_Import_Reader_Exception
*/
class Doctrine_Import_Reader_Exception
{
/** Aggregations: */
/** Compositions: */
/*** Attributes: ***/
} // end of Doctrine_Import_Reader_Exception
?>
<?php
/*
* $Id$
*
* 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.phpdoctrine.com>.
*/
/**
* @package Doctrine
* @url http://www.phpdoctrine.com
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @author Jukka Hassinen <Jukka.Hassinen@BrainAlliance.com>
* @version $Id$
*/
/**
* class Doctrine_Import_Reader_Xml_Propel
*/
class Doctrine_Import_Reader_Xml_Propel extends Doctrine_Import_Reader
{
/** Aggregations: */
/** Compositions: */
/*** Attributes: ***/
/**
* @access private
*/
private $xml;
/**
*
* @param string xml * @return
* @access public
*/
public function setXml( $xml ) {
} // end of member function setXml
} // end of Doctrine_Import_Reader_Xml_Propel
?>
......@@ -82,4 +82,4 @@ class Doctrine_Schema extends Doctrine_Schema_Object
} // end of Doctrine_Schema
?>
......@@ -128,4 +128,4 @@ class Doctrine_Schema_Database extends Doctrine_Schema_Object
} // end of Doctrine_Schema_Database
?>
......@@ -134,4 +134,4 @@ class Doctrine_Schema_Table extends Doctrine_Schema_Object
} // end of Doctrine_Schema_Table
?>
<?php
/**
* ImportTestCase.php - 24.8.2006 2.37.14
*
* Note that some shortcuts maybe used here
* i.e. these tests depends that exporting is working
*
* @author Jukka Hassinen <Jukka.Hassinen@BrainAlliance.com>
* @version $Id$
* @package doctrine
* @package Doctrine
*/
class Doctrine_ImportTestCase extends Doctrine_UnitTestCase
{
function testDatabaseConnectionIsReverseEngineeredToSchema()
private $tmpdir;
private $suffix;
private $schema;
public function setUp()
{
parent::setUp();
//reading
$reader = new Doctrine_Import_Reader_Db();
$reader->setPdo($this->dbh);
$this->schema = $reader->read();
//and building
$this->tmpdir = $this->getTempDir();
$this->suffix = '__Base';
$builder = new Doctrine_Import_Builder_BaseClass();
$builder->setOutputPath($this->tmpdir);
$builder->setFileSuffix($this->suffix.'.php');
$builder->build($this->schema);
}
public function testDatabaseConnectionIsReverseEngineeredToSchema()
{
$this->assertTrue($this->schema instanceof Doctrine_Schema);
//table count should match
$this->assertEqual(count($this->schema), count($this->tables));
}
function testNativeColumnDefinitionsAreTranslatedCorrectly()
{
public function testBaseClassesAreWritten()
{
//now lets match the original with the result
foreach($this->tables as $name)
{
$name = ucwords($name);
$filename = $this->tmpdir.$name.$this->suffix.'.php';
$this->assertTrue(file_exists($filename));
}
}
public function testNativeColumnDefinitionsAreTranslatedCorrectly()
{
$transArr = array();
/* From SQLite column types */
$transArr['sqlite'] = array(
/* array(native type, native length, doctrine type, doctrine length), */
array('int', 11, 'int', 11),
//array('varchar', 255, 'string', 255),
);
foreach ($transArr as $dbType => $colArr)
{
foreach($colArr as $colDef)
{
list($natType, $natLen, $expType, $expLen) = $colDef;
list($resType, $resLen) = Doctrine_DataDict::getDoctrineType($natType, $natLen, $dbType);
$this->assertEqual($resType, $expType);
$this->assertEqual($resLen, $expLen);
}
}
}
function testDoctrineRecordBaseClassesAreBuildCorrectlyAndToPath()
public function testDoctrineRecordBaseClassesAreBuildCorrectly()
{
foreach($this->tables as $name)
{
$name = ucwords($name);
$filename = $this->tmpdir.$name.$this->suffix.'.php';
if(file_exists($filename))
{
require_once $filename;
$obj = new $name.$this->suffix;
list($oType, $oLength,) = $this->connection->getTable($name)->getColumns();
list($rType, $rLength,) = $this->connection->getTable($name.$this->suffix)->getColumns();
$this->assertEquals($rType, $oType);
$this->assertEquals($rLength, $oLength);
}
}
}
public function tearDown()
{
@unlink($this->tmpdir);
@rmdir($this->tmpdir);
}
/**
* Gets the system temporary directory name
* @return null on failure to resolve the system temp dir
*/
private function getTempDir()
{
if(function_exists('sys_get_temp_dir')) {
$tempdir = sys_get_temp_dir();
} elseif (!empty($_ENV['TMP'])) {
$tempdir = $_ENV['TMP'];
} elseif (!empty($_ENV['TMPDIR'])) {
$tempdir = $_ENV['TMPDIR'];
} elseif (!empty($_ENV['TEMP'])) {
$tempdir = $_ENV['TEMP'];
} else {
//a little bit of chewing gum here will do the trick
$tempdir = dirname(tempnam('/THIS_REALLY_SHOULD_NOT_EXISTS', 'na'));
}
if (empty($tempdir)) { return null; }
$tempdir = rtrim($tempdir, '/');
$tempdir .= DIRECTORY_SEPARATOR;
if (is_writable($tempdir) == false) {
return null;
}
$dir = tempnam($tempdir, 'doctrine_tests');
@unlink($dir);
@rmdir($dir);
mkdir($dir);
$dir .= DIRECTORY_SEPARATOR;
return $dir;
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment