<?php

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\Common\EventSubscriber;

require_once __DIR__ . '/../../../TestInit.php';

/**
 * @group DDC-1443
 */
class DDC1443Test extends \Doctrine\Tests\OrmFunctionalTestCase
{
    public function setUp()
    {
        parent::setUp();
        
        $configuration = $this->_getTestEntityManager()->getConfiguration();
        $configuration->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
        
        try {
            $this->_schemaTool->createSchema(array(
                $this->_em->getClassMetadata(__NAMESPACE__ . '\\DDC1443_TestEntity1'),
                $this->_em->getClassMetadata(__NAMESPACE__ . '\\DDC1443_TestEntity2'),
            ));
        } catch(\PDOException $e) {
            
        }
    }
    
    public function testIssue()
    {
        $em = $this->_getTestEntityManager();
        $em->getEventManager()->addEventSubscriber(new TestEntity1Subscriber());
        
        $entity = new DDC1443_TestEntity2();
        
        $em->persist($entity);
        $em->flush();
        
        $q = $em->createQuery('SELECT e FROM ' . __NAMESPACE__ . '\\DDC1443_TestEntity1 e WHERE e.testEntity2 = ?0');
        $q->setParameter(0, $entity);
        
        $this->assertEquals(1, count($q->getResult()));
    }
}

class TestEntity1Subscriber implements EventSubscriber
{
    /**
     * {@inheritdoc}
     */
    public function getSubscribedEvents()
    {
        return array('prePersist');
    }

    /**
     * Check if this entity is listened to
     */
    protected function isListenedTo($entity)
    {
        return ($entity instanceof DDC1443_TestEntity2);
    }

    /**
      * Create a new DDC1443_TestEntity1
     * 
     * @param EventArgs $args 
     */
    public function prePersist($args)
    {
        $entity = $args->getEntity();
        $em     = $args->getEntityManager();
        
        // Is it our desired Entity?
        if ( ! $this->isListenedTo($entity)) return;

        if ($entity->getId() === null) {
            $entityWrapper = new DDC1443_TestEntity1();
            $entityWrapper->setTestEntity2($entity);
            
            $em->persist($entityWrapper);
        }
    }
}

/**
 * @Entity
 * @Table(name="te1")
 */
class DDC1443_TestEntity1
{
    /**
     * @Id
     * @GeneratedValue(strategy="AUTO")
     * @Column(type="integer")
     */
    private $id;
    
    /**
     * @OneToOne(targetEntity="Doctrine\Tests\ORM\Functional\Ticket\DDC1443_TestEntity2", cascade={"persist"})
     * @JoinColumn(name="test_entity2_id", referencedColumnName="id", nullable=false)
     */
    private $testEntity2;
    
    /**
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param DDC1443_TestEntity2 $testEntity2
     */
    public function setTestEntity2(DDC1443_TestEntity2 $testEntity2)
    {
        $this->testEntity2 = $testEntity2;
    }

    /**
     * @return DDC1443_TestEntity2
     */
    public function getTestEntity2()
    {
        return $this->testEntity2;
    }
}

/**
 * @Entity
 * @Table(name="te2")
 */
class DDC1443_TestEntity2
{
    /**
     * @Id
     * @GeneratedValue(strategy="AUTO")
     * @Column(type="integer")
     */
    private $id;
    
    /**
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }
}