<commit_before><?php

namespace Doctrine\Tests\ORM\Mapping;

use Doctrine\Instantiator\Instantiator;
use Doctrine\ORM\Mapping\ReflectionEmbeddedProperty;
use ReflectionProperty;

/**
 * Tests for {@see \Doctrine\ORM\Mapping\ReflectionEmbeddedProperty}
 *
 * @covers \Doctrine\ORM\Mapping\ReflectionEmbeddedProperty
 */
class ReflectionEmbeddedPropertyTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @param ReflectionProperty $parentProperty
     * @param ReflectionProperty $childProperty
     *
     * @dataProvider getTestedReflectionProperties
     */
    public function testCanSetAndGetEmbeddedProperty(
        ReflectionProperty $parentProperty,
        ReflectionProperty $childProperty
    ) {
        $embeddedPropertyReflection = new ReflectionEmbeddedProperty(
            $parentProperty,
            $childProperty,
            $childProperty->getDeclaringClass()->getName()
        );

        $instantiator = new Instantiator();

        $object = $instantiator->instantiate($parentProperty->getDeclaringClass()->getName());

        $embeddedPropertyReflection->setValue($object, 'newValue');

        $this->assertSame('newValue', $embeddedPropertyReflection->getValue($object));

        $embeddedPropertyReflection->setValue($object, 'changedValue');

        $this->assertSame('changedValue', $embeddedPropertyReflection->getValue($object));
    }

    /**
     * Data provider
     *
     * @return ReflectionProperty[][]
     */
    public function getTestedReflectionProperties()
    {
        return array(
            array(
                $this->getReflectionProperty(
                    'Doctrine\\Tests\\Models\\Generic\\BooleanModel',
                    'id'
                ),
                $this->getReflectionProperty(
                    'Doctrine\\Tests\\Models\\Generic\\BooleanModel',
                    'id'
                ),
            ),
            // reflection on classes extending internal PHP classes:
            array(
                $this->getReflectionProperty(
                    'Doctrine\\Tests\\Models\\Reflection\\ArrayObjectExtendingClass',
                    'publicProperty'
                ),
                $this->getReflectionProperty(
                    'Doctrine\\Tests\\Models\\Reflection\\ArrayObjectExtendingClass',
                    'privateProperty'<commit_msg>Verifying that reflection properties that don't contain an embeddable will not crash reading properties, but will return `null` instead<commit_after><?php

namespace Doctrine\Tests\ORM\Mapping;

use Doctrine\Instantiator\Instantiator;
use Doctrine\ORM\Mapping\ReflectionEmbeddedProperty;
use ReflectionProperty;

/**
 * Tests for {@see \Doctrine\ORM\Mapping\ReflectionEmbeddedProperty}
 *
 * @covers \Doctrine\ORM\Mapping\ReflectionEmbeddedProperty
 */
class ReflectionEmbeddedPropertyTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @param ReflectionProperty $parentProperty
     * @param ReflectionProperty $childProperty
     *
     * @dataProvider getTestedReflectionProperties
     */
    public function testCanSetAndGetEmbeddedProperty(
        ReflectionProperty $parentProperty,
        ReflectionProperty $childProperty
    ) {
        $embeddedPropertyReflection = new ReflectionEmbeddedProperty(
            $parentProperty,
            $childProperty,
            $childProperty->getDeclaringClass()->getName()
        );

        $instantiator = new Instantiator();

        $object = $instantiator->instantiate($parentProperty->getDeclaringClass()->getName());

        $embeddedPropertyReflection->setValue($object, 'newValue');

        $this->assertSame('newValue', $embeddedPropertyReflection->getValue($object));

        $embeddedPropertyReflection->setValue($object, 'changedValue');

        $this->assertSame('changedValue', $embeddedPropertyReflection->getValue($object));
    }

    /**
     * @param ReflectionProperty $parentProperty
     * @param ReflectionProperty $childProperty
     *
     * @dataProvider getTestedReflectionProperties
     */
    public function testWillSkipReadingPropertiesFromNullEmbeddable(
        ReflectionProperty $parentProperty,
        ReflectionProperty $childProperty
    )
    {
        $embeddedPropertyReflection = new ReflectionEmbeddedProperty(
            $parentProperty,
            $childProperty,
            $childProperty->getDeclaringClass()->getName()
        );

        $instantiator = new Instantiator();

        $this->assertNull($embeddedPropertyReflection->getValue(
            $instantiator->instantiate($parentProperty->getDeclaringClass()->getName())
        ));
    }

    /**
     * Data provider
     *
     * @return ReflectionProperty[][]
     */
    public function getTestedReflectionProperties()
    {
        return array(
            array(
                $this->getReflectionProperty(
                    'Doctrine\\Tests\\Models\\Generic\\BooleanModel',
                    'id'
                ),
                $this->getReflectionProperty(
                    'Doctrine\\Tests\\Models\\Generic\\BooleanModel',
                    'id'
                ),
            ),
            // reflection on classes extending internal PHP classes:
            array(
                $this->getReflectionProperty(
                    'Doctrine\\Tests\\Models\\Reflection\\ArrayObjectExtendingClass',
                    'publicProperty'
                ),
                $this->getReflectionProperty(
                    'Doctrine\\Tests\\Models\\Reflection\\ArrayObjectExtendingClass',
                    'privateProperty'
{'fraction_non_alphanumeric': 0.10195305500806306, 'fraction_numerical': 0.0, 'mean_word_length': 2.385081867798666, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 40, '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': '375674', 'n_tokens_mistral': 1360, 'n_tokens_neox': 1268, 'n_words': 209}