1
0
mirror of synced 2024-12-15 07:36:03 +03:00
doctrine2/tests/UnitTestCase.php

297 lines
9.9 KiB
PHP
Raw Normal View History

2006-04-14 00:37:28 +04:00
<?php
2007-06-15 01:01:57 +04:00
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.com>.
*/
/**
* Doctrine_UnitTestCase
*
* @package Doctrine
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @category Object Relational Mapping
* @link www.phpdoctrine.com
* @since 1.0
* @version $Revision$
*/
2007-06-19 15:19:53 +04:00
class Doctrine_UnitTestCase extends UnitTestCase
{
2006-04-14 00:37:28 +04:00
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;
2007-02-05 15:04:56 +03:00
2006-04-14 00:37:28 +04:00
protected $users;
protected $valueHolder;
protected $tables = array();
protected $unitOfWork;
2006-12-28 00:20:26 +03:00
protected $driverName = false;
protected $generic = false;
protected $conn;
protected $adapter;
protected $export;
protected $expr;
2006-12-28 00:20:26 +03:00
protected $dataDict;
protected $transaction;
2007-01-15 21:48:50 +03:00
2006-04-14 00:37:28 +04:00
2007-04-11 23:12:52 +04:00
protected $init = false;
2006-04-14 00:37:28 +04:00
2007-06-19 15:19:53 +04:00
public function init()
{
2006-04-14 00:37:28 +04:00
$name = get_class($this);
$this->manager = Doctrine_Manager::getInstance();
2007-04-11 23:12:52 +04:00
$this->manager->setAttribute(Doctrine::ATTR_EXPORT, Doctrine::EXPORT_ALL);
2006-06-09 02:11:36 +04:00
$this->tables = array_merge($this->tables,
2007-06-15 01:01:57 +04:00
array('entity',
'entityReference',
'email',
'phonenumber',
'groupuser',
'album',
'song',
'element',
'error',
'description',
'address',
'account',
'task',
'resource',
'assignment',
'resourceType',
'resourceReference')
2006-06-09 02:11:36 +04:00
);
2006-04-23 12:12:01 +04:00
2006-12-28 00:20:26 +03:00
$class = get_class($this);
$e = explode('_', $class);
2007-06-19 15:19:53 +04:00
if ( ! $this->driverName) {
$this->driverName = 'main';
switch($e[1]) {
case 'Export':
case 'Import':
case 'Transaction':
case 'DataDict':
case 'Sequence':
$this->driverName = 'Sqlite';
2006-12-28 00:20:26 +03:00
break;
}
2007-07-06 16:37:02 +04:00
$module = $e[1];
2007-06-19 15:19:53 +04:00
if(count($e) > 3) {
$driver = $e[2];
switch($e[2]) {
case 'Firebird':
case 'Informix':
case 'Mysql':
case 'Mssql':
case 'Oracle':
case 'Pgsql':
case 'Sqlite':
$this->driverName = $e[2];
break;
}
}
2006-12-28 00:20:26 +03:00
}
2006-04-23 12:12:01 +04:00
try {
2007-01-15 21:48:50 +03:00
$this->conn = $this->connection = $this->manager->getConnection($this->driverName);
2007-02-17 15:38:02 +03:00
$this->manager->setCurrentConnection($this->driverName);
2006-08-22 03:20:33 +04:00
$this->connection->evictTables();
2006-12-28 00:20:26 +03:00
$this->dbh = $this->adapter = $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
$this->manager->setAttribute(Doctrine::ATTR_LISTENER, $this->listener);
2007-03-22 01:11:18 +03:00
} catch(Doctrine_Manager_Exception $e) {
2006-12-28 00:20:26 +03:00
if($this->driverName == 'main') {
2007-06-25 23:25:23 +04:00
$this->dbh = new PDO('sqlite::memory:');
$this->dbh->sqliteCreateFunction('trim', 'trim', 1);
2006-12-28 00:20:26 +03:00
} else {
2007-06-10 23:40:14 +04:00
$this->dbh = $this->adapter = new Doctrine_Adapter_Mock($this->driverName);
2006-12-28 00:20:26 +03:00
}
2006-11-08 13:18:15 +03:00
2007-01-15 21:48:50 +03:00
$this->conn = $this->connection = $this->manager->openConnection($this->dbh, $this->driverName);
2006-12-28 00:20:26 +03:00
if($this->driverName !== 'main') {
$exc = 'Doctrine_Connection_' . ucwords($this->driverName) . '_Exception';
$this->exc = new $exc();
} else {
}
2006-11-08 13:18:15 +03:00
2007-06-19 15:19:53 +04:00
$this->listener = new Doctrine_EventListener();
2006-04-14 00:37:28 +04:00
$this->manager->setAttribute(Doctrine::ATTR_LISTENER, $this->listener);
}
2007-02-17 15:38:02 +03:00
if ($this->driverName !== 'main') {
2007-07-06 16:37:02 +04:00
if (isset($module)) {
switch($module) {
case 'Export':
case 'Import':
case 'Transaction':
case 'Sequence':
case 'Expression':
$lower = strtolower($module);
$this->$lower = $this->connection->$lower;
break;
case 'DataDict':
$this->dataDict = $this->connection->dataDict;
break;
}
}
2006-12-28 00:20:26 +03:00
}
$this->unitOfWork = $this->connection->unitOfWork;
$this->connection->setListener(new Doctrine_EventListener());
2006-08-22 03:20:33 +04:00
$this->query = new Doctrine_Query($this->connection);
2007-02-17 15:38:02 +03:00
if ($this->driverName === 'main') {
$this->prepareTables();
$this->prepareData();
}
}
2006-06-03 13:10:43 +04:00
public function prepareTables() {
foreach($this->tables as $name) {
2006-12-28 00:20:26 +03:00
$query = 'DROP TABLE ' . Doctrine::tableize($name);
2006-06-25 22:34:53 +04:00
try {
$this->conn->exec($query);
} catch(Doctrine_Connection_Exception $e) {
2006-06-25 22:34:53 +04:00
}
2006-04-14 00:37:28 +04:00
}
2007-06-15 01:01:57 +04:00
$this->conn->export->exportClasses($this->tables);
/**
foreach($this->tables as $name) {
$name = ucwords($name);
2006-08-22 03:20:33 +04:00
$table = $this->connection->getTable($name);
2007-06-15 01:01:57 +04:00
$table->clear();
2006-04-14 00:37:28 +04:00
}
2007-06-15 01:01:57 +04:00
*/
2006-04-14 00:37:28 +04:00
2006-12-28 00:20:26 +03:00
$this->objTable = $this->connection->getTable('User');
2006-04-14 00:37:28 +04:00
}
2007-06-19 15:19:53 +04:00
public function prepareData()
{
2006-12-28 00:20:26 +03:00
$groups = new Doctrine_Collection($this->connection->getTable('Group'));
2006-04-14 00:37:28 +04:00
2006-12-28 00:20:26 +03:00
$groups[0]->name = 'Drama Actors';
2006-04-14 00:37:28 +04:00
2006-12-28 00:20:26 +03:00
$groups[1]->name = 'Quality Actors';
2006-04-14 00:37:28 +04:00
2006-12-28 00:20:26 +03:00
$groups[2]->name = 'Action Actors';
$groups[2]['Phonenumber'][0]->phonenumber = '123 123';
$groups->save();
2006-04-14 00:37:28 +04:00
2006-12-28 00:20:26 +03:00
$users = new Doctrine_Collection('User');
2006-04-14 00:37:28 +04:00
2006-12-28 00:20:26 +03:00
$users[0]->name = 'zYne';
$users[0]['Email']->address = 'zYne@example.com';
$users[0]['Phonenumber'][0]->phonenumber = '123 123';
2006-04-14 00:37:28 +04:00
2006-12-28 00:20:26 +03:00
$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';
2006-04-14 00:37:28 +04:00
$users[1]->Group[0] = $groups[2];
2006-12-28 00:20:26 +03:00
$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';
2006-04-14 00:37:28 +04:00
$this->users = $users;
2007-06-25 23:25:23 +04:00
$this->users->save();
2006-04-14 00:37:28 +04:00
}
2007-06-19 15:19:53 +04:00
public function getConnection()
{
2006-08-22 03:20:33 +04:00
return $this->connection;
2006-04-15 14:15:16 +04:00
}
2007-06-19 15:19:53 +04:00
public function assertDeclarationType($type, $type2)
{
$dec = $this->getDeclaration($type);
2007-02-17 15:38:02 +03:00
if ( ! is_array($type2)) {
$type2 = array($type2);
2007-02-17 15:38:02 +03:00
}
$this->assertEqual($dec['type'], $type2);
}
2007-06-19 15:19:53 +04:00
public function getDeclaration($type)
{
return $this->dataDict->getPortableDeclaration(array('type' => $type, 'name' => 'colname', 'length' => 1, 'fixed' => true));
}
2007-06-19 15:19:53 +04:00
public function clearCache()
{
2006-04-14 00:37:28 +04:00
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();
}
}
2007-06-19 15:19:53 +04:00
public function setUp()
{
2007-05-17 01:28:33 +04:00
if ( ! $this->init) {
$this->init();
}
2007-05-27 22:56:54 +04:00
if (isset($this->objTable)) {
$this->objTable->clear();
}
2007-06-25 23:25:23 +04:00
$this->init = true;
2006-04-14 00:37:28 +04:00
}
}