<?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.org>.
 */

/**
 * Doctrine_Ticket_DC544_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.org
 * @since       1.0
 * @version     $Revision$
 */
class Doctrine_Ticket_DC544_TestCase extends Doctrine_UnitTestCase 
{
    public function setUp()
    {
        $this->dbh = new Doctrine_Adapter_Mock('oracle');
        $this->conn = Doctrine_Manager::getInstance()->openConnection($this->dbh);
    }
    
    public function prepareTables()
    {
        $this->tables = array();
        $this->tables[] = 'Ticket_DC450_Reviewlist';
        parent::prepareTables();
    }
    
    public function prepareData()
    {
      $rl1 = new Ticket_DC544_Reviewlist();
      $rl1->ils_login = '8880';
      $rl1->file_date = '2010-01-01 12:00:00';
      $rl1->file_name = 'file_1';
      $rl1->save();
      
      $rl2 = new Ticket_DC544_Reviewlist();
      $rl2->ils_login = '8881';
      $rl2->file_date = '2010-01-01 12:00:00';
      $rl2->file_name = 'file_2';
      $rl2->save();
      
      $rl3 = new Ticket_DC544_Reviewlist();
      $rl3->ils_login = '8882';
      $rl3->file_date = '2010-01-01 12:00:00';
      $rl3->file_name = 'file_3';
      $rl3->save();
    }

    public function testQueryResultWithPager()
    {
        try {
            $q = Doctrine::getTable('Ticket_DC544_Reviewlist')->getAvailable();
            
            $pager = new Doctrine_Pager($q, 1, 5);
            $rs = $pager->execute();

            $this->assertEqual($this->dbh->pop(), 'SELECT a.* FROM ( SELECT to_char(r.file_date, \'YYYY-MM-DD\') AS r__0, COUNT(*) AS r__1 FROM reviewlist r GROUP BY to_char(r.file_date, \'YYYY-MM-DD\') ORDER BY r__0 DESC ) a WHERE ROWNUM <= 5');
            
            $this->assertEqual($pager->getNumResults(), 1);
            $this->assertEqual($rs[0]->fdate, '2010-01-01');
            $this->assertEqual($rs[0]->nb, 3);
            
        } catch (Exception $e) {
            $this->fail($e->getMessage());
        }
    }
}

class Ticket_DC544_Reviewlist extends Doctrine_Record
{
  public function setTableDefinition()
  {
      $this->setTableName('reviewlist');
      $this->hasColumn('id', 'integer', 3, array('type' => 'integer','primary' => true,'unsigned' => true,'sequence' => 'country','length' => '3',));
      $this->hasColumn('ils_login', 'integer', 4, array('type' => 'integer','length' => '4',));
      $this->hasColumn('file_date', 'timestamp', null, array('type' => 'string','notnull' => true,));
      $this->hasColumn('file_name', 'string', 45, array('type' => 'string','notnull' => true,'length' => '45',));
  }

  public function setUp()
  {
      parent::setUp();
  }
}


class Ticket_DC544_ReviewlistTable extends Doctrine_Table
{
    public function getAvailable()
    {
        return $this->createQuery()
        ->select('to_char(file_date, \'YYYY-MM-DD\') fdate, COUNT(*) nb')
        ->groupBy('to_char(file_date, \'YYYY-MM-DD\')')
        ->orderBy('fdate DESC');
    }
}