|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 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 Tests\AppBundle\Form\DataTransformer; |
| 13 | + |
| 14 | +use AppBundle\Entity\Tag; |
| 15 | +use AppBundle\Form\DataTransformer\TagArrayToStringTransformer; |
| 16 | +use Doctrine\Common\Persistence\ObjectManager; |
| 17 | +use Doctrine\ORM\EntityRepository; |
| 18 | + |
| 19 | +/** |
| 20 | + * Tests that tags are transformed correctly using the data transformer. |
| 21 | + * |
| 22 | + * See http://symfony.com/doc/current/testing/database.html |
| 23 | + */ |
| 24 | +class TagArrayToStringTransformerTest extends \PHPUnit\Framework\TestCase |
| 25 | +{ |
| 26 | + /** |
| 27 | + * Ensures that tags are created correctly. |
| 28 | + */ |
| 29 | + public function testCreateTheRightAmountOfTags() |
| 30 | + { |
| 31 | + $tags = $this->getMockedTransformer()->reverseTransform('Hello, Demo, How'); |
| 32 | + |
| 33 | + $this->assertCount(3, $tags); |
| 34 | + $this->assertSame('Hello', $tags[0]->getName()); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Ensures that empty tags and errors in the number of commas are |
| 39 | + * dealed correctly. |
| 40 | + */ |
| 41 | + public function testCreateTheRightAmountOfTagsWithTooManyCommas() |
| 42 | + { |
| 43 | + $transformer = $this->getMockedTransformer(); |
| 44 | + |
| 45 | + $this->assertCount(3, $transformer->reverseTransform('Hello, Demo,, How')); |
| 46 | + $this->assertCount(3, $transformer->reverseTransform('Hello, Demo, How,')); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Ensures that leading/trailing spaces are ignored for tag names. |
| 51 | + */ |
| 52 | + public function testTrimNames() |
| 53 | + { |
| 54 | + $tags = $this->getMockedTransformer()->reverseTransform(' Hello '); |
| 55 | + |
| 56 | + $this->assertSame('Hello', $tags[0]->getName()); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Ensures that duplicated tag names are ignored. |
| 61 | + */ |
| 62 | + public function testDuplicateNames() |
| 63 | + { |
| 64 | + $tags = $this->getMockedTransformer()->reverseTransform('Hello, Hello, Hello'); |
| 65 | + |
| 66 | + $this->assertCount(1, $tags); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Ensures that the transformer uses tags already persisted in the database. |
| 71 | + */ |
| 72 | + public function testUsesAlreadyDefinedTags() |
| 73 | + { |
| 74 | + $persistedTags = [ |
| 75 | + $this->createTag('Hello'), |
| 76 | + $this->createTag('World'), |
| 77 | + ]; |
| 78 | + $tags = $this->getMockedTransformer($persistedTags)->reverseTransform('Hello, World, How, Are, You'); |
| 79 | + |
| 80 | + $this->assertCount(5, $tags); |
| 81 | + $this->assertSame($persistedTags[0], $tags[0]); |
| 82 | + $this->assertSame($persistedTags[1], $tags[1]); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Ensures that the transformation from Tag instances to a simple string |
| 87 | + * works as expected. |
| 88 | + */ |
| 89 | + public function testTransform() |
| 90 | + { |
| 91 | + $persistedTags = [ |
| 92 | + $this->createTag('Hello'), |
| 93 | + $this->createTag('World'), |
| 94 | + ]; |
| 95 | + $transformed = $this->getMockedTransformer()->transform($persistedTags); |
| 96 | + |
| 97 | + $this->assertSame('Hello,World', $transformed); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * This helper method mocks the real TagArrayToStringTransformer class to |
| 102 | + * simplify the tests. See https://phpunit.de/manual/current/en/test-doubles.html. |
| 103 | + * |
| 104 | + * @param array $findByReturnValues The values returned when calling to the findBy() method |
| 105 | + * |
| 106 | + * @return TagArrayToStringTransformer |
| 107 | + */ |
| 108 | + private function getMockedTransformer($findByReturnValues = []) |
| 109 | + { |
| 110 | + $tagRepository = $this->getMockBuilder(EntityRepository::class) |
| 111 | + ->disableOriginalConstructor() |
| 112 | + ->getMock(); |
| 113 | + $tagRepository->expects($this->any()) |
| 114 | + ->method('findBy') |
| 115 | + ->will($this->returnValue($findByReturnValues)); |
| 116 | + |
| 117 | + $entityManager = $this |
| 118 | + ->getMockBuilder(ObjectManager::class) |
| 119 | + ->disableOriginalConstructor() |
| 120 | + ->getMock(); |
| 121 | + $entityManager->expects($this->any()) |
| 122 | + ->method('getRepository') |
| 123 | + ->will($this->returnValue($tagRepository)); |
| 124 | + |
| 125 | + return new TagArrayToStringTransformer($entityManager); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * This helper method creates a Tag instance for the given tag name. |
| 130 | + * |
| 131 | + * @param string $name |
| 132 | + * |
| 133 | + * @return Tag |
| 134 | + */ |
| 135 | + private function createTag($name) |
| 136 | + { |
| 137 | + $tag = new Tag(); |
| 138 | + $tag->setName($name); |
| 139 | + |
| 140 | + return $tag; |
| 141 | + } |
| 142 | +} |
0 commit comments