-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bug #9308 [DoctrineBridge] Loosened CollectionToArrayTransformer::tra…
…nsform() 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
- Loading branch information
Showing
2 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
src/Symfony/Bridge/Doctrine/Tests/Form/DataTransformer/CollectionToArrayTransformerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bridge\Doctrine\Tests\Form\DataTransformer; | ||
|
||
use Doctrine\Common\Collections\ArrayCollection; | ||
use Symfony\Bridge\Doctrine\Form\DataTransformer\CollectionToArrayTransformer; | ||
|
||
/** | ||
* @author Bernhard Schussek <bschussek@gmail.com> | ||
*/ | ||
class CollectionToArrayTransformerTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var CollectionToArrayTransformer | ||
*/ | ||
private $transformer; | ||
|
||
protected function setUp() | ||
{ | ||
$this->transformer = new CollectionToArrayTransformer(); | ||
} | ||
|
||
public function testTransform() | ||
{ | ||
$array = array( | ||
2 => 'foo', | ||
3 => 'bar', | ||
); | ||
|
||
$this->assertSame($array, $this->transformer->transform(new ArrayCollection($array))); | ||
} | ||
|
||
/** | ||
* This test is needed for cases when getXxxs() in the entity returns the | ||
* result of $collection->toArray(), in order to prevent modifications of | ||
* the inner collection. | ||
* | ||
* See https://github.com/symfony/symfony/pull/9308 | ||
*/ | ||
public function testTransformArray() | ||
{ | ||
$array = array( | ||
2 => 'foo', | ||
3 => 'bar', | ||
); | ||
|
||
$this->assertSame($array, $this->transformer->transform($array)); | ||
} | ||
|
||
public function testTransformNull() | ||
{ | ||
$this->assertSame(array(), $this->transformer->transform(null)); | ||
} | ||
|
||
/** | ||
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException | ||
*/ | ||
public function testTransformExpectsArrayOrCollection() | ||
{ | ||
$this->transformer->transform('Foo'); | ||
} | ||
|
||
public function testReverseTransform() | ||
{ | ||
$array = array( | ||
2 => 'foo', | ||
3 => 'bar', | ||
); | ||
|
||
$this->assertEquals(new ArrayCollection($array), $this->transformer->reverseTransform($array)); | ||
} | ||
|
||
public function testReverseTransformEmpty() | ||
{ | ||
$this->assertEquals(new ArrayCollection(), $this->transformer->reverseTransform('')); | ||
} | ||
|
||
public function testReverseTransformNull() | ||
{ | ||
$this->assertEquals(new ArrayCollection(), $this->transformer->reverseTransform(null)); | ||
} | ||
} |