|
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; |
8 | 11 | use Tests\Torr\SimpleNormalizer\Fixture\DummyVO; |
| 12 | +use Tests\Torr\SimpleNormalizer\Fixture\DummyVONormalizer; |
9 | 13 | use Torr\SimpleNormalizer\Exception\IncompleteNormalizationException; |
10 | 14 | use Torr\SimpleNormalizer\Normalizer\SimpleNormalizer; |
11 | | -use Torr\SimpleNormalizer\Normalizer\SimpleObjectNormalizerInterface; |
12 | 15 | use Torr\SimpleNormalizer\Normalizer\Validator\ValidJsonVerifier; |
13 | 16 |
|
14 | 17 | /** |
@@ -124,27 +127,103 @@ 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 DummyVONormalizer(5)); |
| 140 | + |
| 141 | + $normalizer = new SimpleNormalizer( |
| 142 | + objectNormalizers: $locator, |
| 143 | + isDebug: true, |
| 144 | + validJsonVerifier: new ValidJsonVerifier(), |
| 145 | + ); |
| 146 | + |
| 147 | + $normalizer->normalize(new DummyVO(5)); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + */ |
| 152 | + public function testWithEntityManagerButNoMapping () : void |
| 153 | + { |
| 154 | + $metadataFactory = $this->createMock(ClassMetadataFactory::class); |
| 155 | + $metadataFactory |
| 156 | + ->expects(self::once()) |
| 157 | + ->method("hasMetadataFor") |
| 158 | + ->with(DummyVO::class) |
| 159 | + ->willReturn(false); |
| 160 | + |
| 161 | + $entityManager = $this->createMock(EntityManagerInterface::class); |
| 162 | + $entityManager->method("getMetadataFactory")->willReturn($metadataFactory); |
| 163 | + |
| 164 | + $locator = $this->createMock(ServiceLocator::class); |
| 165 | + |
| 166 | + $locator->expects(self::once()) |
| 167 | + ->method("get") |
| 168 | + ->with(DummyVO::class) |
| 169 | + ->willReturn(new DummyVONormalizer(5)); |
| 170 | + |
| 171 | + $normalizer = new SimpleNormalizer( |
| 172 | + objectNormalizers: $locator, |
| 173 | + isDebug: true, |
| 174 | + validJsonVerifier: new ValidJsonVerifier(), |
| 175 | + entityManager: $entityManager, |
| 176 | + ); |
| 177 | + |
| 178 | + $normalizer->normalize(new DummyVO(5)); |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + */ |
| 183 | + public function testWithEntityManagerWithMapping () : void |
| 184 | + { |
| 185 | + $classMetaData = new ClassMetadata("SomeClass"); |
| 186 | + |
| 187 | + $metadataFactory = $this->createMock(ClassMetadataFactory::class); |
| 188 | + $metadataFactory |
| 189 | + ->expects(self::once()) |
| 190 | + ->method("hasMetadataFor") |
| 191 | + ->with(DummyVO::class) |
| 192 | + ->willReturn(true); |
| 193 | + |
| 194 | + $metadataFactory |
| 195 | + ->expects(self::once()) |
| 196 | + ->method("getMetadataFor") |
| 197 | + ->with(DummyVO::class) |
| 198 | + ->willReturn($classMetaData); |
| 199 | + |
| 200 | + $entityManager = $this->createMock(EntityManagerInterface::class); |
| 201 | + $entityManager->method("getMetadataFactory")->willReturn($metadataFactory); |
| 202 | + |
| 203 | + $locator = $this->createMock(ServiceLocator::class); |
| 204 | + |
| 205 | + $locator->expects(self::once()) |
| 206 | + ->method("get") |
| 207 | + ->with("SomeClass") |
| 208 | + ->willReturn(new DummyVONormalizer(5)); |
| 209 | + |
| 210 | + $normalizer = new SimpleNormalizer( |
| 211 | + objectNormalizers: $locator, |
| 212 | + isDebug: true, |
| 213 | + validJsonVerifier: new ValidJsonVerifier(), |
| 214 | + entityManager: $entityManager, |
| 215 | + ); |
| 216 | + |
| 217 | + $normalizer->normalize(new DummyVO(5)); |
| 218 | + } |
| 219 | + |
127 | 220 | /** |
128 | 221 | * @return ServiceLocator<mixed> |
129 | 222 | */ |
130 | 223 | private function createNormalizerObjectNormalizers (mixed $returnValue) : ServiceLocator |
131 | 224 | { |
132 | 225 | return new ServiceLocator([ |
133 | | - DummyVO::class => static fn () => new readonly class($returnValue) implements SimpleObjectNormalizerInterface { |
134 | | - public function __construct ( |
135 | | - private mixed $returnValue, |
136 | | - ) {} |
137 | | - |
138 | | - public function normalize (object $value, array $context, SimpleNormalizer $normalizer) : mixed |
139 | | - { |
140 | | - return $this->returnValue; |
141 | | - } |
142 | | - |
143 | | - public static function getNormalizedType () : string |
144 | | - { |
145 | | - return DummyVO::class; |
146 | | - } |
147 | | - }, |
| 226 | + DummyVO::class => static fn () => new DummyVONormalizer($returnValue), |
148 | 227 | ]); |
149 | 228 | } |
150 | 229 | } |
0 commit comments