Ticket330TestCase.php 2.9 KB
Newer Older
zYne's avatar
zYne committed
1 2
<?php

zYne's avatar
zYne committed
3

zYne's avatar
zYne committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17
/**
 * Doctrine_Ticket330_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 stNode extends Doctrine_Record
{
zYne's avatar
zYne committed
18 19 20 21 22 23 24 25 26 27 28
    public function setTableDefinition()
    {
        $this->setTableName('node_node');

        $this->hasColumn('title', 'string', 255, array ());
    }

    public function setUp()
    {
        $this->hasOne('stNodeDetail as detail', 'stNodeDetail.node_id' , array( 'onDelete'=>'cascade'));
    }
zYne's avatar
zYne committed
29 30 31 32
}
 
class stNodeDetail extends Doctrine_Record
{
zYne's avatar
zYne committed
33 34 35 36 37 38 39 40 41
    public function setTableDefinition()
    {
        $this->setTableName('node_detail');
        
        $this->hasColumn('node_id', 'integer', 10, array (  'unique' => true,));
        $this->hasColumn('null_column', 'string', 255, array ('default'=>null));
        $this->hasColumn('is_bool', 'boolean', null, array ('default' => 0,));
        $this->option('type', 'MyISAM');
    }
zYne's avatar
zYne committed
42
  
zYne's avatar
zYne committed
43 44 45 46
    public function setUp()
    {
        $this->hasOne('stNode as node', 'stNodeDetail.article_id', array('foreign' => 'id' , 'onDelete'=>'cascade'));
    }
zYne's avatar
zYne committed
47 48 49 50 51
}
 
 
class Doctrine_Ticket330_TestCase extends Doctrine_UnitTestCase
{
zYne's avatar
zYne committed
52
    public function prepareData() 
zYne's avatar
zYne committed
53 54
    { }
    public function prepareTables()
zYne's avatar
zYne committed
55 56 57 58 59 60
    {
    	$this->tables[] = 'stNode';
    	$this->tables[] = 'stNodeDetail';
    	parent::prepareTables();
    }

zYne's avatar
zYne committed
61 62
    public function testUnnecessaryQueries()
    {
zYne's avatar
zYne committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
        
        $node1 = new stNode();
        $node1->set('title', 'first node');
        $node1->detail->set('is_bool', true);
        $node1->save();
        
        $node2 = new stNode();
        $node2->set('title', 'second node');
        $node2->detail->set('null_column', 'value');
        $node2->detail->set('is_bool', false);
        $node2->save();
        
        $nodes = Doctrine_Query::create()
                    ->select('n.title, d.*')
                    ->from('stNode n, n.detail d')   
                    ->orderby('n.id')
                    ->execute();

        $prevCount = $this->dbh->count();
        
                
        $this->assertEqual($nodes[0]->detail->get('is_bool'), true);
        $this->assertEqual($nodes[0]->detail->get('null_column'), null);

        // Unnecessary query is triggered on line before due to null value column.
        $this->assertEqual($this->dbh->count(), $prevCount);

        $prevCount = $this->dbh->count();

        $this->assertEqual($nodes[1]->detail->get('null_column'), 'value');
        $this->assertEqual($nodes[1]->detail->get('is_bool'), false);

        // Unecessary query is triggered on line before due to false value column
        $this->assertEqual($this->dbh->count(), $prevCount);
zYne's avatar
zYne committed
97 98
    }
}