EventMappingBuilderTest.php 12 KB

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