Doctrine\Tests\ODM\CouchDB\Mapping\EmbeddedDocumentSerializerTest::testIsChanged PHP Method

testIsChanged() public method

public testIsChanged ( )
    public function testIsChanged()
    {
        $embedderMetadata = $this->metadataFactory->getMetadataFor('Doctrine\\Tests\\ODM\\CouchDB\\Mapping\\Embedder');
        $fieldMapping = $embedderMetadata->fieldMappings['embedded'];
        $instance = $this->serializer->createEmbeddedDocument($this->arrayDataFixture, $fieldMapping);
        $this->assertFalse($this->serializer->isChanged($instance, $this->arrayDataFixture, $fieldMapping));
        $name = $instance->name;
        $instance->name = 'changed';
        $this->assertTrue($this->serializer->isChanged($instance, $this->arrayDataFixture, $fieldMapping));
        $instance->name = $name;
        $this->assertFalse($this->serializer->isChanged($instance, $this->arrayDataFixture, $fieldMapping));
        $nestedName = $instance->embeds['one']->nestedName;
        $instance->embeds['one']->nestedName = 'changed';
        $this->assertTrue($this->serializer->isChanged($instance, $this->arrayDataFixture, $fieldMapping));
        $instance->embeds['one']->nestedName = $nestedName;
        $this->assertFalse($this->serializer->isChanged($instance, $this->arrayDataFixture, $fieldMapping));
        // testing same data representation but different class
        $newNested = new Nested2();
        $newNested->nestedName = $nestedName;
        $instance->embeds['one'] = $newNested;
        $this->assertTrue($this->serializer->isChanged($instance, $this->arrayDataFixture, $fieldMapping));
        $newNested = new Nested();
        $newNested->nestedName = $nestedName;
        $instance->embeds['one'] = $newNested;
        $this->assertFalse($this->serializer->isChanged($instance, $this->arrayDataFixture, $fieldMapping));
        // testing that to null a property is a change
        $instance->embeds['one']->nestedName = null;
        $this->assertTrue($this->serializer->isChanged($instance, $this->arrayDataFixture, $fieldMapping));
        $instance->embeds['one']->nestedName = $nestedName;
        $this->assertFalse($this->serializer->isChanged($instance, $this->arrayDataFixture, $fieldMapping));
        // adding a new value is a change
        $instance->embeds['three'] = new Nested();
        $this->assertTrue($this->serializer->isChanged($instance, $this->arrayDataFixture, $fieldMapping));
        unset($instance->embeds['three']);
        $this->assertFalse($this->serializer->isChanged($instance, $this->arrayDataFixture, $fieldMapping));
        // removing one is a change
        unset($instance->embeds['two']);
        $this->assertTrue($this->serializer->isChanged($instance, $this->arrayDataFixture, $fieldMapping));
        // same number of embeds, but different key
        $instance->embeds['1'] = new Nested();
        $this->assertTrue($this->serializer->isChanged($instance, $this->arrayDataFixture, $fieldMapping));
        // -----------------------------------------------------------------------------
        //  reset things
        // -----------------------------------------------------------------------------
        $instance = $this->serializer->createEmbeddedDocument($this->arrayDataFixture, $embedderMetadata->fieldMappings['embedded']);
        $this->assertFalse($this->serializer->isChanged($instance, $this->arrayDataFixture, $fieldMapping));
        // adding an EmbedOne type of embedded document
        // also tesing the no targetDocument case
        $instance->embedOne = new Nested();
        $this->assertTrue($this->serializer->isChanged($instance, $this->arrayDataFixture, $fieldMapping));
        $fixture = $this->arrayDataFixture;
        $fixture['embedOne'] = array('doctrine_metadata' => array('type' => 'Doctrine.Tests.ODM.CouchDB.Mapping.Nested'));
        $this->assertFalse($this->serializer->isChanged($instance, $fixture, $fieldMapping));
        $instance->embedOne->nestedName = 'so this is nested';
        $this->assertTrue($this->serializer->isChanged($instance, $fixture, $fieldMapping));
    }