Skip to content

Commit 9559fe9

Browse files
committed
[PropertyAccess] Added reproducer for adder/setter priority #29340 #28966
1 parent 53f28bf commit 9559fe9

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\PropertyAccess\Tests\Fixtures;
13+
14+
15+
class TestAdderVersusSetter
16+
{
17+
/** @var array */
18+
private $emails = array();
19+
20+
/**
21+
* @param array $emails
22+
*/
23+
public function setEmails($emails)
24+
{
25+
throw new \RuntimeException('Setter must NOT be called while adder exists');
26+
}
27+
28+
/**
29+
* @return array
30+
*/
31+
public function getEmails()
32+
{
33+
return $this->emails;
34+
}
35+
36+
/**
37+
* @param string $email
38+
*/
39+
public function addEmail($email)
40+
{
41+
$this->emails[] = $email;
42+
}
43+
44+
/**
45+
* @param string $email
46+
*/
47+
public function removeEmail($email)
48+
{
49+
$this->emails = array_diff($this->emails, array($email));
50+
}
51+
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\PropertyAccess\Exception\NoSuchIndexException;
1717
use Symfony\Component\PropertyAccess\PropertyAccessor;
1818
use Symfony\Component\PropertyAccess\Tests\Fixtures\ReturnTyped;
19+
use Symfony\Component\PropertyAccess\Tests\Fixtures\TestAdderVersusSetter;
1920
use Symfony\Component\PropertyAccess\Tests\Fixtures\TestClass;
2021
use Symfony\Component\PropertyAccess\Tests\Fixtures\TestClassIsWritable;
2122
use Symfony\Component\PropertyAccess\Tests\Fixtures\TestClassMagicCall;
@@ -722,4 +723,14 @@ public function testWriteToPluralPropertyWhileSingularOneExists()
722723
self::assertEquals(array('test@email.com'), $object->getEmails());
723724
self::assertNull($object->getEmail());
724725
}
726+
727+
public function testAdderHasHigherPriorityThanPluralSetter()
728+
{
729+
$object = new TestAdderVersusSetter();
730+
731+
$this->propertyAccessor->isWritable($object, 'emails'); //cache access info
732+
$this->propertyAccessor->setValue($object, 'emails', array('test@email.com'));
733+
734+
self::assertEquals(array('test@email.com'), $object->getEmails());
735+
}
725736
}

0 commit comments

Comments
 (0)