UnitTestCase.php 6.27 KB
Newer Older
doctrine's avatar
doctrine committed
1
<?php
2
require_once dirname(__FILE__).'/../lib/Doctrine.php';
doctrine's avatar
doctrine committed
3

4
//Doctrine::compile();
doctrine's avatar
doctrine committed
5 6 7

//require_once("../Doctrine.compiled.php");

doctrine's avatar
doctrine committed
8

doctrine's avatar
doctrine committed
9
//Doctrine::loadAll();
pookey's avatar
pookey committed
10
//ini_set('include_path', $_SERVER['DOCUMENT_ROOT']);
11

doctrine's avatar
doctrine committed
12 13 14
function __autoload($class) {
    Doctrine::autoload($class);
}
doctrine's avatar
doctrine committed
15 16 17 18 19 20 21 22
require_once("classes.php");

require_once("simpletest/unit_tester.php");
require_once("simpletest/reporter.php");


class Doctrine_UnitTestCase extends UnitTestCase {
    protected $manager;
zYne's avatar
zYne committed
23
    protected $connection;
doctrine's avatar
doctrine committed
24 25 26 27 28 29 30
    protected $objTable;
    protected $new;
    protected $old;
    protected $dbh;
    protected $listener;
    protected $cache;
    protected $users;
31
    protected $valueHolder;
doctrine's avatar
doctrine committed
32
    protected $tables = array();
33
    protected $unitOfWork;
doctrine's avatar
doctrine committed
34 35 36 37 38 39 40

    private $init = false;

    public function init() {
        $name = get_class($this);

        $this->manager   = Doctrine_Manager::getInstance();
doctrine's avatar
doctrine committed
41
        $this->manager->setAttribute(Doctrine::ATTR_FETCHMODE, Doctrine::FETCH_IMMEDIATE);
42
        
doctrine's avatar
doctrine committed
43

doctrine's avatar
doctrine committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
        $this->tables = array_merge($this->tables, 
                        array("entity",
                              "entityReference",
                              "email",
                              "phonenumber",
                              "groupuser",
                              "album",
                              "song",
                              "element",
                              "error",
                              "description",
                              "address",
                              "account",
                              "task",
                              "resource",
                              "assignment",
                              "resourceType",
                              "resourceReference")
                              );
63 64 65



66 67
        try {
            $this->connection = $this->manager->getConnection('main');
zYne's avatar
zYne committed
68
            $this->connection->evictTables();
zYne's avatar
zYne committed
69
            $this->dbh      = $this->connection->getDBH();
doctrine's avatar
doctrine committed
70
            $this->listener = $this->manager->getAttribute(Doctrine::ATTR_LISTENER);
71

72
            $this->manager->setAttribute(Doctrine::ATTR_LISTENER, $this->listener);
73
        } catch(Doctrine_Manager_Exception $e) {
pookey's avatar
pookey committed
74
            //$this->dbh     = Doctrine_Db::getConnection();
75
            $this->dbh       = Doctrine_Db::getConnection("sqlite::memory:");
76
            //$this->dbh      = new PDO("sqlite::memory:");
zYne's avatar
zYne committed
77

78
            $this->connection  = $this->manager->openConnection($this->dbh, 'main');
zYne's avatar
zYne committed
79

doctrine's avatar
doctrine committed
80
            $this->listener = new Doctrine_EventListener_Debugger();
doctrine's avatar
doctrine committed
81 82
            $this->manager->setAttribute(Doctrine::ATTR_LISTENER, $this->listener);
        }
83
        $this->unitOfWork = $this->connection->unitOfWork;
84
        $this->connection->setListener(new Doctrine_EventListener());
zYne's avatar
zYne committed
85
        $this->query = new Doctrine_Query($this->connection);
86 87
        $this->prepareTables();
        $this->prepareData();
88

zYne's avatar
zYne committed
89
        $this->valueHolder = new Doctrine_ValueHolder($this->connection->getTable('User'));
90

91
    }
doctrine's avatar
doctrine committed
92
    public function prepareTables() {
93
        foreach($this->tables as $name) {
94
            $query = "DROP TABLE ".Doctrine::tableize($name);
95 96 97 98 99
            try {
                $this->dbh->query($query);
            } catch(PDOException $e) {

            }
doctrine's avatar
doctrine committed
100 101
        }

102
        foreach($this->tables as $name) {
103
            $name = ucwords($name);
zYne's avatar
zYne committed
104
            $table = $this->connection->getTable($name);
105

doctrine's avatar
doctrine committed
106
            $table->clear(); 
doctrine's avatar
doctrine committed
107 108
        }

zYne's avatar
zYne committed
109
        $this->objTable = $this->connection->getTable("User");
doctrine's avatar
doctrine committed
110 111
    }
    public function prepareData() {
zYne's avatar
zYne committed
112
        $groups = new Doctrine_Collection($this->connection->getTable("Group"));
doctrine's avatar
doctrine committed
113 114 115 116

        $groups[0]->name = "Drama Actors";

        $groups[1]->name = "Quality Actors";
117

doctrine's avatar
doctrine committed
118 119 120

        $groups[2]->name = "Action Actors";
        $groups[2]["Phonenumber"][0]->phonenumber = "123 123";
121
        $groups->save();
doctrine's avatar
doctrine committed
122

zYne's avatar
zYne committed
123
        $users = new Doctrine_Collection($this->connection->getTable("User"));
doctrine's avatar
doctrine committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165


        $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;
zYne's avatar
zYne committed
166
        $this->connection->flush();
doctrine's avatar
doctrine committed
167
    }
zYne's avatar
zYne committed
168 169
    public function getConnection() {
        return $this->connection;
doctrine's avatar
doctrine committed
170
    }
doctrine's avatar
doctrine committed
171 172
    public function clearCache() {
        foreach($this->tables as $name) {
zYne's avatar
zYne committed
173
            $table = $this->connection->getTable($name);
doctrine's avatar
doctrine committed
174 175 176 177 178 179
            $table->getCache()->deleteAll();
        }
    }
    public function setUp() {
        if( ! $this->init) $this->init(); 
        
180 181 182
        if(isset($this->objTable))
            $this->objTable->clear();
        
doctrine's avatar
doctrine committed
183 184 185 186
        $this->init    = true;
    }
}
?>