EventMappingBuilderTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Tests\Unit\Service\ApiResourceBuilder\Freemium;
  4. use App\ApiResources\Freemium\FreemiumEvent;
  5. use App\Entity\Booking\Event;
  6. use App\Entity\Booking\EventGender;
  7. use App\Entity\Core\AddressPostal;
  8. use App\Entity\Core\Categories;
  9. use App\Entity\Core\Country;
  10. use App\Entity\Core\File;
  11. use App\Entity\Organization\Organization;
  12. use App\Entity\Place\Place;
  13. use App\Service\ApiResourceBuilder\Freemium\EventMappingBuilder;
  14. use Doctrine\ORM\EntityManagerInterface;
  15. use PHPUnit\Framework\MockObject\MockObject;
  16. use PHPUnit\Framework\TestCase;
  17. class TestableEventMappingBuilder extends EventMappingBuilder
  18. {
  19. public function mapEventInformations(Event $event, FreemiumEvent $freemiumEvent): void
  20. {
  21. parent::mapEventInformations($event, $freemiumEvent);
  22. }
  23. public function mapEventPlaceInformations(Event $event, FreemiumEvent $freemiumEvent): void
  24. {
  25. parent::mapEventPlaceInformations($event, $freemiumEvent);
  26. }
  27. public function mapPlaceInformations(Place $place, FreemiumEvent $freemiumEvent): void
  28. {
  29. parent::mapPlaceInformations($place, $freemiumEvent);
  30. }
  31. public function getPlace(FreemiumEvent $freemiumEvent): ?Place
  32. {
  33. return parent::getPlace($freemiumEvent);
  34. }
  35. public function getAddressPostal(Place $place): AddressPostal
  36. {
  37. return parent::getAddressPostal($place);
  38. }
  39. }
  40. class EventMappingBuilderTest extends TestCase
  41. {
  42. private EntityManagerInterface|MockObject $entityManager;
  43. public function setUp(): void
  44. {
  45. $this->entityManager = $this->createMock(EntityManagerInterface::class);
  46. }
  47. private function getEventMappingBuilderMockForMethod(string $methodName): TestableEventMappingBuilder|MockObject
  48. {
  49. return $this
  50. ->getMockBuilder(TestableEventMappingBuilder::class)
  51. ->setConstructorArgs([$this->entityManager])
  52. ->setMethodsExcept([$methodName])
  53. ->getMock();
  54. }
  55. public function testMapInformations(): void
  56. {
  57. $eventMappingBuilder = $this->getEventMappingBuilderMockForMethod('mapInformations');
  58. $event = $this->createMock(Event::class);
  59. $freemiumEvent = $this->createMock(FreemiumEvent::class);
  60. $eventMappingBuilder
  61. ->expects($this->once())
  62. ->method('mapEventInformations')
  63. ->with($event, $freemiumEvent);
  64. $eventMappingBuilder
  65. ->expects($this->once())
  66. ->method('mapEventPlaceInformations')
  67. ->with($event, $freemiumEvent);
  68. $eventMappingBuilder->mapInformations($event, $freemiumEvent);
  69. }
  70. public function testMapEventInformations(): void
  71. {
  72. $eventMappingBuilder = $this->getEventMappingBuilderMockForMethod('mapEventInformations');
  73. $event = $this->createMock(Event::class);
  74. $organization = $this->createMock(Organization::class);
  75. $image = $this->createMock(File::class);
  76. $category1 = $this->createMock(Categories::class);
  77. $category2 = $this->createMock(Categories::class);
  78. $eventGender = $this->createMock(EventGender::class);
  79. $freemiumEvent = new FreemiumEvent();
  80. $freemiumEvent->name = 'Test Event';
  81. $freemiumEvent->organization = $organization;
  82. $freemiumEvent->datetimeStart = new \DateTime('2024-01-01 20:00:00');
  83. $freemiumEvent->datetimeEnd = new \DateTime('2024-01-01 22:00:00');
  84. $freemiumEvent->description = 'Test description';
  85. $freemiumEvent->image = $image;
  86. $freemiumEvent->url = 'https://example.com';
  87. $freemiumEvent->urlTicket = 'https://tickets.example.com';
  88. $freemiumEvent->pricing = null;
  89. $freemiumEvent->priceMini = 0.0;
  90. $freemiumEvent->priceMaxi = 50.0;
  91. $freemiumEvent->gender = $eventGender;
  92. $freemiumEvent->addCategory($category1);
  93. $freemiumEvent->addCategory($category2);
  94. $event->expects($this->once())->method('setName')->with('Test Event');
  95. $event->expects($this->once())->method('setOrganization')->with($organization);
  96. $event->expects($this->once())->method('setDatetimeStart')->with(new \DateTime('2024-01-01 20:00:00'));
  97. $event->expects($this->once())->method('setDatetimeEnd')->with(new \DateTime('2024-01-01 22:00:00'));
  98. $event->expects($this->once())->method('setDescription')->with('Test description');
  99. $event->expects($this->once())->method('setImage')->with($image);
  100. $event->expects($this->once())->method('setUrl')->with('https://example.com');
  101. $event->expects($this->once())->method('setUrlTicket')->with('https://tickets.example.com');
  102. $event->expects($this->once())->method('setPricing')->with(null);
  103. $event->expects($this->once())->method('setPriceMini')->with(0.0);
  104. $event->expects($this->once())->method('setPriceMaxi')->with(50.0);
  105. $event->expects($this->once())->method('removeAllCategories');
  106. $event->expects($this->exactly(2))->method('addCategory');
  107. $eventMappingBuilder->mapEventInformations($event, $freemiumEvent);
  108. }
  109. public function testMapEventPlaceInformations(): void
  110. {
  111. $eventMappingBuilder = $this->getEventMappingBuilderMockForMethod('mapEventPlaceInformations');
  112. $event = $this->createMock(Event::class);
  113. $freemiumEvent = $this->createMock(FreemiumEvent::class);
  114. $place = $this->createMock(Place::class);
  115. $eventMappingBuilder
  116. ->expects($this->once())
  117. ->method('getPlace')
  118. ->with($freemiumEvent)
  119. ->willReturn($place);
  120. $eventMappingBuilder
  121. ->expects($this->once())
  122. ->method('mapPlaceInformations')
  123. ->with($place, $freemiumEvent);
  124. $this->entityManager
  125. ->expects($this->once())
  126. ->method('persist')
  127. ->with($place);
  128. $event->expects($this->once())->method('setPlace')->with($place);
  129. $eventMappingBuilder->mapEventPlaceInformations($event, $freemiumEvent);
  130. }
  131. public function testMapEventPlaceInformationsWithNullPlace(): void
  132. {
  133. $eventMappingBuilder = $this->getEventMappingBuilderMockForMethod('mapEventPlaceInformations');
  134. $event = $this->createMock(Event::class);
  135. $freemiumEvent = $this->createMock(FreemiumEvent::class);
  136. $eventMappingBuilder
  137. ->expects($this->once())
  138. ->method('getPlace')
  139. ->with($freemiumEvent)
  140. ->willReturn(null);
  141. $eventMappingBuilder
  142. ->expects($this->never())
  143. ->method('mapPlaceInformations');
  144. $this->entityManager
  145. ->expects($this->never())
  146. ->method('persist');
  147. $event->expects($this->once())
  148. ->method('setPlace')
  149. ->with(null);
  150. $eventMappingBuilder->mapEventPlaceInformations($event, $freemiumEvent);
  151. }
  152. public function testMapPlaceInformations(): void
  153. {
  154. $eventMappingBuilder = $this->getEventMappingBuilderMockForMethod('mapPlaceInformations');
  155. $place = $this->createMock(Place::class);
  156. $addressPostal = $this->createMock(AddressPostal::class);
  157. $organization = $this->createMock(Organization::class);
  158. $country = $this->createMock(Country::class);
  159. $freemiumEvent = new FreemiumEvent();
  160. $freemiumEvent->organization = $organization;
  161. $freemiumEvent->placeName = 'Test Venue';
  162. $freemiumEvent->streetAddress = '123 Test Street';
  163. $freemiumEvent->streetAddressSecond = 'Apt 1';
  164. $freemiumEvent->streetAddressThird = 'Floor 2';
  165. $freemiumEvent->postalCode = '12345';
  166. $freemiumEvent->addressCity = 'Test City';
  167. $freemiumEvent->addressCountry = $country;
  168. $freemiumEvent->latitude = 48.8566;
  169. $freemiumEvent->longitude = 2.3522;
  170. $eventMappingBuilder
  171. ->expects($this->once())
  172. ->method('getAddressPostal')
  173. ->with($place)
  174. ->willReturn($addressPostal);
  175. // Verify address postal setters
  176. $addressPostal->expects($this->once())->method('setStreetAddress')->with('123 Test Street')->willReturn($addressPostal);
  177. $addressPostal->expects($this->once())->method('setStreetAddressSecond')->with('Apt 1')->willReturn($addressPostal);
  178. $addressPostal->expects($this->once())->method('setStreetAddressThird')->with('Floor 2')->willReturn($addressPostal);
  179. $addressPostal->expects($this->once())->method('setPostalCode')->with('12345')->willReturn($addressPostal);
  180. $addressPostal->expects($this->once())->method('setAddressCity')->with('Test City')->willReturn($addressPostal);
  181. $addressPostal->expects($this->once())->method('setAddressCountry')->with($country)->willReturn($addressPostal);
  182. $addressPostal->expects($this->once())->method('setLatitude')->with(48.8566)->willReturn($addressPostal);
  183. $addressPostal->expects($this->once())->method('setLongitude')->with(2.3522)->willReturn($addressPostal);
  184. // Verify place setters
  185. $place->expects($this->once())->method('setOrganization')->with($organization)->willReturn($place);
  186. $place->expects($this->once())->method('setName')->with('Test Venue')->willReturn($place);
  187. $place->expects($this->once())->method('setAddressPostal')->with($addressPostal)->willReturn($place);
  188. $eventMappingBuilder->mapPlaceInformations($place, $freemiumEvent);
  189. }
  190. public function testGetPlaceWithExistingPlace(): void
  191. {
  192. $eventMappingBuilder = $this->getEventMappingBuilderMockForMethod('getPlace');
  193. $existingPlace = $this->createMock(Place::class);
  194. $freemiumEvent = new FreemiumEvent();
  195. $freemiumEvent->place = $existingPlace;
  196. $result = $eventMappingBuilder->getPlace($freemiumEvent);
  197. $this->assertSame($existingPlace, $result);
  198. }
  199. public function testGetPlaceCreatesNewPlace(): void
  200. {
  201. $eventMappingBuilder = $this->getEventMappingBuilderMockForMethod('getPlace');
  202. $freemiumEvent = new FreemiumEvent();
  203. $freemiumEvent->place = null;
  204. $freemiumEvent->placeName = 'Test Venue';
  205. $result = $eventMappingBuilder->getPlace($freemiumEvent);
  206. $this->assertInstanceOf(Place::class, $result);
  207. }
  208. public function testGetPlaceReturnsNullWhenNoInformation(): void
  209. {
  210. $eventMappingBuilder = $this->getEventMappingBuilderMockForMethod('getPlace');
  211. $freemiumEvent = new FreemiumEvent();
  212. $freemiumEvent->place = null;
  213. $freemiumEvent->placeName = null;
  214. $freemiumEvent->streetAddress = null;
  215. $freemiumEvent->streetAddressSecond = null;
  216. $freemiumEvent->streetAddressThird = null;
  217. $freemiumEvent->postalCode = null;
  218. $freemiumEvent->addressCity = null;
  219. $result = $eventMappingBuilder->getPlace($freemiumEvent);
  220. $this->assertNull($result);
  221. }
  222. public function testGetAddressPostalWithExistingAddress(): void
  223. {
  224. $eventMappingBuilder = $this->getEventMappingBuilderMockForMethod('getAddressPostal');
  225. $existingAddress = $this->createMock(AddressPostal::class);
  226. $place = $this->createMock(Place::class);
  227. $place->method('getAddressPostal')->willReturn($existingAddress);
  228. $result = $eventMappingBuilder->getAddressPostal($place);
  229. $this->assertSame($existingAddress, $result);
  230. }
  231. public function testGetAddressPostalCreatesNew(): void
  232. {
  233. $eventMappingBuilder = $this->getEventMappingBuilderMockForMethod('getAddressPostal');
  234. $place = $this->createMock(Place::class);
  235. $place->method('getAddressPostal')->willReturn(null);
  236. $result = $eventMappingBuilder->getAddressPostal($place);
  237. $this->assertInstanceOf(AddressPostal::class, $result);
  238. }
  239. }