Skip to content

Commit

Permalink
Fix reverseTransform method (#552)
Browse files Browse the repository at this point in the history
A variable name was wrong and tests have been added.
  • Loading branch information
franmomu committed Mar 22, 2021
1 parent cc89819 commit 0e3605d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Model/ModelManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ public function reverseTransform(object $object, array $array = []): void
foreach ($array as $name => $value) {
$property = $this->getFieldName($metadata, $name);

$this->propertyAccessor->setValue($instance, $property, $value);
$this->propertyAccessor->setValue($object, $property, $value);
}
}

Expand Down
50 changes: 48 additions & 2 deletions tests/Model/ModelManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,11 @@ public function testGetParentMetadataForProperty(): void
$this->assertSame($metadata->fieldMappings[$lastPropertyName]['type'], 'bool');
}

/**
* NEXT_MAJOR: Remove this test.
*
* @group legacy
*/
public function testModelReverseTransformWithSetter(): void
{
$class = TestDocument::class;
Expand All @@ -252,6 +257,11 @@ public function testModelReverseTransformWithSetter(): void
$this->assertTrue($object->schwifty);
}

/**
* NEXT_MAJOR: Remove this test.
*
* @group legacy
*/
public function testModelReverseTransformFailsWithPrivateSetter(): void
{
$class = SimpleDocumentWithPrivateSetter::class;
Expand All @@ -262,6 +272,11 @@ public function testModelReverseTransformFailsWithPrivateSetter(): void
$manager->modelReverseTransform($class, ['schmeckles' => 42]);
}

/**
* NEXT_MAJOR: Remove this test.
*
* @group legacy
*/
public function testModelReverseTransformFailsWithPrivateProperties(): void
{
$class = TestDocument::class;
Expand All @@ -272,14 +287,45 @@ public function testModelReverseTransformFailsWithPrivateProperties(): void
$manager->modelReverseTransform($class, ['plumbus' => 42]);
}

public function testModelReverseTransformFailsWithPrivateProperties2(): void
public function testReverseTransformWithSetter(): void
{
$class = TestDocument::class;

$manager = $this->createModelManagerForClass($class);
$testDocument = new TestDocument();

$manager->reverseTransform(
$testDocument,
[
'schmeckles' => 42,
'multi_word_property' => 'hello',
'schwifty' => true,
]
);

$this->assertSame(42, $testDocument->getSchmeckles());
$this->assertSame('hello', $testDocument->getMultiWordProperty());
$this->assertTrue($testDocument->schwifty);
}

public function testReverseTransformFailsWithPrivateSetter(): void
{
$class = SimpleDocumentWithPrivateSetter::class;
$manager = $this->createModelManagerForClass($class);

$this->expectException(NoSuchPropertyException::class);

$manager->reverseTransform(new SimpleDocumentWithPrivateSetter(1), ['schmeckles' => 42]);
}

public function testReverseTransformFailsWithPrivateProperties(): void
{
$class = TestDocument::class;
$manager = $this->createModelManagerForClass($class);

$this->expectException(NoSuchPropertyException::class);

$manager->modelReverseTransform($class, ['plumbus' => 42]);
$manager->reverseTransform(new TestDocument(), ['plumbus' => 42]);
}

/**
Expand Down

0 comments on commit 0e3605d

Please sign in to comment.