1
0
mirror of synced 2024-12-13 14:56:01 +03:00
doctrine2/tests/UnitTestCase.php

180 lines
6.1 KiB
PHP
Raw Normal View History

2006-04-14 00:37:28 +04:00
<?php
2006-08-08 13:07:55 +04:00
require_once("../Doctrine.php");
//Doctrine::compile();
2006-08-08 13:07:55 +04:00
//require_once("../Doctrine.compiled.php");
2006-04-14 00:37:28 +04:00
2006-05-30 12:35:28 +04:00
//Doctrine::loadAll();
function __autoload($class) {
Doctrine::autoload($class);
}
2006-04-14 00:37:28 +04:00
require_once("classes.php");
ini_set('include_path',ucwords($_SERVER["DOCUMENT_ROOT"]));
require_once("simpletest/unit_tester.php");
require_once("simpletest/reporter.php");
class Doctrine_UnitTestCase extends UnitTestCase {
protected $manager;
2006-08-22 03:20:33 +04:00
protected $connection;
2006-04-14 00:37:28 +04:00
protected $objTable;
protected $new;
protected $old;
protected $dbh;
protected $listener;
protected $cache;
protected $users;
protected $valueHolder;
protected $tables = array();
2006-04-14 00:37:28 +04:00
private $init = false;
public function init() {
$name = get_class($this);
$this->manager = Doctrine_Manager::getInstance();
2006-04-14 14:20:19 +04:00
$this->manager->setAttribute(Doctrine::ATTR_CACHE, Doctrine::CACHE_NONE);
2006-04-17 02:46:05 +04:00
$this->manager->setAttribute(Doctrine::ATTR_FETCHMODE, Doctrine::FETCH_IMMEDIATE);
2006-04-23 12:12:01 +04:00
2006-06-09 02:11:36 +04:00
$this->tables = array_merge($this->tables,
array("entity",
"entityReference",
"email",
"phonenumber",
"groupuser",
"album",
"song",
"element",
"error",
"description",
"address",
"account",
"task",
"resource",
"assignment",
"resourceType",
"resourceReference")
);
2006-04-23 12:12:01 +04:00
2006-04-14 00:37:28 +04:00
if($this->manager->count() > 0) {
2006-08-22 03:20:33 +04:00
$this->connection = $this->manager->getConnection(0);
$this->connection->evictTables();
$this->dbh = $this->connection->getDBH();
2006-04-14 00:37:28 +04:00
$this->listener = $this->manager->getAttribute(Doctrine::ATTR_LISTENER);
2006-04-23 12:12:01 +04:00
2006-04-14 00:37:28 +04:00
} else {
2006-06-25 22:34:53 +04:00
//$this->dbh = Doctrine_DB::getConnection();
$this->dbh = Doctrine_DB::getConn("sqlite::memory:");
//$this->dbh = new PDO("sqlite::memory:");
2006-08-22 03:20:33 +04:00
$this->connection = $this->manager->openConnection($this->dbh);
2006-06-03 13:10:43 +04:00
$this->listener = new Doctrine_EventListener_Debugger();
2006-04-14 00:37:28 +04:00
$this->manager->setAttribute(Doctrine::ATTR_LISTENER, $this->listener);
}
2006-08-22 03:20:33 +04:00
$this->query = new Doctrine_Query($this->connection);
$this->prepareTables();
$this->prepareData();
2006-08-22 03:20:33 +04:00
$this->valueHolder = new Doctrine_ValueHolder($this->connection->getTable('User'));
}
2006-06-03 13:10:43 +04:00
public function prepareTables() {
foreach($this->tables as $name) {
2006-06-25 22:34:53 +04:00
$query = "DROP TABLE ".strtolower($name);
try {
$this->dbh->query($query);
} catch(PDOException $e) {
}
2006-04-14 00:37:28 +04:00
}
foreach($this->tables as $name) {
$name = ucwords($name);
2006-08-22 03:20:33 +04:00
$table = $this->connection->getTable($name);
2006-04-15 14:15:16 +04:00
$table->getCache()->deleteAll();
2006-06-03 13:10:43 +04:00
$table->clear();
2006-04-14 00:37:28 +04:00
}
2006-08-22 03:20:33 +04:00
$this->objTable = $this->connection->getTable("User");
2006-04-14 00:37:28 +04:00
}
public function prepareData() {
2006-08-22 03:20:33 +04:00
$groups = new Doctrine_Collection($this->connection->getTable("Group"));
2006-04-14 00:37:28 +04:00
$groups[0]->name = "Drama Actors";
$groups[1]->name = "Quality Actors";
2006-04-14 00:37:28 +04:00
$groups[2]->name = "Action Actors";
$groups[2]["Phonenumber"][0]->phonenumber = "123 123";
$groups->save();
2006-04-14 00:37:28 +04:00
2006-08-22 03:20:33 +04:00
$users = new Doctrine_Collection($this->connection->getTable("User"));
2006-04-14 00:37:28 +04:00
$users[0]->name = "zYne";
$users[0]["Email"]->address = "zYne@example.com";
$users[0]["Phonenumber"][0]->phonenumber = "123 123";
$users[1]->name = "Arnold Schwarzenegger";
$users[1]->Email->address = "arnold@example.com";
$users[1]["Phonenumber"][0]->phonenumber = "123 123";
$users[1]["Phonenumber"][1]->phonenumber = "456 456";
$users[1]->Phonenumber[2]->phonenumber = "789 789";
$users[1]->Group[0] = $groups[2];
$users[2]->name = "Michael Caine";
$users[2]->Email->address = "caine@example.com";
$users[2]->Phonenumber[0]->phonenumber = "123 123";
$users[3]->name = "Takeshi Kitano";
$users[3]->Email->address = "kitano@example.com";
$users[3]->Phonenumber[0]->phonenumber = "111 222 333";
$users[4]->name = "Sylvester Stallone";
$users[4]->Email->address = "stallone@example.com";
$users[4]->Phonenumber[0]->phonenumber = "111 555 333";
$users[4]["Phonenumber"][1]->phonenumber = "123 213";
$users[4]["Phonenumber"][2]->phonenumber = "444 555";
$users[5]->name = "Kurt Russell";
$users[5]->Email->address = "russell@example.com";
$users[5]->Phonenumber[0]->phonenumber = "111 222 333";
$users[6]->name = "Jean Reno";
$users[6]->Email->address = "reno@example.com";
$users[6]->Phonenumber[0]->phonenumber = "111 222 333";
$users[6]["Phonenumber"][1]->phonenumber = "222 123";
$users[6]["Phonenumber"][2]->phonenumber = "123 456";
$users[7]->name = "Edward Furlong";
$users[7]->Email->address = "furlong@example.com";
$users[7]->Phonenumber[0]->phonenumber = "111 567 333";
$this->users = $users;
2006-08-22 03:20:33 +04:00
$this->connection->flush();
2006-04-14 00:37:28 +04:00
}
2006-08-22 03:20:33 +04:00
public function getConnection() {
return $this->connection;
2006-04-15 14:15:16 +04:00
}
2006-04-14 00:37:28 +04:00
public function clearCache() {
foreach($this->tables as $name) {
2006-08-22 03:20:33 +04:00
$table = $this->connection->getTable($name);
2006-04-14 00:37:28 +04:00
$table->getCache()->deleteAll();
}
}
public function setUp() {
if( ! $this->init) $this->init();
if(isset($this->objTable))
$this->objTable->clear();
2006-04-14 00:37:28 +04:00
$this->init = true;
}
}
?>