PHP Code:
<?php
require('Dev/PDOBench.php');
require('Dev/DevFuncs.php');
ra('../ORM/');
require('simpletest/unit_tester.php');
require('simpletest/reporter.php');
require('simpletest/reporter_showpasses.php');
// Host, Username, Password and Database for tests:
define('DBHOST','localhost');
define('DBUSER','dev');
define('DBPASS','devnull');
define('DBNAME','development');
/* NOTICE: These tests will drop/create the tables named
"Groups_test","Persons_test","Memeberships_test" and "Posts_test" on the
database the tests are pointed at - these tests should
NOT be run on anything else then a database used for
testing purposes. If you don't know what something of the above
means - don't run the tests
NOTICE: These test will currently only work on a mysql database.
To get the tests to run, set define RUN_TESTS to true
*/
define('RUN_TESTS',true);
if(RUN_TESTS === true){
class TestObjectRelationalMapping extends UnitTestCase{
function TestObjectRelationalMapping(){
$this->UnitTestCase("O/R Mapping test");
$this->pdoBench = new PDOBench("mysql:host=".DBHOST.";dbname=".DBNAME,DBUSER,DBPASS);
$this->worker = new UnitOfWork($this->pdoBench,'./Classes');
}
function setUp(){
$this->pdo = new PDO("mysql:host=".DBHOST.";dbname=".DBNAME,DBUSER,DBPASS);
$groups = "CREATE TABLE `Groups_test` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(25) NOT NULL,
`comment` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;";
$this->pdo->exec($groups);
$groupsData = "INSERT INTO `Groups_test` (`id`, `name`, `comment`)
VALUES (1, 'Admin', 'Administrators....'), (2, 'Users', 'Normal users...');";
$this->pdo->exec($groupsData);
$memberships = "CREATE TABLE `Memberships_test` (
`id` int(11) NOT NULL auto_increment,
`person` int(11) NOT NULL,
`group` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;";
$this->pdo->exec($memberships);
$persons = "CREATE TABLE `Persons_test` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(50) NOT NULL,
`age` int(11) NOT NULL,
`email` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;";
$this->pdo->exec($persons);
$personsData = "INSERT INTO `Persons_test` (`id`, `name`, `age`, `email`) VALUES
(1, 'Fredrik', 20, 'thrthr@gmail.com'), (2, 'Madeleine', 19, 'unkown');";
$this->pdo->exec($personsData);
$posts = "CREATE TABLE `Posts_test` (
`id` int(11) NOT NULL auto_increment,
`author` int(11) default NULL,
`text` text NOT NULL,
`subject` varchar(255) default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;";
$this->pdo->exec($posts);
$this->worker->unregisterAll();
}
function tearDown(){
$this->pdo->exec('DROP TABLE `Groups_test`, `Memberships_test`, `Persons_test`, `Posts_test`;');
}
function testFind(){
$result = $this->worker->mapper("Person")->find(array('age' => 20));
$this->assertEqual(1,count($result));
$this->assertIsA($result[0],'Person');
}
function testFindByPk(){
$person = $this->worker->mapper("Person")->findByPk(1);
$this->assertIsA($person,'Person');
}
function testFindBySql(){
$result = $this->worker->mapper("Person")->findBySql("select * from `Persons_test`");
$this->assertEqual(2,count($result));
$this->assertIsA($result[0],'Person');
$this->assertIsA($result[1],'Person');
$this->assertNotEqual($result[0]->id,$result[1]->id);
}
function testFindOne(){
$result = $this->worker->mapper("Person")->findFirst(array('age' => 20));
$this->assertIsA($result,'Person');
}
function testCreate(){
$person = $this->worker->create("Person");
$this->assertIsA($person,"Person");
$this->assertNull($person->id);
}
function testCreationsAreSaved(){
$person = $this->worker->create("Person");
$person->name = "Dummy";
$person->age = 1;
$person->email = "Unkown...";
$this->assertIsA($person,"Person");
$this->assertNull($person->id);
$this->worker->commit();
$this->assertEqual($person->id,3);
}
function testJoinedCreationsAreSaved(){
$person = $this->worker->create("Person");
$person->name = "Dummy";
$person->age = 2;
$person->email = "Unkown...";
$post1 = $person->posts->create();
$post2 = $person->posts->create();
$post3 = $person->posts->create();
$this->assertIsA($post1,"Post");
$this->assertIsA($post2,"Post");
$this->assertIsA($post3,"Post");
$post1->text = "I ";
$post2->text = "<3 ";
$post3->text = " you";
$this->worker->commit();
$this->assertNotNull($person->id);
$this->assertNotNull($post1->id);
$this->assertNotNull($post2->id);
$this->assertNotNull($post3->id);
}
function testRegisterDirty(){
$person = $this->worker->mapper("Person")->findByPk(1);
$person->name = "thr";
$this->worker->registerDirty($person);
$this->worker->commit();
$newWorker = new UnitOfWork($this->pdoBench,'./Classes');
$personCopy = $newWorker->mapper("Person")->findByPk(1);
$this->assertEqual($person->name,$personCopy->name);
}
function testRegisterDelete(){
$person = $this->worker->mapper("Person")->findByPk(1);
$this->worker->registerDelete($person);
$this->worker->commit();
$newWorker = new UnitOfWork($this->pdoBench,'./Classes');
$personCopy = $newWorker->mapper("Person")->findByPk(1);
$this->assertNull($personCopy);
}
function testLinkedCreationsAreSavedToDb(){
$person = $this->worker->create("Person");
$person->name = "Dummy";
$person->age = 2;
$person->email = "Unkown...";
$post1 = $person->posts->create();
$post2 = $person->posts->create();
$post3 = $person->posts->create();
$post1->text = "I ";
$post2->text = "<3 ";
$post3->text = " you";
$this->worker->commit();
$newWorker = new UnitOfWork($this->pdoBench,'./Classes');
$person = $newWorker->mapper("Person")->findByPk(3);
foreach($person->posts as $post){
$this->assertIsA($post,"Post");
}
}
function testCollectionCreationsAutomaticlyAssigned(){
$person = $this->worker->mapper("Person")->findByPk(1);
$group = $this->worker->mapper("Group")->findByPk(1);
$membership = $person->memberships->create();
$membership->group = $group;
$this->assertEqual($person->id,$membership->person->id);
$this->worker->Commit();
$newWorker = new UnitOfWork($this->pdoBench,'./Classes');
$membership = $newWorker->mapper("Membership")->findByPk(1);
$this->assertEqual($person->id,$membership->person->id);
$this->assertEqual($group->id,$membership->group->id);
}
function testThresholdAutoLoad(){
$person = $this->worker->mapper("Person")->findByPk(1);
for($i = 0; $i < 25;$i++){
$post = $person->posts->create();
$post->text = $i;
}
$this->worker->commit();
$newWorker = new UnitOfWork($this->pdoBench,'./Classes');
$person = $newWorker->mapper("Person")->findByPk(1);
$person->posts->setThreshold(5);
foreach($person->posts as $post){
$this->assertIsA($post,"Post");
}
}
// "aliasing" is actually a normal php reference
function testAliasing(){
$person = $this->worker->mapper("Person")->findByPk(1);
$person->mail = "this is a new email";
$this->worker->registerDirty($person);
$this->worker->commit();
// Person::$mail is a reference to Person::$email (sql field)
// That's defined in Person::__construct();
$this->assertEqual($person->mail,$person->email);
$newWorker = new UnitOfWork($this->pdoBench,'./Classes');
$person2 = $newWorker->mapper("Person")->findByPk(1);
$this->assertEqual($person->mail,$person2->email);
}
function testSyntaticalSugar(){
$person = $this->worker->findPersonByPk(1);
$this->assertIsA($person,"Person");
$personList = $this->worker->findPerson(array('name' => 'Fredrik'));
$this->assertEqual(1,count($personList));
$this->worker->deletePersonByPk(1);
}
function testDisplayPDO(){
$this->pdoBench->display();
}
}
$testORM = new TestObjectRelationalMapping;
$testORM->run(new ShowPasses());
}
?>
Bookmarks