|
2 | 2 |
|
3 | 3 | namespace Tests\Torr\SimpleNormalizer\Normalizer; |
4 | 4 |
|
| 5 | +use Doctrine\ORM\EntityManagerInterface; |
| 6 | +use Doctrine\ORM\Mapping\ClassMetadata; |
| 7 | +use Doctrine\ORM\Mapping\ClassMetadataFactory; |
5 | 8 | use PHPUnit\Framework\Attributes\DataProvider; |
6 | 9 | use PHPUnit\Framework\TestCase; |
7 | 10 | use Symfony\Component\DependencyInjection\ServiceLocator; |
@@ -124,6 +127,126 @@ public function testInvalidNestedNormalizer () : void |
124 | 127 | ]); |
125 | 128 | } |
126 | 129 |
|
| 130 | + /** |
| 131 | + */ |
| 132 | + public function testWithoutEntityManager () : void |
| 133 | + { |
| 134 | + $locator = $this->createMock(ServiceLocator::class); |
| 135 | + |
| 136 | + $locator->expects(self::once()) |
| 137 | + ->method("get") |
| 138 | + ->with(DummyVO::class) |
| 139 | + ->willReturn(new readonly class implements SimpleObjectNormalizerInterface { |
| 140 | + public function normalize (object $value, array $context, SimpleNormalizer $normalizer) : int |
| 141 | + { |
| 142 | + return 5; |
| 143 | + } |
| 144 | + |
| 145 | + public static function getNormalizedType () : string |
| 146 | + { |
| 147 | + return DummyVO::class; |
| 148 | + } |
| 149 | + }); |
| 150 | + |
| 151 | + $normalizer = new SimpleNormalizer( |
| 152 | + objectNormalizers: $locator, |
| 153 | + isDebug: true, |
| 154 | + validJsonVerifier: new ValidJsonVerifier(), |
| 155 | + ); |
| 156 | + |
| 157 | + $normalizer->normalize(new DummyVO(5)); |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + */ |
| 162 | + public function testWithEntityManagerButNoMapping () : void |
| 163 | + { |
| 164 | + $metadataFactory = $this->createMock(ClassMetadataFactory::class); |
| 165 | + $metadataFactory |
| 166 | + ->expects(self::once()) |
| 167 | + ->method("hasMetadataFor") |
| 168 | + ->with(DummyVO::class) |
| 169 | + ->willReturn(false); |
| 170 | + |
| 171 | + $entityManager = $this->createMock(EntityManagerInterface::class); |
| 172 | + $entityManager->method("getMetadataFactory")->willReturn($metadataFactory); |
| 173 | + |
| 174 | + $locator = $this->createMock(ServiceLocator::class); |
| 175 | + |
| 176 | + $locator->expects(self::once()) |
| 177 | + ->method("get") |
| 178 | + ->with(DummyVO::class) |
| 179 | + ->willReturn(new readonly class implements SimpleObjectNormalizerInterface { |
| 180 | + public function normalize (object $value, array $context, SimpleNormalizer $normalizer) : int |
| 181 | + { |
| 182 | + return 5; |
| 183 | + } |
| 184 | + |
| 185 | + public static function getNormalizedType () : string |
| 186 | + { |
| 187 | + return DummyVO::class; |
| 188 | + } |
| 189 | + }); |
| 190 | + |
| 191 | + $normalizer = new SimpleNormalizer( |
| 192 | + objectNormalizers: $locator, |
| 193 | + isDebug: true, |
| 194 | + validJsonVerifier: new ValidJsonVerifier(), |
| 195 | + entityManager: $entityManager, |
| 196 | + ); |
| 197 | + |
| 198 | + $normalizer->normalize(new DummyVO(5)); |
| 199 | + } |
| 200 | + |
| 201 | + /** |
| 202 | + */ |
| 203 | + public function testWithEntityManagerWithMapping () : void |
| 204 | + { |
| 205 | + $classMetaData = new ClassMetadata("SomeClass"); |
| 206 | + |
| 207 | + $metadataFactory = $this->createMock(ClassMetadataFactory::class); |
| 208 | + $metadataFactory |
| 209 | + ->expects(self::once()) |
| 210 | + ->method("hasMetadataFor") |
| 211 | + ->with(DummyVO::class) |
| 212 | + ->willReturn(true); |
| 213 | + |
| 214 | + $metadataFactory |
| 215 | + ->expects(self::once()) |
| 216 | + ->method("getMetadataFor") |
| 217 | + ->with(DummyVO::class) |
| 218 | + ->willReturn($classMetaData); |
| 219 | + |
| 220 | + $entityManager = $this->createMock(EntityManagerInterface::class); |
| 221 | + $entityManager->method("getMetadataFactory")->willReturn($metadataFactory); |
| 222 | + |
| 223 | + $locator = $this->createMock(ServiceLocator::class); |
| 224 | + |
| 225 | + $locator->expects(self::once()) |
| 226 | + ->method("get") |
| 227 | + ->with("SomeClass") |
| 228 | + ->willReturn(new readonly class implements SimpleObjectNormalizerInterface { |
| 229 | + public function normalize (object $value, array $context, SimpleNormalizer $normalizer) : int |
| 230 | + { |
| 231 | + return 5; |
| 232 | + } |
| 233 | + |
| 234 | + public static function getNormalizedType () : string |
| 235 | + { |
| 236 | + return DummyVO::class; |
| 237 | + } |
| 238 | + }); |
| 239 | + |
| 240 | + $normalizer = new SimpleNormalizer( |
| 241 | + objectNormalizers: $locator, |
| 242 | + isDebug: true, |
| 243 | + validJsonVerifier: new ValidJsonVerifier(), |
| 244 | + entityManager: $entityManager, |
| 245 | + ); |
| 246 | + |
| 247 | + $normalizer->normalize(new DummyVO(5)); |
| 248 | + } |
| 249 | + |
127 | 250 | /** |
128 | 251 | * @return ServiceLocator<mixed> |
129 | 252 | */ |
|
0 commit comments