Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0cc177f

Browse files
[PropertyAccess] Fix accessing public property in Object
New test Dynamic property with magic getter
1 parent 2381f41 commit 0cc177f

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Symfony\Component\PropertyAccess\Tests\Fixtures;
4+
5+
class TestPublicPropertyGetterOnObjectMagicGet
6+
{
7+
public $a = 'A';
8+
private $b = 'B';
9+
10+
public function __get(string $propoerty)
11+
{
12+
if ($propoerty === 'b') {
13+
return $this->b;
14+
}
15+
}
16+
}

src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
use Symfony\Component\PropertyAccess\Tests\Fixtures\TestClassTypeErrorInsideCall;
3434
use Symfony\Component\PropertyAccess\Tests\Fixtures\TestPublicPropertyDynamicallyCreated;
3535
use Symfony\Component\PropertyAccess\Tests\Fixtures\TestPublicPropertyGetterOnObject;
36+
use Symfony\Component\PropertyAccess\Tests\Fixtures\TestPublicPropertyGetterOnObjectMagicGet;
3637
use Symfony\Component\PropertyAccess\Tests\Fixtures\TestSingularAndPluralProps;
3738
use Symfony\Component\PropertyAccess\Tests\Fixtures\Ticket5775Object;
3839
use Symfony\Component\PropertyAccess\Tests\Fixtures\TypeHinted;
@@ -974,4 +975,20 @@ public function testGetDynamicPublicProperty()
974975

975976
$this->assertSame($value, $this->propertyAccessor->getValue($object, $path));
976977
}
978+
979+
public function testGetDynamicPublicPropertyWithMagicGetterDisallow()
980+
{
981+
$this->expectException(NoSuchPropertyException::class);
982+
$object = new TestPublicPropertyGetterOnObjectMagicGet();
983+
$propertyAccessor = new PropertyAccessor(PropertyAccessor::DISALLOW_MAGIC_METHODS);
984+
$propertyAccessor->getValue($object, 'c');
985+
}
986+
987+
public function testGetDynamicPublicPropertyWithMagicGetterAllow()
988+
{
989+
$value = 'B';
990+
$path = 'b';
991+
$object = new TestPublicPropertyGetterOnObjectMagicGet();
992+
$this->assertSame($value, $this->propertyAccessor->getValue($object, $path));
993+
}
977994
}

0 commit comments

Comments
 (0)