HydrateTestCase.php 3.88 KB
Newer Older
zYne's avatar
zYne committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
<?php
/*
 *  $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>.
 */

/**
zYne's avatar
zYne committed
23
 * Doctrine_Hydrate_TestCase
zYne's avatar
zYne committed
24 25 26 27 28 29 30 31 32
 *
 * @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$
 */
zYne's avatar
zYne committed
33
class Doctrine_Hydrate_TestCase extends Doctrine_UnitTestCase
zYne's avatar
zYne committed
34
{
zYne's avatar
zYne committed
35 36 37 38 39 40 41 42 43
    protected $testData1 = array(
                              array(
                                  'e' => array('id' => 1, 'name' => 'zYne'),
                                  'p' => array('id' => 1, 'phonenumber' => '123 123', 'user_id' => 1)
                                  ),
                              array(
                                  'e' => array('id' => 2, 'name' => 'John'),
                                  'p' => array('id' => 2, 'phonenumber' => '222 222', 'user_id' => 2)
                                  ),
zYne's avatar
zYne committed
44 45 46 47
                              array(
                                  'e' => array('id' => 2, 'name' => 'John'),
                                  'p' => array('id' => 3, 'phonenumber' => '343 343', 'user_id' => 2)
                                  ),
zYne's avatar
zYne committed
48 49
                              array(
                                  'e' => array('id' => 3, 'name' => 'Arnold'),
zYne's avatar
zYne committed
50 51 52 53 54
                                  'p' => array('id' => 4, 'phonenumber' => '333 333', 'user_id' => 3)
                                  ),
                              array(
                                  'e' => array('id' => 4, 'name' => 'Arnold'),
                                  'p' => array('id' => null, 'phonenumber' => null, 'user_id' => null)
zYne's avatar
zYne committed
55 56
                                  )
                              );
57 58
    public function prepareData()
    { }
zYne's avatar
zYne committed
59

60
    public function testHydrateHooks()
zYne's avatar
zYne committed
61
    {
62
        $user = new User();
63
        $user->getTable()->addRecordListener(new HydrationListener);
64 65 66 67 68 69 70 71 72 73

        $user->name = 'zYne';
        $user->save();

        $this->conn->clear();

        $user = Doctrine_Query::create()->from('User u')->fetchOne();

        $this->assertEqual($user->name, 'ZYNE');
        $this->assertEqual($user->password, 'DEFAULT PASS');
zYne's avatar
zYne committed
74
    }
75
}
76
class HydrationListener extends Doctrine_Record_Listener
77 78
{
    public function preHydrate(Doctrine_Event $event) 
zYne's avatar
zYne committed
79
    {
80 81 82 83
        $data = $event->data;
        $data['password'] = 'default pass';
        
        $event->data = $data;
zYne's avatar
zYne committed
84
    }
85
    public function postHydrate(Doctrine_Event $event)
zYne's avatar
zYne committed
86
    {
87 88 89
    	foreach ($event->data as $key => $value) {
            $event->data[$key] = strtoupper($value);
        }
zYne's avatar
zYne committed
90 91
    }
}
92
class Doctrine_Hydrate_Mock extends Doctrine_Hydrator_Abstract
zYne's avatar
zYne committed
93 94 95 96 97 98 99
{
    protected $data;

    public function setData($data)
    {
        $this->data = $data;
    }
100
    
101
    public function hydrateResultSet($stmt, $tableAliases, $hydrationMode = null)
102
    {
103
        return true;
zYne's avatar
zYne committed
104
    }
zYne's avatar
zYne committed
105
}