|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright (c) 2017 DarkWeb Design |
| 4 | + * |
| 5 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | + * of this software and associated documentation files (the "Software"), to deal |
| 7 | + * in the Software without restriction, including without limitation the rights |
| 8 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | + * copies of the Software, and to permit persons to whom the Software is |
| 10 | + * furnished to do so, subject to the following conditions: |
| 11 | + * |
| 12 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 13 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 14 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 15 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 16 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 17 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 18 | + * SOFTWARE. |
| 19 | + */ |
| 20 | + |
| 21 | +namespace DarkWebDesign\SymfonyAddon\Transformer\Tests; |
| 22 | + |
| 23 | +use DarkWebDesign\SymfonyAddon\Transformer\EntityToIdentifierTransformer; |
| 24 | +use DarkWebDesign\SymfonyAddon\Transformer\Tests\Models\City; |
| 25 | +use DarkWebDesign\SymfonyAddon\Transformer\Tests\Models\PointOfInterest; |
| 26 | +use PHPUnit_Framework_TestCase; |
| 27 | +use stdClass; |
| 28 | + |
| 29 | +class EntityToIdentifierTransformerTest extends PHPUnit_Framework_TestCase |
| 30 | +{ |
| 31 | + /** @var \DarkWebDesign\SymfonyAddon\Transformer\Tests\Models\City */ |
| 32 | + private $entity; |
| 33 | + |
| 34 | + /** @var string */ |
| 35 | + private $className; |
| 36 | + |
| 37 | + /** @var int */ |
| 38 | + private $identifier; |
| 39 | + |
| 40 | + /** @var \Doctrine\Common\Persistence\ObjectManager */ |
| 41 | + private $entityManager; |
| 42 | + |
| 43 | + /** @var \Doctrine\Common\Persistence\ObjectRepository */ |
| 44 | + private $repository; |
| 45 | + |
| 46 | + /** @var \Doctrine\Common\Persistence\Mapping\ClassMetadata */ |
| 47 | + private $metadata; |
| 48 | + |
| 49 | + protected function setUp() |
| 50 | + { |
| 51 | + $this->entity = new City(); |
| 52 | + $this->entity->setId(123); |
| 53 | + |
| 54 | + $this->className = get_class($this->entity); |
| 55 | + $this->identifier = $this->entity->getId(); |
| 56 | + |
| 57 | + $this->entityManager = $this->getMock('Doctrine\Common\Persistence\ObjectManager'); |
| 58 | + $this->repository = $this->getMock('Doctrine\Common\Persistence\ObjectRepository'); |
| 59 | + $this->metadata = $this->getMock('Doctrine\Common\Persistence\Mapping\ClassMetadata'); |
| 60 | + |
| 61 | + $this->entityManager->method('getRepository')->willReturn($this->repository); |
| 62 | + $this->entityManager->method('getClassMetadata')->willReturn($this->metadata); |
| 63 | + |
| 64 | + $this->metadata->method('getIdentifierValues')->willReturn(array('id' => $this->identifier)); |
| 65 | + |
| 66 | + $this->metadata->isIdentifierComposite = false; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @expectedException \Symfony\Component\Form\Exception\InvalidArgumentException |
| 71 | + * @expectedExceptionMessage Expected an entity with a single identifier. |
| 72 | + */ |
| 73 | + public function testConstructIdentifierComposite() |
| 74 | + { |
| 75 | + $this->metadata->isIdentifierComposite = true; |
| 76 | + |
| 77 | + new EntityToIdentifierTransformer($this->entityManager, $this->className); |
| 78 | + } |
| 79 | + |
| 80 | + public function testTransform() |
| 81 | + { |
| 82 | + $transformer = new EntityToIdentifierTransformer($this->entityManager, $this->className); |
| 83 | + |
| 84 | + $identifier = $transformer->transform($this->entity); |
| 85 | + |
| 86 | + $this->assertSame($this->identifier, $identifier); |
| 87 | + } |
| 88 | + |
| 89 | + public function testTransformNull() |
| 90 | + { |
| 91 | + $transformer = new EntityToIdentifierTransformer($this->entityManager, $this->className); |
| 92 | + |
| 93 | + $identifier = $transformer->transform(null); |
| 94 | + |
| 95 | + $this->assertNull($identifier); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * @param mixed $value |
| 100 | + * |
| 101 | + * @dataProvider providerTransformNoObject |
| 102 | + * |
| 103 | + * @expectedException \Symfony\Component\Form\Exception\TransformationFailedException |
| 104 | + * @expectedExceptionMessage Expected an object. |
| 105 | + */ |
| 106 | + public function testTransformNoObject($value) |
| 107 | + { |
| 108 | + $transformer = new EntityToIdentifierTransformer($this->entityManager, $this->className); |
| 109 | + |
| 110 | + $transformer->transform($value); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * @return array[] |
| 115 | + */ |
| 116 | + public function providerTransformNoObject() |
| 117 | + { |
| 118 | + return array( |
| 119 | + 'bool' => array(true), |
| 120 | + 'int' => array(1), |
| 121 | + 'float' => array(1.2), |
| 122 | + 'string' => array('foo'), |
| 123 | + 'array' => array(array('foo', 'bar')), |
| 124 | + 'resource' => array(tmpfile()), |
| 125 | + 'callable' => array(function () {}) |
| 126 | + ); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * @expectedException \Symfony\Component\Form\Exception\TransformationFailedException |
| 131 | + * @expectedExceptionMessage Expected entity DarkWebDesign\SymfonyAddon\Transformer\Tests\Models\City. |
| 132 | + */ |
| 133 | + public function testTransformInvalidEntity() |
| 134 | + { |
| 135 | + $transformer = new EntityToIdentifierTransformer($this->entityManager, $this->className); |
| 136 | + |
| 137 | + $entity = new PointOfInterest(); |
| 138 | + |
| 139 | + $transformer->transform($entity); |
| 140 | + } |
| 141 | + |
| 142 | + public function testReverseTransform() |
| 143 | + { |
| 144 | + $transformer = new EntityToIdentifierTransformer($this->entityManager, $this->className); |
| 145 | + |
| 146 | + $this->repository->method('find')->willReturn($this->entity); |
| 147 | + |
| 148 | + $entity = $transformer->reverseTransform($this->identifier); |
| 149 | + |
| 150 | + $this->assertSame($this->identifier, $entity->getId()); |
| 151 | + } |
| 152 | + |
| 153 | + public function testReverseTransformNull() |
| 154 | + { |
| 155 | + $transformer = new EntityToIdentifierTransformer($this->entityManager, $this->className); |
| 156 | + |
| 157 | + $entity = $transformer->reverseTransform(null); |
| 158 | + |
| 159 | + $this->assertNull($entity); |
| 160 | + } |
| 161 | + |
| 162 | + public function testReverseTransformEmptyString() |
| 163 | + { |
| 164 | + $transformer = new EntityToIdentifierTransformer($this->entityManager, $this->className); |
| 165 | + |
| 166 | + $entity = $transformer->reverseTransform(''); |
| 167 | + |
| 168 | + $this->assertNull($entity); |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * @param mixed $value |
| 173 | + * |
| 174 | + * @dataProvider providerReverseTransformNoScalar |
| 175 | + * |
| 176 | + * @expectedException \Symfony\Component\Form\Exception\TransformationFailedException |
| 177 | + * @expectedExceptionMessage Expected a scalar. |
| 178 | + */ |
| 179 | + public function testReverseTransformNoScalar($value) |
| 180 | + { |
| 181 | + $transformer = new EntityToIdentifierTransformer($this->entityManager, $this->className); |
| 182 | + |
| 183 | + $transformer->reverseTransform($value); |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * @return array[] |
| 188 | + */ |
| 189 | + public function providerReverseTransformNoScalar() |
| 190 | + { |
| 191 | + return array( |
| 192 | + 'array' => array(array('foo', 'bar')), |
| 193 | + 'object' => array(new stdClass()), |
| 194 | + 'resource' => array(tmpfile()), |
| 195 | + 'callable' => array(function () {}), |
| 196 | + ); |
| 197 | + } |
| 198 | + |
| 199 | + /** |
| 200 | + * @expectedException \Symfony\Component\Form\Exception\TransformationFailedException |
| 201 | + * @expectedExceptionMessage Entity DarkWebDesign\SymfonyAddon\Transformer\Tests\Models\City with identifier "123" not found. |
| 202 | + */ |
| 203 | + public function testReverseTransformEntityNotFound() |
| 204 | + { |
| 205 | + $transformer = new EntityToIdentifierTransformer($this->entityManager, $this->className); |
| 206 | + |
| 207 | + $this->repository->method('find')->willReturn(null); |
| 208 | + |
| 209 | + $transformer->reverseTransform($this->identifier); |
| 210 | + } |
| 211 | +} |
0 commit comments