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

421 lines
16 KiB
PHP
Raw Normal View History

<?php
2007-06-20 02:26:41 +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_Db_TestCase
*
* @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$
*/
class Doctrine_Db_TestCase extends Doctrine_UnitTestCase
{
2006-11-08 13:18:15 +03:00
2007-07-12 02:03:47 +04:00
public function prepareData()
{ }
public function prepareTables()
{ }
public function init()
{ }
2007-01-12 14:38:03 +03:00
2007-06-20 02:26:41 +04:00
public function testInitialize()
{
2007-07-12 02:03:47 +04:00
$this->conn = Doctrine_Manager::getInstance()->openConnection(array('sqlite::memory:'));
$this->conn->exec('CREATE TABLE entity (id INTEGER, name TEXT)');
2006-09-23 14:44:39 +04:00
2007-07-12 02:03:47 +04:00
$this->conn->exec("INSERT INTO entity (id, name) VALUES (1, 'zYne')");
$this->conn->exec("INSERT INTO entity (id, name) VALUES (2, 'John')");
2007-07-12 02:03:47 +04:00
$this->assertEqual($this->conn->getAttribute(Doctrine::ATTR_DRIVER_NAME), 'sqlite');
}
2007-06-20 02:26:41 +04:00
public function testAddValidEventListener()
{
2007-07-12 02:03:47 +04:00
$this->conn->setListener(new Doctrine_EventListener());
2006-11-08 13:18:15 +03:00
2007-07-12 02:03:47 +04:00
$this->assertTrue($this->conn->getListener() instanceof Doctrine_EventListener);
2006-09-23 14:44:39 +04:00
try {
2007-07-12 02:03:47 +04:00
$ret = $this->conn->addListener(new Doctrine_Connection_TestLogger());
2006-09-23 14:44:39 +04:00
$this->pass();
2007-06-20 02:26:41 +04:00
$this->assertTrue($ret instanceof Doctrine_Connection);
} catch(Doctrine_EventListener_Exception $e) {
2006-09-23 14:44:39 +04:00
$this->fail();
}
2007-07-12 02:03:47 +04:00
$this->assertTrue($this->conn->getListener() instanceof Doctrine_EventListener_Chain);
$this->assertTrue($this->conn->getListener()->get(0) instanceof Doctrine_Connection_TestLogger);
2006-11-08 13:18:15 +03:00
2006-09-23 14:44:39 +04:00
try {
2007-07-12 02:03:47 +04:00
$ret = $this->conn->addListener(new Doctrine_Connection_TestValidListener());
2006-09-23 14:44:39 +04:00
$this->pass();
2007-06-20 02:26:41 +04:00
$this->assertTrue($ret instanceof Doctrine_Connection);
} catch(Doctrine_EventListener_Exception $e) {
2006-09-23 14:44:39 +04:00
$this->fail();
}
2007-07-12 02:03:47 +04:00
$this->assertTrue($this->conn->getListener() instanceof Doctrine_EventListener_Chain);
$this->assertTrue($this->conn->getListener()->get(0) instanceof Doctrine_Connection_TestLogger);
$this->assertTrue($this->conn->getListener()->get(1) instanceof Doctrine_Connection_TestValidListener);
2006-09-23 14:44:39 +04:00
try {
2007-07-12 02:03:47 +04:00
$ret = $this->conn->addListener(new Doctrine_EventListener_Chain(), 'chain');
2006-09-23 14:44:39 +04:00
$this->pass();
2007-06-20 02:26:41 +04:00
$this->assertTrue($ret instanceof Doctrine_Connection);
} catch(Doctrine_EventListener_Exception $e) {
2006-09-23 14:44:39 +04:00
$this->fail();
}
2007-07-12 02:03:47 +04:00
$this->assertTrue($this->conn->getListener() instanceof Doctrine_EventListener_Chain);
$this->assertTrue($this->conn->getListener()->get(0) instanceof Doctrine_Connection_TestLogger);
$this->assertTrue($this->conn->getListener()->get(1) instanceof Doctrine_Connection_TestValidListener);
$this->assertTrue($this->conn->getListener()->get('chain') instanceof Doctrine_EventListener_Chain);
2007-06-20 02:26:41 +04:00
2006-09-23 14:44:39 +04:00
// replacing
try {
2007-07-12 02:03:47 +04:00
$ret = $this->conn->addListener(new Doctrine_EventListener_Chain(), 'chain');
2006-09-23 14:44:39 +04:00
$this->pass();
2007-06-20 02:26:41 +04:00
$this->assertTrue($ret instanceof Doctrine_Connection);
} catch(Doctrine_EventListener_Exception $e) {
2006-09-23 14:44:39 +04:00
$this->fail();
}
2007-07-12 02:03:47 +04:00
$this->assertTrue($this->conn->getListener() instanceof Doctrine_EventListener_Chain);
$this->assertTrue($this->conn->getListener()->get(0) instanceof Doctrine_Connection_TestLogger);
$this->assertTrue($this->conn->getListener()->get(1) instanceof Doctrine_Connection_TestValidListener);
$this->assertTrue($this->conn->getListener()->get('chain') instanceof Doctrine_EventListener_Chain);
2006-09-23 14:44:39 +04:00
}
2006-11-08 13:18:15 +03:00
2007-06-20 02:26:41 +04:00
public function testListeningEventsWithSingleListener()
{
2007-07-12 02:03:47 +04:00
$this->conn->setListener(new Doctrine_Connection_TestLogger());
$listener = $this->conn->getListener();
$stmt = $this->conn->prepare('INSERT INTO entity (id) VALUES(?)');
2006-09-23 14:44:39 +04:00
2007-06-25 14:08:03 +04:00
$this->assertEqual($listener->pop(), 'postPrepare');
$this->assertEqual($listener->pop(), 'prePrepare');
2006-09-23 14:44:39 +04:00
$stmt->execute(array(1));
2007-07-12 02:03:47 +04:00
$this->assertEqual($listener->pop(), 'postStmtExecute');
$this->assertEqual($listener->pop(), 'preStmtExecute');
2006-09-23 14:44:39 +04:00
2007-07-12 02:03:47 +04:00
$this->conn->exec('DELETE FROM entity');
2006-09-23 14:44:39 +04:00
2007-06-25 14:08:03 +04:00
$this->assertEqual($listener->pop(), 'postExec');
$this->assertEqual($listener->pop(), 'preExec');
2006-09-23 14:44:39 +04:00
2007-07-12 02:03:47 +04:00
$this->conn->beginTransaction();
2006-09-23 14:44:39 +04:00
2007-06-25 14:08:03 +04:00
$this->assertEqual($listener->pop(), 'postTransactionBegin');
$this->assertEqual($listener->pop(), 'preTransactionBegin');
2007-06-20 02:26:41 +04:00
2007-07-12 02:03:47 +04:00
$this->conn->exec('INSERT INTO entity (id) VALUES (1)');
2006-09-23 14:44:39 +04:00
2007-06-25 14:08:03 +04:00
$this->assertEqual($listener->pop(), 'postExec');
$this->assertEqual($listener->pop(), 'preExec');
2006-09-23 14:44:39 +04:00
2007-07-12 02:03:47 +04:00
$this->conn->commit();
2006-09-23 14:44:39 +04:00
2007-06-25 14:08:03 +04:00
$this->assertEqual($listener->pop(), 'postTransactionCommit');
$this->assertEqual($listener->pop(), 'preTransactionCommit');
2006-09-23 14:44:39 +04:00
}
2007-07-12 02:03:47 +04:00
2007-06-20 02:26:41 +04:00
public function testListeningQueryEventsWithListenerChain()
{
2007-07-12 02:03:47 +04:00
$this->conn->exec('DROP TABLE entity');
2006-11-08 13:18:15 +03:00
2007-07-12 02:03:47 +04:00
$this->conn->addListener(new Doctrine_Connection_TestLogger());
$this->conn->addListener(new Doctrine_Connection_TestLogger());
2006-09-23 14:44:39 +04:00
2007-07-12 02:03:47 +04:00
$this->conn->exec('CREATE TABLE entity (id INT)');
2006-09-23 14:44:39 +04:00
2007-07-12 02:03:47 +04:00
$listener = $this->conn->getListener()->get(0);
$listener2 = $this->conn->getListener()->get(1);
2007-06-25 14:08:03 +04:00
$this->assertEqual($listener->pop(), 'postExec');
$this->assertEqual($listener->pop(), 'preExec');
2006-09-23 14:44:39 +04:00
2007-06-25 14:08:03 +04:00
$this->assertEqual($listener2->pop(), 'postExec');
$this->assertEqual($listener2->pop(), 'preExec');
}
2007-06-25 14:08:03 +04:00
2007-06-20 02:26:41 +04:00
public function testListeningPrepareEventsWithListenerChain()
{
2006-09-23 14:44:39 +04:00
2007-07-12 02:03:47 +04:00
$stmt = $this->conn->prepare('INSERT INTO entity (id) VALUES(?)');
$listener = $this->conn->getListener()->get(0);
$listener2 = $this->conn->getListener()->get(1);
2007-06-25 14:08:03 +04:00
$this->assertEqual($listener->pop(), 'postPrepare');
$this->assertEqual($listener->pop(), 'prePrepare');
2006-09-23 14:44:39 +04:00
2007-06-25 14:08:03 +04:00
$this->assertEqual($listener2->pop(), 'postPrepare');
$this->assertEqual($listener2->pop(), 'prePrepare');
2006-09-23 14:44:39 +04:00
$stmt->execute(array(1));
2007-07-12 02:03:47 +04:00
$this->assertEqual($listener->pop(), 'postStmtExecute');
$this->assertEqual($listener->pop(), 'preStmtExecute');
2007-07-12 02:03:47 +04:00
$this->assertEqual($listener2->pop(), 'postStmtExecute');
$this->assertEqual($listener2->pop(), 'preStmtExecute');
}
public function testListeningErrorHandlingMethodsOnExec()
{
$this->conn->setAttribute(Doctrine::ATTR_THROW_EXCEPTIONS, false);
2007-07-12 02:03:47 +04:00
$listener = $this->conn->getListener()->get(0);
$this->conn->exec('DELETE FROM unknown');
$this->assertEqual($listener->pop(), 'postError');
$this->assertEqual($listener->pop(), 'preError');
$this->assertEqual($listener->pop(), 'preExec');
}
public function testListeningErrorHandlingMethodsOnQuery()
{
$this->conn->setAttribute(Doctrine::ATTR_THROW_EXCEPTIONS, false);
2007-07-12 02:03:47 +04:00
$listener = $this->conn->getListener()->get(0);
$this->conn->execute('DELETE FROM unknown');
$this->assertEqual($listener->pop(), 'postError');
$this->assertEqual($listener->pop(), 'preError');
$this->assertEqual($listener->pop(), 'preQuery');
}
public function testListeningErrorHandlingMethodsOnPrepare()
{
$this->conn->setAttribute(Doctrine::ATTR_THROW_EXCEPTIONS, false);
2007-07-12 02:03:47 +04:00
$listener = $this->conn->getListener()->get(0);
$this->conn->prepare('INSERT INTO unknown (id) VALUES (?)');
$this->assertEqual($listener->pop(), 'postError');
$this->assertEqual($listener->pop(), 'preError');
$this->assertEqual($listener->pop(), 'prePrepare');
}
public function testListeningErrorHandlingMethodsOnStatementExecute()
{
$this->conn->setAttribute(Doctrine::ATTR_THROW_EXCEPTIONS, false);
2007-07-12 02:03:47 +04:00
$listener = $this->conn->getListener()->get(0);
$stmt = $this->conn->prepare('INSERT INTO entity (id) VALUES (?)');
$stmt->execute(array(1, 2, 3));
$this->assertEqual($listener->pop(), 'postError');
$this->assertEqual($listener->pop(), 'preError');
$this->assertEqual($listener->pop(), 'preStmtExecute');
$this->assertEqual($listener->pop(), 'postPrepare');
$this->assertEqual($listener->pop(), 'prePrepare');
}
2007-06-25 14:08:03 +04:00
public function testListeningExecEventsWithListenerChain()
2007-06-20 02:26:41 +04:00
{
2007-07-12 02:03:47 +04:00
$this->conn->exec('DELETE FROM entity');
$listener = $this->conn->getListener()->get(0);
$listener2 = $this->conn->getListener()->get(1);
2007-06-25 14:08:03 +04:00
$this->assertEqual($listener->pop(), 'postExec');
$this->assertEqual($listener->pop(), 'preExec');
2006-09-23 14:44:39 +04:00
2007-06-25 14:08:03 +04:00
$this->assertEqual($listener2->pop(), 'postExec');
$this->assertEqual($listener2->pop(), 'preExec');
}
2007-07-12 02:03:47 +04:00
2007-06-20 02:26:41 +04:00
public function testListeningTransactionEventsWithListenerChain()
{
2007-07-12 02:03:47 +04:00
$this->conn->beginTransaction();
$listener = $this->conn->getListener()->get(0);
$listener2 = $this->conn->getListener()->get(1);
2007-06-25 14:08:03 +04:00
$this->assertEqual($listener->pop(), 'postTransactionBegin');
$this->assertEqual($listener->pop(), 'preTransactionBegin');
2006-09-23 14:44:39 +04:00
2007-06-25 14:08:03 +04:00
$this->assertEqual($listener2->pop(), 'postTransactionBegin');
$this->assertEqual($listener2->pop(), 'preTransactionBegin');
2006-09-23 14:44:39 +04:00
2007-07-12 02:03:47 +04:00
$this->conn->exec('INSERT INTO entity (id) VALUES (1)');
2006-09-23 14:44:39 +04:00
2007-07-12 02:03:47 +04:00
$this->conn->commit();
2006-09-23 14:44:39 +04:00
2007-06-25 14:08:03 +04:00
$this->assertEqual($listener->pop(), 'postTransactionCommit');
$this->assertEqual($listener->pop(), 'preTransactionCommit');
2006-09-23 14:44:39 +04:00
2007-06-25 14:08:03 +04:00
$this->assertEqual($listener->pop(), 'postExec');
$this->assertEqual($listener->pop(), 'preExec');
2006-09-23 14:44:39 +04:00
2007-07-12 02:03:47 +04:00
$this->conn->exec('DROP TABLE entity');
2006-09-23 14:44:39 +04:00
}
2007-07-12 02:03:47 +04:00
2007-06-20 02:26:41 +04:00
public function testSetValidEventListener()
{
2006-09-23 14:44:39 +04:00
try {
2007-07-12 02:03:47 +04:00
$this->conn->setListener(new Doctrine_Connection_TestLogger());
2006-09-23 14:44:39 +04:00
$this->pass();
2007-06-20 02:26:41 +04:00
} catch(Doctrine_EventListener_Exception $e) {
2006-09-23 14:44:39 +04:00
$this->fail();
}
2007-07-12 02:03:47 +04:00
$this->assertTrue($this->conn->getListener() instanceof Doctrine_Connection_TestLogger);
2006-09-23 14:44:39 +04:00
try {
2007-07-12 02:03:47 +04:00
$this->conn->setListener(new Doctrine_Connection_TestValidListener());
2006-09-23 14:44:39 +04:00
$this->pass();
2007-06-20 02:26:41 +04:00
} catch(Doctrine_EventListener_Exception $e) {
2006-09-23 14:44:39 +04:00
$this->fail();
}
2007-07-12 02:03:47 +04:00
$this->assertTrue($this->conn->getListener() instanceof Doctrine_Connection_TestValidListener);
2006-09-23 14:44:39 +04:00
try {
2007-07-12 02:03:47 +04:00
$this->conn->setListener(new Doctrine_EventListener_Chain());
2006-09-23 14:44:39 +04:00
$this->pass();
2007-06-20 02:26:41 +04:00
} catch(Doctrine_EventListener_Exception $e) {
2006-09-23 14:44:39 +04:00
$this->fail();
}
2007-07-12 02:03:47 +04:00
$this->assertTrue($this->conn->getListener() instanceof Doctrine_EventListener_Chain);
2006-09-23 14:44:39 +04:00
try {
2007-07-12 02:03:47 +04:00
$this->conn->setListener(new Doctrine_EventListener());
2006-09-23 14:44:39 +04:00
$this->pass();
2007-06-20 02:26:41 +04:00
} catch(Doctrine_EventListener_Exception $e) {
2006-09-23 14:44:39 +04:00
$this->fail();
}
2007-07-12 02:03:47 +04:00
$this->assertTrue($this->conn->getListener() instanceof Doctrine_EventListener);
2006-09-23 14:44:39 +04:00
}
2007-07-12 02:03:47 +04:00
2007-06-20 02:26:41 +04:00
public function testSetInvalidEventListener()
{
2006-09-23 14:44:39 +04:00
try {
2007-07-12 02:03:47 +04:00
$this->conn->setListener(new Doctrine_Connection_TestInvalidListener());
2006-09-23 14:44:39 +04:00
$this->fail();
2007-06-20 02:26:41 +04:00
} catch(Doctrine_EventListener_Exception $e) {
2006-09-23 14:44:39 +04:00
$this->pass();
}
}
2007-06-20 02:26:41 +04:00
public function testInvalidDSN()
{
$manager = Doctrine_Manager::getInstance();
try {
2007-07-12 02:03:47 +04:00
$this->conn = $manager->openConnection('');
$this->fail();
2007-06-20 02:26:41 +04:00
} catch(Doctrine_Exception $e) {
$this->pass();
}
try {
2007-07-12 02:03:47 +04:00
$this->conn = $manager->openConnection('unknown');
$this->fail();
2007-06-20 02:26:41 +04:00
} catch(Doctrine_Exception $e) {
$this->pass();
}
try {
2007-07-12 02:03:47 +04:00
$this->conn = $manager->openConnection(0);
$this->fail();
2007-06-20 02:26:41 +04:00
} catch(Doctrine_Exception $e) {
$this->pass();
}
}
2007-06-20 02:26:41 +04:00
public function testInvalidScheme()
{
$manager = Doctrine_Manager::getInstance();
try {
2007-07-12 02:03:47 +04:00
$this->conn = $manager->openConnection('unknown://:memory:');
$this->fail();
2007-06-20 02:26:41 +04:00
} catch(Doctrine_Exception $e) {
$this->pass();
}
}
2007-06-20 02:26:41 +04:00
public function testInvalidHost()
{
$manager = Doctrine_Manager::getInstance();
try {
2007-07-12 02:03:47 +04:00
$this->conn = $manager->openConnection('mysql://user:password@');
$this->fail();
2007-06-20 02:26:41 +04:00
} catch(Doctrine_Exception $e) {
$this->pass();
}
}
2007-06-20 02:26:41 +04:00
public function testInvalidDatabase()
{
$manager = Doctrine_Manager::getInstance();
try {
2007-07-12 02:03:47 +04:00
$this->conn = $manager->openConnection('mysql://user:password@host/');
$this->fail();
2007-06-20 02:26:41 +04:00
} catch(Doctrine_Exception $e) {
$this->pass();
}
}
2007-06-20 02:26:41 +04:00
/**
public function testGetConnectionPdoLikeDSN()
{
2007-07-12 02:03:47 +04:00
$this->conn = Doctrine_Manager::openConnection(array('mysql:host=localhost;dbname=test', 'root', 'password'));
$this->assertEqual($this->conn->getOption('dsn'), 'mysql:host=localhost;dbname=test');
$this->assertEqual($this->conn->getOption('username'), 'root');
$this->assertEqual($this->conn->getOption('password'), 'password');
2006-09-23 02:29:06 +04:00
2007-07-12 02:03:47 +04:00
$this->conn = Doctrine_Connection::getConnection('sqlite::memory:');
2006-09-23 02:29:06 +04:00
2007-07-12 02:03:47 +04:00
$this->assertEqual($this->conn->getOption('dsn'), 'sqlite::memory:');
$this->assertEqual($this->conn->getOption('username'), false);
$this->assertEqual($this->conn->getOption('password'), false);
2006-09-23 02:29:06 +04:00
}
2007-06-20 02:26:41 +04:00
public function testDriverName()
{
2006-09-23 14:44:39 +04:00
}
2007-06-20 02:26:41 +04:00
public function testGetConnectionWithPearLikeDSN()
{
2007-07-12 02:03:47 +04:00
$this->conn = Doctrine_Connection::getConnection('mysql://zYne:password@localhost/test');
$this->assertEqual($this->conn->getOption('dsn'), 'mysql:host=localhost;dbname=test');
$this->assertEqual($this->conn->getOption('username'), 'zYne');
$this->assertEqual($this->conn->getOption('password'), 'password');
2006-09-23 14:44:39 +04:00
2007-07-12 02:03:47 +04:00
$this->conn = Doctrine_Connection::getConnection('sqlite://:memory:');
2006-09-23 14:44:39 +04:00
2007-07-12 02:03:47 +04:00
$this->assertEqual($this->conn->getOption('dsn'), 'sqlite::memory:');
$this->assertEqual($this->conn->getOption('username'), false);
$this->assertEqual($this->conn->getOption('password'), false);
}
2007-06-20 02:26:41 +04:00
*/
}
2006-09-23 02:29:06 +04:00
2007-06-20 02:26:41 +04:00
class Doctrine_Connection_TestLogger implements Doctrine_Overloadable {
private $messages = array();
public function __call($m, $a) {
$this->messages[] = $m;
}
public function pop() {
return array_pop($this->messages);
}
public function getAll() {
return $this->messages;
}
}
2007-06-20 02:26:41 +04:00
class Doctrine_Connection_TestValidListener extends Doctrine_EventListener { }
class Doctrine_Connection_TestInvalidListener { }