<commit_before><?php

namespace Doctrine\Tests\ORM\Functional;

use Doctrine\Tests\Models\Company\CompanyContractListener;
use Doctrine\Tests\Models\Company\CompanyFixContract;
use Doctrine\Tests\Models\DDC3597\DDC3597Image;
use Doctrine\Tests\Models\DDC3597\DDC3597Media;
use Doctrine\Tests\Models\DDC3597\DDC3597Root;

/**
 * @group DDC-1955
 * @group 5570
 * @group 6174
 */
class EntityListenersOnMergeTest extends \Doctrine\Tests\OrmFunctionalTestCase
{

    /**
     * @var \Doctrine\Tests\Models\Company\CompanyContractListener
     */
    private $listener;

    protected function setUp()
    {
        $this->useModelSet('company');
        parent::setUp();

        $this->_schemaTool->createSchema(
            [
                $this->_em->getClassMetadata(DDC3597Root::class),
                $this->_em->getClassMetadata(DDC3597Media::class),
                $this->_em->getClassMetadata(DDC3597Image::class),
            ]
        );

        $this->listener = $this->_em->getConfiguration()
            ->getEntityListenerResolver()
            ->resolve('Doctrine\Tests\Models\Company\CompanyContractListener');
    }

    protected function tearDown()
    {
        parent::tearDown();
        $this->_schemaTool->dropSchema(
            [
                $this->_em->getClassMetadata(DDC3597Root::class),
                $this->_em->getClassMetadata(DDC3597Media::class),
                $this->_em->getClassMetadata(DDC3597Image::class),
            ]
        );
    }

    public function testMergeNewEntityLifecyleEventsModificationsShouldBeKept()
    {
        $imageEntity = new DDC3597Image('foobar');
        $imageEntity->setFormat('JPG');
        $imageEntity->setSize(123);
        $imageEntity->getDimension()->setWidth(300);
        $imageEntity->getDimension()->setHeight(500);

        $imageEntity = $this->_em->merge($imageEntity);

        $this->assertNotNull($imageEntity->getCreatedAt());
        $this->assertNotNull($imageEntity->getUpdatedAt());
    }

    public function testPrePersistListenersShouldBeFiredWithCorrectEntityData()
    {
        $fix = new CompanyFixContract();
        $fix->setFixPrice(2000);

        $this->listener->prePersistCalls = [];

        $fix = $this->_em->merge($fix);
        $this->_em->flush();

        $this->assertCount(1, $this->listener->prePersistCalls);

        $this->assertSame($fix, $this->listener->prePersistCalls[0][0]);

        $this->assertInstanceOf(
            'Doctrine\Tests\Models\Company\CompanyFixContract',
            $this->listener->prePersistCalls[0][0]
        );

        $this->assertInstanceOf(
            'Doctrine\ORM\Event\LifecycleEventArgs',
            $this->listener->prePersistCalls[0][1]
        );

        $this->assertArrayHasKey('fixPrice', $this->listener->snapshots[CompanyContractListener::PRE_PERSIST][0]);
        $this->assertEquals(
            $fix->getFixPrice(),
            $this->listener->snapshots[CompanyContractListener::PRE_PERSIST][0]['fixPrice']
        );
    }
}<commit_msg>#6174 #5570 CS fixes around the `EntityListenersOnMergeTest`<commit_after><?php

namespace Doctrine\Tests\ORM\Functional;

use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\Tests\Models\Company\CompanyContractListener;
use Doctrine\Tests\Models\Company\CompanyFixContract;
use Doctrine\Tests\Models\DDC3597\DDC3597Image;
use Doctrine\Tests\Models\DDC3597\DDC3597Media;
use Doctrine\Tests\Models\DDC3597\DDC3597Root;
use Doctrine\Tests\OrmFunctionalTestCase;

/**
 * @group DDC-1955
 * @group 5570
 * @group 6174
 */
class EntityListenersOnMergeTest extends OrmFunctionalTestCase
{
    /**
     * @var CompanyContractListener
     */
    private $listener;

    protected function setUp()
    {
        $this->useModelSet('company');
        parent::setUp();

        $this->_schemaTool->createSchema([
            $this->_em->getClassMetadata(DDC3597Root::class),
            $this->_em->getClassMetadata(DDC3597Media::class),
            $this->_em->getClassMetadata(DDC3597Image::class),
        ]);

        $this->listener = $this->_em->getConfiguration()
            ->getEntityListenerResolver()
            ->resolve(CompanyContractListener::class);
    }

    protected function tearDown()
    {
        parent::tearDown();

        $this->_schemaTool->dropSchema([
            $this->_em->getClassMetadata(DDC3597Root::class),
            $this->_em->getClassMetadata(DDC3597Media::class),
            $this->_em->getClassMetadata(DDC3597Image::class),
        ]);
    }

    public function testMergeNewEntityLifecyleEventsModificationsShouldBeKept()
    {
        $imageEntity = new DDC3597Image('foobar');

        $imageEntity->setFormat('JPG');
        $imageEntity->setSize(123);
        $imageEntity->getDimension()->setWidth(300);
        $imageEntity->getDimension()->setHeight(500);

        $imageEntity = $this->_em->merge($imageEntity);

        $this->assertNotNull($imageEntity->getCreatedAt());
        $this->assertNotNull($imageEntity->getUpdatedAt());
    }

    public function testPrePersistListenersShouldBeFiredWithCorrectEntityData()
    {
        $fix = new CompanyFixContract();

        $fix->setFixPrice(2000);

        $this->listener->prePersistCalls = [];

        $fix = $this->_em->merge($fix);
        $this->_em->flush();

        $this->assertCount(1, $this->listener->prePersistCalls);

        $this->assertSame($fix, $this->listener->prePersistCalls[0][0]);

        $this->assertInstanceOf(CompanyFixContract::class, $this->listener->prePersistCalls[0][0]);
        $this->assertInstanceOf(LifecycleEventArgs::class, $this->listener->prePersistCalls[0][1]);

        $this->assertArrayHasKey('fixPrice', $this->listener->snapshots[CompanyContractListener::PRE_PERSIST][0]);
        $this->assertEquals(
            $fix->getFixPrice(),
            $this->listener->snapshots[CompanyContractListener::PRE_PERSIST][0]['fixPrice']
        );
    }
}

{'fraction_non_alphanumeric': 0.1461603375527426, 'fraction_numerical': 0.030379746835443037, 'mean_word_length': 3.0897170462387855, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 123, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '981900', 'n_tokens_mistral': 1843, 'n_tokens_neox': 1728, 'n_words': 184}