<?php

require_once('db/query.class.phps');

Mock::generate("Criteria");
Mock::generate("OrderByCriteria");
Mock::generate("SetFieldsCriteria");

class 
TestQuery extends UnitTestCase    {

    function 
setUp()    {
        
$this->obj    =&    new Query('SELECT * FROM table');
    }

    function 
tearDown()    {
        unset(
$this->obj);
    }

    function 
testConstruct()    {
        
$this->assertEqual($this->obj->query'SELECT * FROM table');
        
$this->assertTrue(is_array($this->obj->criteria) );
        
$this->assertTrue(is_array($this->obj->orderby) );
        
$this->assertTrue(is_null($this->obj->set) );
    }

    function 
testAddCriteria()    {
        
$Criteria    =&    new MockCriteria($this);
        
$this->obj->addCriteria($Criteria);
        
$this->assertReference($Criteria$this->obj->criteria[0]);
        
$Criteria->tally();
        
        
$Criteria    =&    new MockOrderByCriteria($this);
        
$this->obj->addCriteria($Criteria);
        
$this->assertReference($Criteria$this->obj->orderby[0]);
        
$Criteria->tally();

        
$Criteria    =&    new MockSetFieldsCriteria($this);
        
$this->obj->addCriteria($Criteria);
        
$this->assertReference($Criteria$this->obj->set);
        
$Criteria->tally();
    }

    function 
testGenerateSql()    {
        
$where    =&    new MockCriteria($this);
        
$where->setReturnValue('generateSql'"field = '1'");
        
        
$order    =&    new MockOrderByCriteria($this);
        
$order->setReturnValue('generateSql'"field DESC");
        
        
$set    =&    new MockSetFieldsCriteria($this);
        
$set->setReturnValue('generateSql'"`field1` = 'value1', `field2` = 'value2'");

        
$this->obj->addCriteria($order);
        
$this->obj->addCriteria($where);
        
$this->obj->addCriteria($set);
        
$this->obj->setQuery("UPDATE table");

        
$this->assertEqual(
            
$this->obj->generateSql(),
            
"UPDATE table SET `field1` = 'value1', `field2` = 'value2' WHERE field = '1' ORDER BY field DESC"
        
);

        
$where->tally();
        
$order->tally();
        
$set->tally();
    }

}

?>