UnitTestCase.php 6.15 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();
10 11
ini_set('include_path', $_SERVER['DOCUMENT_ROOT']);

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();
doctrine's avatar
doctrine committed
33 34 35 36 37 38 39

    private $init = false;

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

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

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



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

71
            $this->manager->setAttribute(Doctrine::ATTR_LISTENER, $this->listener);
doctrine's avatar
doctrine committed
72
        } else {
73 74 75
            //$this->dbh     = Doctrine_DB::getConnection();
            $this->dbh      = Doctrine_DB::getConn("sqlite::memory:");
            //$this->dbh      = new PDO("sqlite::memory:");
zYne's avatar
zYne committed
76
            $this->connection  = $this->manager->openConnection($this->dbh);
doctrine's avatar
doctrine committed
77
            $this->listener = new Doctrine_EventListener_Debugger();
doctrine's avatar
doctrine committed
78 79
            $this->manager->setAttribute(Doctrine::ATTR_LISTENER, $this->listener);
        }
80
        $this->connection->setListener(new Doctrine_EventListener());
zYne's avatar
zYne committed
81
        $this->query = new Doctrine_Query($this->connection);
82 83
        $this->prepareTables();
        $this->prepareData();
84

zYne's avatar
zYne committed
85
        $this->valueHolder = new Doctrine_ValueHolder($this->connection->getTable('User'));
86
    }
doctrine's avatar
doctrine committed
87
    public function prepareTables() {
88
        foreach($this->tables as $name) {
89
            $query = "DROP TABLE ".Doctrine::tableize($name);
90 91 92 93 94
            try {
                $this->dbh->query($query);
            } catch(PDOException $e) {

            }
doctrine's avatar
doctrine committed
95 96
        }

97
        foreach($this->tables as $name) {
98
            $name = ucwords($name);
zYne's avatar
zYne committed
99
            $table = $this->connection->getTable($name);
100

doctrine's avatar
doctrine committed
101
            $table->clear(); 
doctrine's avatar
doctrine committed
102 103
        }

zYne's avatar
zYne committed
104
        $this->objTable = $this->connection->getTable("User");
doctrine's avatar
doctrine committed
105 106
    }
    public function prepareData() {
zYne's avatar
zYne committed
107
        $groups = new Doctrine_Collection($this->connection->getTable("Group"));
doctrine's avatar
doctrine committed
108 109 110 111

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

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

doctrine's avatar
doctrine committed
113 114 115

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

zYne's avatar
zYne committed
118
        $users = new Doctrine_Collection($this->connection->getTable("User"));
doctrine's avatar
doctrine committed
119 120 121 122 123 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


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