8000 bug #9308 [DoctrineBridge] Loosened CollectionToArrayTransformer::tra… · symfony/symfony@0080399 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0080399

Browse files
committed
bug #9308 [DoctrineBridge] Loosened CollectionToArrayTransformer::transform() to accept arrays (bschussek)
This PR was merged into the 2.2 branch. Discussion ---------- [DoctrineBridge] Loosened CollectionToArrayTransformer::transform() to accept arrays | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Previously, writing an association getter like this was impossible: ```php public function addTag(Tag $tag) { ... } public function removeTag(Tag $tag) { ... } public function getTags() { return $this->tags->toArray(); } ``` Using `toArray()` is a useful way to restrict modifications of the collection to the specified methods. But previously, CollectionToArrayTransformer failed in this case, because it did not accept arrays as input. Commits ------- 55001ab [DoctrineBridge] Loosened CollectionToArrayTransformer::transform() to accept arrays
2 parents 3e684b4 + 55001ab commit 0080399

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

src/Symfony/Bridge/Doctrine/Form/DataTransformer/CollectionToArrayTransformer.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ public function transform($collection)
3636
return array();
3737
}
3838

39+
// For cases when the collection getter returns $collection->toArray()
40+
// in order to prevent modifications of the returned collection
41+
if (is_array($collection)) {
42+
return $collection;
43+
}
44+
3945
if (!$collection instanceof Collection) {
4046
throw new TransformationFailedException('Expected a Doctrine\Common\Collections\Collection object.');
4147
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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\Bridge\Doctrine\Tests\Form\DataTransformer;
13+
14+
use Doctrine\Common\Collections\ArrayCollection;
15+
use Symfony\Bridge\Doctrine\Form\DataTransformer\CollectionToArrayTransformer;
16+
17+
/**
18+
* @author Bernhard Schussek <bschussek@gmail.com>
19+
*/
20+
class CollectionToArrayTransformerTest extends \PHPUnit_Framework_TestCase
21+
{
22+
/**
23+
* @var CollectionToArrayTransformer
24+
*/
25+
private $transformer;
26+
27+
protected function setUp()
28+
{
29+
$this->transformer = new CollectionToArrayTransformer();
30+
}
31+
32+
public function testTransform()
33+
{
34+
$array = array(
35+
2 => 'foo',
36+
3 => 'bar',
37+
);
38+
39+
$this->assertSame($array, $this->transformer->transform(new ArrayCollection($array)));
40+
}
41+
42+
/**
43+
* This test is needed for cases when getXxxs() in the entity returns the
44+
* result of $collection->toArray(), in order to prevent modifications of
45+
* the inner collection.
46+
*
47+
* See https://github.com/symfony/symfony/pull/9308
48+
*/
49+
public function testTransformArray()
50+
{
51+
$array = array(
52+
2 => 'foo',
53+
3 => 'bar',
54+
);
55+
56+
$this->assertSame($array, $this->transformer->transform($array));
57+
}
58+
59+
public function testTransformNull()
60+
{
61+
$this->assertSame(array(), $this->transformer->transform(null));
62+
}
63+
64+
/**
65+
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
66+
*/
67+
public function testTransformExpectsArrayOrCollection()
68+
{
69+
$this->transformer->transform('Foo');
70+
}
71+
72+
public function testReverseTransform()
73+
{
74+
$array = array(
75+
2 => 'foo',
76+
3 => 'bar',
77+
);
78+
79+
$this->assertEquals(new ArrayCollection($array), $this->transformer->reverseTransform($array));
80+
}
81+
82+
public function testReverseTransformEmpty()
83+
{
84+
$this->assertEquals(new ArrayCollection(), $this->transformer->reverseTransform(''));
85+
}
86+
87+
public function testReverseTransformNull()
88+
{
89+
$this->assertEquals(new ArrayCollection(), $this->transformer->reverseTransform(null));
90+
}
91+
}

0 commit comments

Comments
 (0)
0