EventListenerTestCase.php 7.15 KB
Newer Older
doctrine's avatar
doctrine committed
1
<?php
zYne's avatar
zYne committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*
 *  $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_EventListener_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$
 */
33 34 35 36 37 38
class EventListenerTest extends Doctrine_Record {
    public function setTableDefinition() {
        $this->hasColumn("name", "string", 100);
        $this->hasColumn("password", "string", 8);
    }
    public function setUp() {
zYne's avatar
zYne committed
39
        $this->attribute(Doctrine::ATTR_LISTENER, new Doctrine_EventListener_AccessorInvoker());
40 41 42 43 44 45 46 47
    }
    public function getName($name) {
        return strtoupper($name);
    }
    public function setPassword($password) {
        return md5($password);
    }
}
48 49
class Doctrine_EventListener_TestLogger implements Doctrine_Overloadable, Countable {
    private $messages = array();
doctrine's avatar
doctrine committed
50

51 52 53 54 55 56 57 58 59
    public function __call($m, $a) {

        $this->messages[] = $m;
    }
    public function pop() {
        return array_pop($this->messages);
    }
    public function clear() {
        $this->messages = array();
doctrine's avatar
doctrine committed
60
    }
61 62 63 64 65 66 67 68
    public function getAll() {
        return $this->messages;
    }
    public function count() {
        return count($this->messages);
    }
}

zYne's avatar
zYne committed
69
class Doctrine_EventListener_TestCase extends Doctrine_UnitTestCase {
70 71
    private $logger;

72

73 74 75 76 77 78 79
    public function testSetListener() {
        $this->logger = new Doctrine_EventListener_TestLogger();
    
        $e = new EventListenerTest;
        
        $e->getTable()->setListener($this->logger);

80 81 82
        $e->name = 'listener';
        $e->save();

83 84 85 86 87 88 89 90 91
        $this->assertEqual($e->getTable()->getListener(), $this->logger);
    }
    public function testOnLoad() {
        $this->logger->clear();
        $this->assertEqual($this->connection->getTable('EventListenerTest')->getListener(), $this->logger);
        $this->connection->clear();

        $e = $this->connection->getTable('EventListenerTest')->find(1);

92

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
        $this->assertEqual($e->getTable()->getListener(), $this->logger);

        $this->assertEqual($this->logger->pop(), 'onLoad');
        $this->assertEqual($this->logger->pop(), 'onPreLoad');
    }

    public function testOnCreate() {
        $e = new EventListenerTest;
        

        $e->setListener($this->logger);
        $this->logger->clear();
        $e = new EventListenerTest;

        $this->assertEqual($this->logger->pop(), 'onCreate');
        $this->assertEqual($this->logger->pop(), 'onPreCreate');
        $this->assertEqual($this->logger->count(), 0);
110
    }
111 112 113 114 115 116 117 118 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 161 162 163
    public function testOnSleepAndOnWakeUp() {
        $e = new EventListenerTest;

        $this->logger->clear();

        $s = serialize($e);

        $this->assertEqual($this->logger->pop(), 'onSleep');
        $this->assertEqual($this->logger->count(), 0);

        $e = unserialize($s);

        $this->assertEqual($this->logger->pop(), 'onWakeUp');
        $this->assertEqual($this->logger->count(), 0);
    }
    public function testTransaction() {
        $e = new EventListenerTest();
        $e->name = "test 1";
        
        $this->logger->clear();

        $e->save();

        $this->assertEqual($this->logger->pop(), 'onSave');
        $this->assertEqual($this->logger->pop(), 'onInsert');
        $this->assertEqual($this->logger->pop(), 'onPreInsert');
        $this->assertEqual($this->logger->pop(), 'onPreSave');
        
        $e->name = "test 2";

        $e->save();

        $this->assertEqual($this->logger->pop(), 'onSave');
        $this->assertEqual($this->logger->pop(), 'onUpdate');
        $this->assertEqual($this->logger->pop(), 'onPreUpdate');
        $this->assertEqual($this->logger->pop(), 'onPreSave');
        
        $this->logger->clear();

        $e->delete();

        $this->assertEqual($this->logger->pop(), 'onDelete');
        $this->assertEqual($this->logger->pop(), 'onPreDelete');
    }
    public function testTransactionWithConnectionListener() {
        $e = new EventListenerTest();
        $e->getTable()->getConnection()->setListener($this->logger);
        
        $e->name = "test 2";
        
        $this->logger->clear();
        
        $e->save();
164

165
        $this->assertEqual($this->logger->pop(), 'onTransactionCommit');
166
        $this->assertEqual($this->logger->pop(), 'onPreTransactionCommit');
167 168 169 170
        $this->assertEqual($this->logger->pop(), 'onSave');
        $this->assertEqual($this->logger->pop(), 'onInsert');
        $this->assertEqual($this->logger->pop(), 'onPreInsert');
        $this->assertEqual($this->logger->pop(), 'onPreSave');
171

172 173 174 175 176 177
        $this->assertEqual($this->logger->pop(), 'onTransactionBegin');
        $this->assertEqual($this->logger->pop(), 'onPreTransactionBegin');

        $e->name = "test 1";

        $e->save();
178

179
        $this->assertEqual($this->logger->pop(), 'onTransactionCommit');
180
        $this->assertEqual($this->logger->pop(), 'onPreTransactionCommit');
181 182 183 184
        $this->assertEqual($this->logger->pop(), 'onSave');
        $this->assertEqual($this->logger->pop(), 'onUpdate');
        $this->assertEqual($this->logger->pop(), 'onPreUpdate');
        $this->assertEqual($this->logger->pop(), 'onPreSave');
185

186 187 188 189 190 191 192 193
        $this->assertEqual($this->logger->pop(), 'onTransactionBegin');
        $this->assertEqual($this->logger->pop(), 'onPreTransactionBegin');

        $this->logger->clear();

        $e->delete();

        $this->assertEqual($this->logger->pop(), 'onTransactionCommit');
194
        $this->assertEqual($this->logger->pop(), 'onPreTransactionCommit');
195
        $this->assertEqual($this->logger->pop(), 'onDelete');
196

197 198 199
        $this->assertEqual($this->logger->pop(), 'onPreDelete');
        $this->assertEqual($this->logger->pop(), 'onTransactionBegin');
        $this->assertEqual($this->logger->pop(), 'onPreTransactionBegin');
zYne's avatar
zYne committed
200 201
    
        $this->connection->setListener(new Doctrine_EventListener());
202 203
    }

zYne's avatar
zYne committed
204 205


doctrine's avatar
doctrine committed
206
    public function prepareData() { }
207 208 209 210
    public function prepareTables() {
        $this->tables = array('EventListenerTest');
        parent::prepareTables();
    }
zYne's avatar
zYne committed
211

doctrine's avatar
doctrine committed
212 213
}
?>