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

/**
  * Doctrine_Ticket_DC373_TestCase
  *
  * @package     Doctrine
  * @author      Joe Siponen <joe.siponen@gmail.com>
  * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
* @category    Object Relational Mapping
  * @link        www.doctrine-project.org
  * @since       1.0
  * @version     $Revision$
  */
class Doctrine_Ticket_DC373_TestCase extends Doctrine_UnitTestCase 
{
  protected
    $i18nTestTable,
    $i18nTestInstance;
    
  
  public function prepareTables()
  {
    $this->tables[] = 'Ticket_DC373_I18n';
    parent::prepareTables();
  }
  
  public function setUp()
  {
    parent::setUp();
    
    $this->i18nTestTable  = $this->connection->getTable('Ticket_DC373_I18n');

    $this->i18nTestInstance = new Ticket_DC373_I18n();
    $this->i18nTestInstance->Translation['en']->name = 'english test 1';
    $this->i18nTestInstance->Translation['fr']->name = 'french test 1';
    $this->i18nTestInstance->save();
  }

  protected function newConnection()
  {
    static $newConnectionCounter = 0;
    $newConnectionName = __CLASS__.$newConnectionCounter++;
    $conn = $this->manager->openConnection($this->dbh, $newConnectionName);
    $this->manager->setCurrentConnection($newConnectionName);
    $conn->setListener(new Doctrine_EventListener());
    return $conn;
  }
  
  // public function testI18nInitializationPostConnectionEvictTables()
  // {
  //   $this->manager->setCurrentConnection($this->driverName);
  //   
  //   $this->assertTrue($this->i18nTestInstance->exists());
  //   
  //   $i18nFoundTestInstance = $this->i18nTestTable->find($this->i18nTestInstance->get('id'));
  //   
  //   $this->assertTrue(is_object($i18nFoundTestInstance));
  //   $this->assertTrue($i18nFoundTestInstance->exists());
  //   
  //   $this->connection->evictTables();
  // }
  
  public function testI18nInitializationInNewConnectionAndAfterEvictTables()
  {
    $this->assertTrue($this->i18nTestInstance->exists());
    
    $i18nFoundTestInstance = $this->i18nTestTable->find($this->i18nTestInstance->get('id'));
    
    $this->assertTrue(is_object($i18nFoundTestInstance));
    $this->assertTrue($i18nFoundTestInstance->exists());
    
    
    // ==========================================
    // = Testing retrieval using new connection =
    // ==========================================
    $otherConnection = $this->newConnection();
    
    $i18nTestTableFromOtherConnection = $otherConnection->getTable('Ticket_DC373_I18n');
      
    try {
      /* 
       * The line following this comment block throws an exception saying that no 'name' 
       * column exists on the table. This is because the full I18n behaviour initialization
       * won't run more than once even if it the new connection needs to reinitilialize
       * tables.
       *
       * Doctrine_Connection_Sqlite_Exception
       * [SQLSTATE[HY000]: 
       * General error: 1 no such column: t.name. Failing Query: "SELECT t.id AS t__id, t.name AS t__name FROM ticket__d_c373__i18_n t WHERE (t.id = ?) LIMIT 1"] in /Users/joes/doctrine_1.2.4/lib/Doctrine/Connection.php on line 1082
       */
       
      $i18nFoundTestInstance = $i18nTestTableFromOtherConnection->find($this->i18nTestInstance->get('id')); //Throws exception
      $this->assertTrue(is_object($i18nFoundTestInstance));
      $this->assertTrue($i18nFoundTestInstance->exists());
      
    } catch (Exception $e) {
      $this->fail('Exception: '. $e->getMessage());
    }
    
    // =================================
    // = Testing retrieval evictTables =
    // =================================
    
    // Check that we are still able to find the object (i.e. check that opening
    // the new connection didn't corrupt the test's state)
    $i18nFoundTestInstance = $this->i18nTestTable->find($this->i18nTestInstance->get('id'));
    
    $this->assertTrue(is_object($i18nFoundTestInstance));
    $this->assertTrue($i18nFoundTestInstance->exists());
      
    $this->connection->evictTables();
    
    try {
      /* 
       * The line following this comment block throws an exception saying that no 'name' 
       * column exists on the table. This is because the full I18n behaviour initialization
       * won't run more than once even if its table needs to be reinitilialized after being
       * evicted.
       *
       * Doctrine_Connection_Sqlite_Exception
       * [SQLSTATE[HY000]: 
       * General error: 1 no such column: t.name. Failing Query: "SELECT t.id AS t__id, t.name AS t__name FROM ticket__d_c373__i18_n t WHERE (t.id = ?) LIMIT 1"] in /Users/joes/doctrine_1.2.4/lib/Doctrine/Connection.php on line 1082
       */
      
      $i18nFoundTestInstance = $this->i18nTestTable->find($this->i18nTestInstance->get('id')); //Throws exception
    } catch (Exception $e) {
      $this->fail('Exception: '. $e->getMessage());
    } 
  }
}

class Ticket_DC373_I18n extends Doctrine_Record
{
  public function setTableDefinition()
  {
    $this->hasColumn('name', 'string', 200);
  }

  public function setUp()
  {
    $this->actAs('I18n', array('fields' => array('name')));
  }
}