Author: ts
Date: Wed Jan 30 13:12:03 2008
New Revision: 7259

Log:
- Started test suite for database tests of enhancement #10151.

Added:
    trunk/PersistentObject/tests/data/database_type.dba
    trunk/PersistentObject/tests/data/database_type.sql
    trunk/PersistentObject/tests/data/database_type_test_object.php   (with 
props)
    trunk/PersistentObject/tests/data/databasetypetestobject.php   (with props)
    trunk/PersistentObject/tests/database_type_test.php   (with props)
Modified:
    trunk/PersistentObject/src/object/persistent_object_property.php

Modified: trunk/PersistentObject/src/object/persistent_object_property.php
==============================================================================
--- trunk/PersistentObject/src/object/persistent_object_property.php 
[iso-8859-1] (original)
+++ trunk/PersistentObject/src/object/persistent_object_property.php 
[iso-8859-1] Wed Jan 30 13:12:03 2008
@@ -191,7 +191,7 @@
                 }
                 break;
             case 'databaseType':
-                if ( $propertyValue !== PDO::PARAM_STR && $propertyValue !== 
PDO::PARAM_LOB && $propertyValue !== PDO::PARAM_INT && PDO::PARAM_BOOL )
+                if ( $propertyValue !== PDO::PARAM_STR && $propertyValue !== 
PDO::PARAM_LOB && $propertyValue !== PDO::PARAM_INT && $propertyValue !== 
PDO::PARAM_BOOL )
                 {
                     throw new ezcBaseValueException(
                         $propertyName,

Added: trunk/PersistentObject/tests/data/database_type.dba
==============================================================================
--- trunk/PersistentObject/tests/data/database_type.dba (added)
+++ trunk/PersistentObject/tests/data/database_type.dba [iso-8859-1] Wed Jan 30 
13:12:03 2008
@@ -1,0 +1,74 @@
+<?php return array (
+  0 => 
+  array (
+    'PO_database_type_test' => 
+    ezcDbSchemaTable::__set_state(array(
+       'fields' => 
+      array (
+        'bool' => 
+        ezcDbSchemaField::__set_state(array(
+           'type' => 'boolean',
+           'length' => 0,
+           'notNull' => true,
+           'default' => NULL,
+           'autoIncrement' => false,
+           'unsigned' => false,
+        )),
+        'id' => 
+        ezcDbSchemaField::__set_state(array(
+           'type' => 'integer',
+           'length' => 0,
+           'notNull' => true,
+           'default' => NULL,
+           'autoIncrement' => true,
+           'unsigned' => false,
+        )),
+        'int' => 
+        ezcDbSchemaField::__set_state(array(
+           'type' => 'integer',
+           'length' => 0,
+           'notNull' => true,
+           'default' => NULL,
+           'autoIncrement' => false,
+           'unsigned' => false,
+        )),
+        'lob' => 
+        ezcDbSchemaField::__set_state(array(
+           'type' => 'blob',
+           'length' => 0,
+           'notNull' => true,
+           'default' => NULL,
+           'autoIncrement' => false,
+           'unsigned' => false,
+        )),
+        'str' => 
+        ezcDbSchemaField::__set_state(array(
+           'type' => 'text',
+           'length' => 100,
+           'notNull' => true,
+           'default' => NULL,
+           'autoIncrement' => false,
+           'unsigned' => false,
+        )),
+      ),
+       'indexes' => 
+      array (
+        'primary' => 
+        ezcDbSchemaIndex::__set_state(array(
+           'indexFields' => 
+          array (
+            'id' => 
+            ezcDbSchemaIndexField::__set_state(array(
+               'sorting' => NULL,
+            )),
+          ),
+           'primary' => true,
+           'unique' => true,
+        )),
+      ),
+    )),
+  ),
+  1 => 
+  array (
+  ),
+); ?>

Added: trunk/PersistentObject/tests/data/database_type.sql
==============================================================================
--- trunk/PersistentObject/tests/data/database_type.sql (added)
+++ trunk/PersistentObject/tests/data/database_type.sql [iso-8859-1] Wed Jan 30 
13:12:03 2008
@@ -1,0 +1,29 @@
+-- phpMyAdmin SQL Dump
+-- version 2.11.2
+-- http://www.phpmyadmin.net
+--
+-- Host: localhost
+-- Generation Time: Jan 28, 2008 at 05:12 PM
+-- Server version: 5.0.54
+-- PHP Version: 5.2.5RC2-dev
+
+SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
+
+--
+-- Database: `test`
+--
+
+-- --------------------------------------------------------
+
+--
+-- Table structure for table `PO_database_type_test`
+--
+
+CREATE TABLE IF NOT EXISTS `PO_database_type_test` (
+  `id` int(11) NOT NULL auto_increment,
+  `bool` tinyint(1) NOT NULL,
+  `int` int(11) NOT NULL,
+  `str` varchar(100) NOT NULL,
+  `lob` blob NOT NULL,
+  PRIMARY KEY  (`id`)
+) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

Added: trunk/PersistentObject/tests/data/database_type_test_object.php
==============================================================================
--- trunk/PersistentObject/tests/data/database_type_test_object.php (added)
+++ trunk/PersistentObject/tests/data/database_type_test_object.php 
[iso-8859-1] Wed Jan 30 13:12:03 2008
@@ -1,0 +1,107 @@
+<?php
+/**
+CREATE TABLE IF NOT EXISTS `PO_database_type_test` (
+  `id` int(11) NOT NULL auto_increment,
+  `bool` tinyint(1) NOT NULL,
+  `int` int(11) NOT NULL,
+  `str` varchar(100) NOT NULL,
+  `lob` blob NOT NULL,
+  PRIMARY KEY  (`id`)
+) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
+*/
+
+class DatabaseTypeTestObject
+{
+    public $id     = null;
+    public $bool   = null;
+    public $int    = null;
+    public $str    = null;
+    public $lob    = null;
+                
+    /**
+     * Inserts some data to use for testing.
+     */
+    public static function insertData()
+    {
+        $db = ezcDbInstance::get();
+        $stmt = $db->prepare(
+            "insert into " . $db->quoteIdentifier( "PO_database_type_test" ) . 
+            " (" .
+                $db->quoteIdentifier( "bool" ) . ", " .
+                $db->quoteIdentifier( "int" )  . ", " .
+                $db->quoteIdentifier( "str" )  . ", " .
+                $db->quoteIdentifier( "lob" )  . 
+            " ) VALUES ( :bool, :int, :str, :lob )"
+        );
+
+        $stmt->bindValue( ':bool', 1 );
+        $stmt->bindValue( ':int', 23 );
+        $stmt->bindValue( ':str', 'Non binary string' );
+        $stmt->bindValue( ':lob', "Binary \x00 string", PDO::PARAM_LOB );
+
+        $stmt->execute();
+
+        $stmt->bindValue( ':bool', 0 );
+        $stmt->bindValue( ':int', -42 );
+        $stmt->bindValue( ':str', "Binary \x00 string", PDO::PARAM_STR );
+        $stmt->bindValue( ':lob', "Binary \x00 string", PDO::PARAM_LOB );
+
+        $stmt->execute();
+    }
+
+    /**
+     * Saves the schema from database to file.
+     *
+     * Use this method if you have changed the definition of the persistent 
object
+     * and need to update the file on disk.
+     */
+    public function saveSchema()
+    {
+        $db = ezcDbInstance::get();
+        $schema = ezcDbSchema::createFromDb( $db );
+        $schema->writeToFile( 'array', dirname( __FILE__ ) . 
'/database_type.dba' );
+    }
+
+    /**
+     * Loads the schema from file into the database.
+     *
+     * If autoIncrement is set to false a schema with the id field not set to 
autoincrement is used.
+     */
+    public static function setupTables( $autoIncrement = true )
+    {
+        $db = ezcDbInstance::get();
+        $schema = ezcDbSchema::createFromFile( 'array', dirname( __FILE__ ) . 
'/database_type.dba' );
+        $schema->writeToDb( $db );
+    }
+
+    public static function cleanup()
+    {
+        $db = ezcDbInstance::get();
+        if ( $db->getName() == "oracle" )
+        {
+            $db->exec( "DROP SEQUENCE " . $db->quoteIdentifier( 
"PO_database_type_test_id_seq" ) );
+        }
+        $db->exec( "DROP TABLE " . $db->quoteIdentifier( 
"PO_database_type_test" ) );
+    }
+
+    public function setState( array $state )
+    {
+        foreach ( $state as $key => $value )
+        {
+            $this->$key = $value;
+        }
+    }
+
+    public function getState()
+    {
+        $result = array();
+        $result['id']   = $this->id;
+        $result['bool'] = $this->bool;
+        $result['int']  = $this->int;
+        $result['str']  = $this->str;
+        $result['lob']  = $this->lob;
+        return $result;
+    }
+}
+
+?>

Propchange: trunk/PersistentObject/tests/data/database_type_test_object.php
------------------------------------------------------------------------------
    svn:eol-style = native

Added: trunk/PersistentObject/tests/data/databasetypetestobject.php
==============================================================================
--- trunk/PersistentObject/tests/data/databasetypetestobject.php (added)
+++ trunk/PersistentObject/tests/data/databasetypetestobject.php [iso-8859-1] 
Wed Jan 30 13:12:03 2008
@@ -1,0 +1,47 @@
+<?php
+/*
+ * Holds the definition for MultiRelationTestPerson
+ * This definition is used by the code manager for
+ * various tests in the system.
+ */
+
+$def = new ezcPersistentObjectDefinition();
+$def->table = "PO_database_type_test";
+$def->class = "DatabaseTypeTestObject";
+
+$def->idProperty = new ezcPersistentObjectIdProperty;
+$def->idProperty->columnName = 'id';
+$def->idProperty->propertyName = 'id';
+$def->idProperty->generator = new ezcPersistentGeneratorDefinition(
+    'ezcPersistentSequenceGenerator',
+    array( 'sequence' => 'PO_database_type_test_id_seq' )
+);
+$def->idProperty->databaseType = PDO::PARAM_INT;
+
+$def->properties['bool'] = new ezcPersistentObjectProperty;
+$def->properties['bool']->columnName = 'bool';
+$def->properties['bool']->propertyName = 'bool';
+$def->properties['bool']->propertyType = 
ezcPersistentObjectProperty::PHP_TYPE_INT;
+$def->properties['bool']->databaseType = PDO::PARAM_BOOL;
+
+$def->properties['int'] = new ezcPersistentObjectProperty;
+$def->properties['int']->columnName = 'int';
+$def->properties['int']->propertyName = 'int';
+$def->properties['int']->propertyType = 
ezcPersistentObjectProperty::PHP_TYPE_INT;
+$def->properties['int']->databaseType = PDO::PARAM_INT;
+
+$def->properties['str'] = new ezcPersistentObjectProperty;
+$def->properties['str']->columnName = 'str';
+$def->properties['str']->propertyName = 'str';
+$def->properties['str']->propertyType = 
ezcPersistentObjectProperty::PHP_TYPE_STRING;
+$def->properties['str']->databaseType = PDO::PARAM_STR;
+
+$def->properties['lob'] = new ezcPersistentObjectProperty;
+$def->properties['lob']->columnName = 'lob';
+$def->properties['lob']->propertyName = 'lob';
+$def->properties['lob']->propertyType = 
ezcPersistentObjectProperty::PHP_TYPE_STRING;
+$def->properties['lob']->databaseType = PDO::PARAM_LOB;
+
+return $def;
+
+?>

Propchange: trunk/PersistentObject/tests/data/databasetypetestobject.php
------------------------------------------------------------------------------
    svn:eol-style = native

Added: trunk/PersistentObject/tests/database_type_test.php
==============================================================================
--- trunk/PersistentObject/tests/database_type_test.php (added)
+++ trunk/PersistentObject/tests/database_type_test.php [iso-8859-1] Wed Jan 30 
13:12:03 2008
@@ -1,0 +1,170 @@
+<?php
+/**
+ * @copyright Copyright (C) 2005-2008 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ * @version //autogentag//
+ * @filesource
+ * @package PersistentObject
+ * @subpackage Tests
+ */
+
+require_once dirname( __FILE__ ) . "/data/database_type_test_object.php";
+
+/**
+ * Tests ezcPersistentManyToManyRelation class.
+ *
+ * @package PersistentObject
+ * @subpackage Tests
+ */
+class ezcPersistentDatabaseTypeTest extends ezcTestCase
+{
+
+    private $session;
+
+    public static function suite()
+    {
+        return new PHPUnit_Framework_TestSuite( __CLASS__ );
+    }
+
+    public function setup()
+    {
+        try
+        {
+            $this->db = ezcDbInstance::get();
+        }
+        catch ( Exception $e )
+        {
+            $this->markTestSkipped( 'There was no database configured' );
+        }
+        DatabaseTypeTestObject::setupTables();
+        DatabaseTypeTestObject::insertData();
+        $this->session = new ezcPersistentSession(
+            ezcDbInstance::get(),
+            new ezcPersistentCodeManager( dirname( __FILE__ ) . "/data/" )
+        );
+    }
+
+    public function teardown()
+    {
+        // DatabaseTypeTestObject::cleanup();
+    }
+
+    public function testLoadCorrectMysqlSqlite()
+    {
+        if ( $this->session->database->getName() !== 'mysql' && 
$this->session->database->getName() !== 'sqlite' )
+        {
+            $this->markTestSkipped( 'Will only be run with MySQL and SQLite.' 
);
+        }
+        $obj = $this->session->load( 'DatabaseTypeTestObject', 1 );
+
+        $this->assertSame(
+            '1',
+            $obj->id
+        );
+        $this->assertSame(
+            '23',
+            $obj->int
+        );
+        $this->assertSame(
+            'Non binary string',
+            $obj->str
+        );
+        $this->assertSame(
+            "Binary \x00 string",
+            $obj->lob
+        );
+    }
+
+    public function testLoadCorrectPostgres()
+    {
+        if ( $this->session->database->getName() !== 'pgsql' )
+        {
+            $this->markTestSkipped( 'Will only be run with PostgreSQL.' );
+        }
+        $obj = $this->session->load( 'DatabaseTypeTestObject', 1 );
+
+        $this->assertSame(
+            '1',
+            $obj->id
+        );
+        $this->assertSame(
+            '23',
+            $obj->int
+        );
+        $this->assertSame(
+            'Non binary string',
+            $obj->str
+        );
+        $this->assertTrue(
+            is_resource( $obj->lob ),
+            'Postgre did not return a resource for a BLOB field.'
+        );
+    }
+
+    public function testLoadIncorrectMysqlSqlite()
+    {
+        if ( $this->session->database->getName() !== 'mysql' && 
$this->session->database->getName() !== 'sqlite' )
+        {
+            $this->markTestSkipped( 'Will only be run with MySQL and SQLite.' 
);
+        }
+        $obj = $this->session->load( 'DatabaseTypeTestObject', 2 );
+
+        $this->assertEquals(
+            '2',
+            $obj->id
+        );
+        $this->assertEquals(
+            '0',
+            $obj->bool
+        );
+        $this->assertEquals(
+            '-42',
+            $obj->int
+        );
+        // Works in MySQL and SQLite with non-blobs, too
+        $this->assertEquals(
+            "Binary \x00 string",
+            $obj->str,
+            'Binary string not returned completly from text field.'
+        );
+        $this->assertEquals(
+            "Binary \x00 string",
+            $obj->lob,
+            'Binary string not returned completly from BLOB field.'
+        );
+    }
+
+    public function testLoadIncorrectPostgres()
+    {
+        if ( $this->session->database->getName() !== 'pgsql' )
+        {
+            $this->markTestSkipped( 'Will only be run with PostgreSQL.' );
+        }
+        $obj = $this->session->load( 'DatabaseTypeTestObject', 2 );
+
+        $this->assertSame(
+            2,
+            $obj->id
+        );
+        $this->assertSame(
+            false,
+            $obj->bool
+        );
+        $this->assertSame(
+            '-42',
+            $obj->int
+        );
+        // String is cut after the null char
+        $this->assertSame(
+            "Binary ",
+            $obj->str,
+            'Binary string not cut at null-char in Postgres text field.'
+        );
+        $this->assertTrue(
+            is_resource( $obj->lob ),
+            'Postgre did not return a resource for a BLOB field.'
+        );
+    }
+}
+
+?>

Propchange: trunk/PersistentObject/tests/database_type_test.php
------------------------------------------------------------------------------
    svn:eol-style = native


-- 
svn-components mailing list
svn-components@lists.ez.no
http://lists.ez.no/mailman/listinfo/svn-components

Reply via email to