<?php

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\ORM\Events;

/**
 * @group DDC-2157
 */
class DDC2157Test extends \Doctrine\Tests\OrmFunctionalTestCase
{
    protected function setUp() {
        parent::setUp();


        $this->_schemaTool->createSchema(array(
            $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC2157MyMappedTask'),
            $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC2157MyTask'),
        ));
    }

    public function testIssue()
    {
       $taks = new DDC2157MyTask("Foo");

       $this->_em->persist($taks);
       $this->_em->flush();

       $this->assertContains(Events::preFlush,   $taks->calls);
       $this->assertContains(Events::prePersist, $taks->calls);
       $this->assertContains(Events::postPersist, $taks->calls);

       $taks->calls = array();

       $taks->setValue("Bar");
       
       $this->_em->persist($taks);
       $this->_em->flush();
       $this->_em->clear();

       $this->assertContains(Events::preFlush,   $taks->calls);
       $this->assertContains(Events::preUpdate, $taks->calls);
       $this->assertContains(Events::postUpdate, $taks->calls);

       $result = $this->_em->createQuery("SELECT t FROM Doctrine\Tests\ORM\Functional\Ticket\DDC2157MyTask t")->getResult();
       $this->assertCount(1, $result);
       $this->assertInstanceOf('Doctrine\Tests\ORM\Functional\Ticket\DDC2157MyTask', ($loaded = $result[0]));
       $this->assertContains(Events::postLoad, $loaded->calls);

       $loaded->calls = array();

       $this->_em->remove($loaded);
       $this->_em->flush();

       $this->assertContains(Events::preRemove, $loaded->calls);
       $this->assertContains(Events::postRemove, $loaded->calls);
    }
}


/**
 * @MappedSuperclass
 * @HasLifecycleCallbacks
 */
class DDC2157MyMappedTask
{
    /**
     * @Id
     * @GeneratedValue
     * @Column(type="integer")
     */
    private $id;

    /**
     * @Column(type="string")
     */
    private $value;

    public $calls;

    public function __construct($value)
    {
        $this->setValue($value);
    }

    public function getValue()
    {
        return $this->value;
    }

    public function setValue($value)
    {
        $this->value = $value;
    }

    /** @PostLoad */
    public function postLoad()
    {
        $this->calls[] = __FUNCTION__;
    }

    /** @PreFlush */
    public function preFlush()
    {
        $this->calls[] = __FUNCTION__;
    }

    /** @PrePersist */
    public function prePersist()
    {
        $this->calls[] = __FUNCTION__;
    }

    /** @PostPersist */
    public function postPersist()
    {
        $this->calls[] = __FUNCTION__;
    }

    /**  @PreUpdate */
    public function preUpdate()
    {
        $this->calls[] = __FUNCTION__;
    }

    /** @PostUpdate */
    public function postUpdate()
    {
        $this->calls[] = __FUNCTION__;
    }

    /** @PreRemove */
    public function preRemove()
    {
        $this->calls[] = __FUNCTION__;
    }

    /** @PostRemove */
    public function postRemove()
    {
        $this->calls[] = __FUNCTION__;
    }
}

/**
 * @Entity()
 */
class DDC2157MyTask extends DDC2157MyMappedTask
{

}