Files
DomFramework/Tests/DblayerComplet.php

392 lines
16 KiB
PHP

<?php
/** DomFramework - Tests
* @package domframework
* @author Dominique Fournier <dominique@fournier38.fr>
* @license BSD
*/
namespace Domframework\Tests;
class dblayerTest{ENGINE} extends PHPUnit_Framework_TestCase
{
// Test with column name 'group', 'object', 'where', 'with space'
// Test with table name 'group', 'object', 'where', 'with space'
// For the 3 DB engines
public $engine="{ENGINE}";
public $confs = array (
"sqlite" => array (
"dsn" => "sqlite:/tmp/databaseDBLayer.db",
"username" => null,
"password" => null,
"driver_options" => null,
"tableprefix" => "",
),
"mysql" => array (
"dsn" => "mysql:host=127.0.0.1;port=3306;dbname=test",
"username" => "root",
"password" => "lqsym",
"driver_options" => null,
"tableprefix" => "",
),
"pgsql" => array (
"dsn" => "pgsql:host=127.0.0.1;port=5432;dbname=dbname",
"username" => "root",
"password" => "root",
"driver_options" => null,
"tableprefix" => "",
),
);
/** @group singleton */
public function test_dropTable ()
{
$dbconfig = $this->confs["{ENGINE}"];
$db = new dblayer ($dbconfig["dsn"], $dbconfig["username"],
$dbconfig["password"], $dbconfig["driver_options"]);
foreach (array ("users", "grouped", "multiple", "multiple2", "users3",
"readOR") as
$table)
{
$db->table = $table;
try
{
$res = $db->dropTable();
}
catch (Exception $e)
{
}
}
$db->disconnect ();
// Never generate an error, just drop the table if it exists, and do noting
// if it doesn't exists
}
/** @group singleton */
public function test_createTable1 ()
{
// Create a table named grouped
$dbconfig = $this->confs["{ENGINE}"];
$db = new dblayer ($dbconfig["dsn"], $dbconfig["username"],
$dbconfig["password"], $dbconfig["driver_options"]);
$db->table = "grouped";
$db->fields = array ("group"=>array ("varchar", "255", "not null"),
"object"=>array ("varchar", "255", "not null"),
"where"=>array ("varchar", "255", "not null"),
"with space"=>array ("varchar", "255", "not null"));
$db->unique = array ();
$db->primary = "group";
$res = $db->createTable ();
$db->disconnect ();
$this->assertSame (0, $res);
}
/** @group singleton */
public function test_insert1 ()
{
$dbconfig = $this->confs["{ENGINE}"];
$db = new dblayer ($dbconfig["dsn"], $dbconfig["username"],
$dbconfig["password"], $dbconfig["driver_options"]);
$db->table = "grouped";
$db->fields = array ("group"=>array ("varchar", "255", "not null"),
"object"=>array ("varchar", "255", "not null"),
"where"=>array ("varchar", "255", "not null"),
"with space"=>array ("varchar", "255", "not null"));
$db->unique = array ();
$db->primary = "group";
$res = $db->insert (array ("group"=>"gr ou\"p",
"object"=>"/éobj%",
"where"=>"\$'\"",
"with space"=>"with space"));
// SQLite start at 1, MySQL start at 0...
$this->assertSame ($res, "gr ou\"p");
}
public function test_read1 ()
{
$dbconfig = $this->confs["{ENGINE}"];
$db = new dblayer ($dbconfig["dsn"], $dbconfig["username"],
$dbconfig["password"], $dbconfig["driver_options"]);
$db->table = "grouped";
$db->fields = array ("group"=>array ("varchar", "255", "not null"),
"object"=>array ("varchar", "255", "not null"),
"where"=>array ("varchar", "255", "not null"),
"with space"=>array ("varchar", "255", "not null"));
$db->unique = array ();
$res = $db->read (array (array ("group", "gr ou\"p"),
array ("object","/éobj%"),
array ("where","\$'\""),
array ("with space","with space")));
$this->assertSame (array (0=>array ("group"=>"gr ou\"p",
"object"=>"/éobj%",
"where"=>"\$'\"",
"with space"=>"with space")), $res);
}
public function test_update1 ()
{
$dbconfig = $this->confs["{ENGINE}"];
$db = new dblayer ($dbconfig["dsn"], $dbconfig["username"],
$dbconfig["password"], $dbconfig["driver_options"]);
$db->table = "grouped";
$db->fields = array ("group"=>array ("varchar", "255", "not null"),
"object"=>array ("varchar", "255", "not null"),
"where"=>array ("varchar", "255", "not null"),
"with space"=>array ("varchar", "255", "not null"));
$db->unique = array ();
$db->primary = "group";
// Don't update primary key
$res = $db->update ("gr ou\"p", array ("object"=>"%éàoppp",
"with space"=>"WITH SPACE"));
$this->assertSame (1, $res);
}
public function test_read2 ()
{
$dbconfig = $this->confs["{ENGINE}"];
$db = new dblayer ($dbconfig["dsn"], $dbconfig["username"],
$dbconfig["password"], $dbconfig["driver_options"]);
$db->table = "grouped";
$db->fields = array ("group"=>array ("varchar", "255", "not null"),
"object"=>array ("varchar", "255", "not null"),
"where"=>array ("varchar", "255", "not null"),
"with space"=>array ("varchar", "255", "not null"));
$db->unique = array ();
$res = $db->read (array (array ("group", "gr ou\"p"),
array ("object","%éàoppp"),
array ("where","\$'\""),
array ("with space","WITH SPACE")));
$this->assertSame (array (0=>array ("group"=>"gr ou\"p",
"object"=>"%éàoppp",
"where"=>"\$'\"",
"with space"=>"WITH SPACE")), $res);
}
public function test_update2 ()
{
$dbconfig = $this->confs["{ENGINE}"];
$db = new dblayer ($dbconfig["dsn"], $dbconfig["username"],
$dbconfig["password"], $dbconfig["driver_options"]);
$db->table = "grouped";
$db->fields = array ("group"=>array ("varchar", "255", "not null"),
"object"=>array ("varchar", "255", "not null"),
"where"=>array ("varchar", "255", "not null"),
"with space"=>array ("varchar", "255", "not null"));
$db->unique = array (array ("group","object"));
$db->primary = "group";
// Update primary key
$res = $db->update ("gr ou\"p", array ("group"=>"NEW GROUP",
"object"=>"%éàoppp",
"with space"=>"WITH SPACE"));
$this->assertSame (1, $res);
}
public function test_read3 ()
{
$dbconfig = $this->confs["{ENGINE}"];
$db = new dblayer ($dbconfig["dsn"], $dbconfig["username"],
$dbconfig["password"], $dbconfig["driver_options"]);
$db->table = "grouped";
$db->fields = array ("group"=>array ("varchar", "255", "not null"),
"object"=>array ("varchar", "255", "not null"),
"where"=>array ("varchar", "255", "not null"),
"with space"=>array ("varchar", "255", "not null"));
$db->unique = array ();
$res = $db->read (array (array ("group", "NEW GROUP"),
array ("object","%éàoppp"),
array ("where","\$'\""),
array ("with space","WITH SPACE")));
$this->assertSame (array (0=>array ("group"=>"NEW GROUP",
"object"=>"%éàoppp",
"where"=>"\$'\"",
"with space"=>"WITH SPACE")), $res);
}
public function test_update3 ()
{
$dbconfig = $this->confs["{ENGINE}"];
$db = new dblayer ($dbconfig["dsn"], $dbconfig["username"],
$dbconfig["password"], $dbconfig["driver_options"]);
$db->table = "grouped";
$db->fields = array ("group"=>array ("varchar", "255", "not null"),
"object"=>array ("varchar", "255", "not null"),
"where"=>array ("varchar", "255", "not null"),
"with space"=>array ("varchar", "255", "not null"));
$db->unique = array ("group");
$db->primary = "group";
// Update primary key with primary key in unique with same values to test if
// the exception is NOT raised
$res = $db->update ("NEW GROUP", array ("group"=>"NEW GROUP",
"object"=>"%éàoppp",
"with space"=>"WITH SPACE"));
// SQLite and PostgreSQL return 1 line modified
// MySQL return 0 by default because there is no modification on the line
// http://fr2.php.net/manual/en/pdostatement.rowcount.php#104930
// Now, the MYSQL_ATTR_FOUND_ROWS is added to connection to be the same in
// all the engines
$this->assertSame (1, $res);
}
/** @group singleton */
// Part to test the foreign keys
public function test_createTable2 ()
{
// Create a table named group
$dbconfig = $this->confs["{ENGINE}"];
$db = new dblayer ($dbconfig["dsn"], $dbconfig["username"],
$dbconfig["password"], $dbconfig["driver_options"]);
$db->disconnect ();
$db = new dblayer ($dbconfig["dsn"], $dbconfig["username"],
$dbconfig["password"], $dbconfig["driver_options"]);
$db->table = "users";
$db->fields = array ("user"=>array ("varchar", "255", "not null"),
"groupmember"=>array ("varchar", "255", "not null"),
"where"=>array ("varchar", "255", "not null"),
"with space"=>array ("varchar", "255", "not null"));
$db->foreign = array (
"groupmember"=>array ("grouped", "group", "ON UPDATE CASCADE ON DELETE CASCADE"),
);
$res = $db->createTable ();
$this->assertSame (0, $res);
}
public function test_insert2 ()
{
$dbconfig = $this->confs["{ENGINE}"];
$db = new dblayer ($dbconfig["dsn"], $dbconfig["username"],
$dbconfig["password"], $dbconfig["driver_options"]);
$db->table = "users";
$db->fields = array ("user"=>array ("varchar", "255", "not null"),
"groupmember"=>array ("varchar", "255", "not null"),
"where"=>array ("varchar", "255", "not null"),
"with space"=>array ("varchar", "255", "not null"));
$db->unique = array ("user");
$db->primary = "user";
$db->foreign = array (
"groupmember"=>array ("grouped", "group", "ON UPDATE CASCADE ON DELETE CASCADE"),
);
$res = $db->insert (array ("user"=>"Us ou\"r",
"groupmember"=>"NEW GROUP",
"where"=>"\$'\"",
"with space"=>"with space"));
$db->disconnect ();
// SQLite start at 1, MySQL start at 0...
$this->assertLessThanOrEqual ($res, 2);
}
// Test the unique feature
public function test_insert3 ()
{
$dbconfig = $this->confs["{ENGINE}"];
$db = new dblayer ($dbconfig["dsn"], $dbconfig["username"],
$dbconfig["password"], $dbconfig["driver_options"]);
$db->table = "users";
$db->fields = array ("user"=>array ("varchar", "255", "not null"),
"groupmember"=>array ("varchar", "255", "not null"),
"where"=>array ("varchar", "255", "not null"),
"with space"=>array ("varchar", "255", "not null"));
// Unique simple in insert
$db->unique = array ("user");
$db->primary = "user";
$db->foreign = array (
"groupmember"=>array ("grouped", "group", "ON UPDATE CASCADE ON DELETE CASCADE"),
);
$this->setExpectedException ("Exception");
$res = $db->insert (array ("user"=>"Us ou\"r",
"groupmember"=>"NEW GROUP",
"where"=>"\$'\"",
"with space"=>"with space"));
}
public function test_insert4 ()
{
$dbconfig = $this->confs["{ENGINE}"];
$db = new dblayer ($dbconfig["dsn"], $dbconfig["username"],
$dbconfig["password"], $dbconfig["driver_options"]);
$db->table = "users";
$db->fields = array ("user"=>array ("varchar", "255", "not null"),
"groupmember"=>array ("varchar", "255", "not null"),
"where"=>array ("varchar", "255", "not null"),
"with space"=>array ("varchar", "255", "not null"));
// Unique multiple in insert
$db->unique = array (array ("user", "groupmember"));
$db->primary = "user";
$db->foreign = array (
"groupmember"=>array ("grouped", "group", "ON UPDATE CASCADE ON DELETE CASCADE"),
);
$this->setExpectedException ("Exception");
$res = $db->insert (array ("user"=>"Us ou\"r",
"groupmember"=>"NEW GROUP",
"where"=>"\$'\"",
"with space"=>"with space"));
}
/** @group singleton */
// Test multiple actions in one single connection
public function test_multiple1 ()
{
// Create a table named group
$dbconfig = $this->confs["{ENGINE}"];
$db = new dblayer ($dbconfig["dsn"], $dbconfig["username"],
$dbconfig["password"], $dbconfig["driver_options"]);
$db->table = "multiple";
$db->fields = array ("user"=>array ("varchar", "255", "not null"),
"groupmember"=>array ("varchar", "255", "not null"),
"where"=>array ("varchar", "255", "not null"),
"with space"=>array ("varchar", "255", "not null"));
$db->createTable ();
$db2 = new dblayer ($dbconfig["dsn"], $dbconfig["username"],
$dbconfig["password"], $dbconfig["driver_options"]);
$db2->table = "multiple2";
$db2->fields = array ("user"=>array ("varchar", "255", "not null"),
"groupmember"=>array ("varchar", "255", "not null"),
"where"=>array ("varchar", "255", "not null"),
"with space"=>array ("varchar", "255", "not null"));
$db2->createTable ();
$res = $db2->read (array (array ("user", "toto")));
$res = $db->read (array (array ("user", "toto")));
$db->disconnect ();
$this->assertSame (array (), $res);
}
/** @group singleton */
public function test_createTable3 ()
{
// Create a table named group
$dbconfig = $this->confs["{ENGINE}"];
$db = new dblayer ($dbconfig["dsn"], $dbconfig["username"],
$dbconfig["password"], $dbconfig["driver_options"]);
$db->table = "users3";
$db->fields = array ("user"=>array ("varchar", "255", "not null"),
"groupmember"=>array ("varchar", "255", "not null"),
"where"=>array ("varchar", "255", "not null"),
"with space"=>array ("varchar", "255", "not null"));
$res = $db->createTable ();
$db->disconnect ();
$this->assertSame (0, $res);
}
/** Check the OR feature on the same column with different value */
public function test_readOR1 ()
{
$dbconfig = $this->confs["{ENGINE}"];
$db = new dblayer ($dbconfig["dsn"], $dbconfig["username"],
$dbconfig["password"], $dbconfig["driver_options"]);
$db->table = "readOR";
$db->fields = array ("id"=>array ("integer"),
"user"=>array ("varchar", "255", "not null"));
$db->primary = "id";
$db->unique = array ("id");
$db->createTable ();
$db->insert (array ("id"=>"0", "user"=>"test0"));
$db->insert (array ("id"=>"1", "user"=>"test1"));
$res = $db->read (array (array ("id", 0), array ("id", 1)), array ("user"),
null, true);
$db->disconnect ();
$this->assertSame (array (0=>array ("user"=>"test0"),
1=>array ("user"=>"test1")), $res);
}
}