| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301 |
- <?php
- declare(strict_types=1);
- namespace App\Tests\Unit\Service\ApiResourceBuilder\Freemium;
- use App\ApiResources\Freemium\FreemiumEvent;
- use App\Entity\Booking\Event;
- use App\Entity\Booking\EventGender;
- use App\Entity\Core\AddressPostal;
- use App\Entity\Core\Categories;
- use App\Entity\Core\Country;
- use App\Entity\Core\File;
- use App\Entity\Organization\Organization;
- use App\Entity\Place\Place;
- use App\Service\ApiResourceBuilder\Freemium\EventMappingBuilder;
- use Doctrine\ORM\EntityManagerInterface;
- use PHPUnit\Framework\MockObject\MockObject;
- use PHPUnit\Framework\TestCase;
- class TestableEventMappingBuilder extends EventMappingBuilder
- {
- public function mapEventInformations(Event $event, FreemiumEvent $freemiumEvent): void
- {
- parent::mapEventInformations($event, $freemiumEvent);
- }
- public function mapEventPlaceInformations(Event $event, FreemiumEvent $freemiumEvent): void
- {
- parent::mapEventPlaceInformations($event, $freemiumEvent);
- }
- public function mapPlaceInformations(Place $place, FreemiumEvent $freemiumEvent): void
- {
- parent::mapPlaceInformations($place, $freemiumEvent);
- }
- public function getPlace(FreemiumEvent $freemiumEvent): ?Place
- {
- return parent::getPlace($freemiumEvent);
- }
- public function getAddressPostal(Place $place): AddressPostal
- {
- return parent::getAddressPostal($place);
- }
- }
- class EventMappingBuilderTest extends TestCase
- {
- private EntityManagerInterface|MockObject $entityManager;
- public function setUp(): void
- {
- $this->entityManager = $this->createMock(EntityManagerInterface::class);
- }
- private function getEventMappingBuilderMockForMethod(string $methodName): TestableEventMappingBuilder|MockObject
- {
- return $this
- ->getMockBuilder(TestableEventMappingBuilder::class)
- ->setConstructorArgs([$this->entityManager])
- ->setMethodsExcept([$methodName])
- ->getMock();
- }
- public function testMapInformations(): void
- {
- $eventMappingBuilder = $this->getEventMappingBuilderMockForMethod('mapInformations');
- $event = $this->createMock(Event::class);
- $freemiumEvent = $this->createMock(FreemiumEvent::class);
- $eventMappingBuilder
- ->expects($this->once())
- ->method('mapEventInformations')
- ->with($event, $freemiumEvent);
- $eventMappingBuilder
- ->expects($this->once())
- ->method('mapEventPlaceInformations')
- ->with($event, $freemiumEvent);
- $eventMappingBuilder->mapInformations($event, $freemiumEvent);
- }
- public function testMapEventInformations(): void
- {
- $eventMappingBuilder = $this->getEventMappingBuilderMockForMethod('mapEventInformations');
- $event = $this->createMock(Event::class);
- $organization = $this->createMock(Organization::class);
- $image = $this->createMock(File::class);
- $category1 = $this->createMock(Categories::class);
- $category2 = $this->createMock(Categories::class);
- $eventGender = $this->createMock(EventGender::class);
- $freemiumEvent = new FreemiumEvent();
- $freemiumEvent->name = 'Test Event';
- $freemiumEvent->organization = $organization;
- $freemiumEvent->datetimeStart = new \DateTime('2024-01-01 20:00:00');
- $freemiumEvent->datetimeEnd = new \DateTime('2024-01-01 22:00:00');
- $freemiumEvent->description = 'Test description';
- $freemiumEvent->image = $image;
- $freemiumEvent->url = 'https://example.com';
- $freemiumEvent->urlTicket = 'https://tickets.example.com';
- $freemiumEvent->pricing = null;
- $freemiumEvent->priceMini = 0.0;
- $freemiumEvent->priceMaxi = 50.0;
- $freemiumEvent->gender = $eventGender;
- $freemiumEvent->addCategory($category1);
- $freemiumEvent->addCategory($category2);
- $event->expects($this->once())->method('setName')->with('Test Event');
- $event->expects($this->once())->method('setOrganization')->with($organization);
- $event->expects($this->once())->method('setDatetimeStart')->with(new \DateTime('2024-01-01 20:00:00'));
- $event->expects($this->once())->method('setDatetimeEnd')->with(new \DateTime('2024-01-01 22:00:00'));
- $event->expects($this->once())->method('setDescription')->with('Test description');
- $event->expects($this->once())->method('setImage')->with($image);
- $event->expects($this->once())->method('setUrl')->with('https://example.com');
- $event->expects($this->once())->method('setUrlTicket')->with('https://tickets.example.com');
- $event->expects($this->once())->method('setPricing')->with(null);
- $event->expects($this->once())->method('setPriceMini')->with(0.0);
- $event->expects($this->once())->method('setPriceMaxi')->with(50.0);
- $event->expects($this->once())->method('removeAllCategories');
- $event->expects($this->exactly(2))->method('addCategory');
- $eventMappingBuilder->mapEventInformations($event, $freemiumEvent);
- }
- public function testMapEventPlaceInformations(): void
- {
- $eventMappingBuilder = $this->getEventMappingBuilderMockForMethod('mapEventPlaceInformations');
- $event = $this->createMock(Event::class);
- $freemiumEvent = $this->createMock(FreemiumEvent::class);
- $place = $this->createMock(Place::class);
- $eventMappingBuilder
- ->expects($this->once())
- ->method('getPlace')
- ->with($freemiumEvent)
- ->willReturn($place);
- $eventMappingBuilder
- ->expects($this->once())
- ->method('mapPlaceInformations')
- ->with($place, $freemiumEvent);
- $this->entityManager
- ->expects($this->once())
- ->method('persist')
- ->with($place);
- $event->expects($this->once())->method('setPlace')->with($place);
- $eventMappingBuilder->mapEventPlaceInformations($event, $freemiumEvent);
- }
- public function testMapEventPlaceInformationsWithNullPlace(): void
- {
- $eventMappingBuilder = $this->getEventMappingBuilderMockForMethod('mapEventPlaceInformations');
- $event = $this->createMock(Event::class);
- $freemiumEvent = $this->createMock(FreemiumEvent::class);
- $eventMappingBuilder
- ->expects($this->once())
- ->method('getPlace')
- ->with($freemiumEvent)
- ->willReturn(null);
- $eventMappingBuilder
- ->expects($this->never())
- ->method('mapPlaceInformations');
- $this->entityManager
- ->expects($this->never())
- ->method('persist');
- $event->expects($this->once())
- ->method('setPlace')
- ->with(null);
- $eventMappingBuilder->mapEventPlaceInformations($event, $freemiumEvent);
- }
- public function testMapPlaceInformations(): void
- {
- $eventMappingBuilder = $this->getEventMappingBuilderMockForMethod('mapPlaceInformations');
- $place = $this->createMock(Place::class);
- $addressPostal = $this->createMock(AddressPostal::class);
- $organization = $this->createMock(Organization::class);
- $country = $this->createMock(Country::class);
- $freemiumEvent = new FreemiumEvent();
- $freemiumEvent->organization = $organization;
- $freemiumEvent->placeName = 'Test Venue';
- $freemiumEvent->streetAddress = '123 Test Street';
- $freemiumEvent->streetAddressSecond = 'Apt 1';
- $freemiumEvent->streetAddressThird = 'Floor 2';
- $freemiumEvent->postalCode = '12345';
- $freemiumEvent->addressCity = 'Test City';
- $freemiumEvent->addressCountry = $country;
- $freemiumEvent->latitude = 48.8566;
- $freemiumEvent->longitude = 2.3522;
- $eventMappingBuilder
- ->expects($this->once())
- ->method('getAddressPostal')
- ->with($place)
- ->willReturn($addressPostal);
- // Verify address postal setters
- $addressPostal->expects($this->once())->method('setStreetAddress')->with('123 Test Street')->willReturn($addressPostal);
- $addressPostal->expects($this->once())->method('setStreetAddressSecond')->with('Apt 1')->willReturn($addressPostal);
- $addressPostal->expects($this->once())->method('setStreetAddressThird')->with('Floor 2')->willReturn($addressPostal);
- $addressPostal->expects($this->once())->method('setPostalCode')->with('12345')->willReturn($addressPostal);
- $addressPostal->expects($this->once())->method('setAddressCity')->with('Test City')->willReturn($addressPostal);
- $addressPostal->expects($this->once())->method('setAddressCountry')->with($country)->willReturn($addressPostal);
- $addressPostal->expects($this->once())->method('setLatitude')->with(48.8566)->willReturn($addressPostal);
- $addressPostal->expects($this->once())->method('setLongitude')->with(2.3522)->willReturn($addressPostal);
- // Verify place setters
- $place->expects($this->once())->method('setOrganization')->with($organization)->willReturn($place);
- $place->expects($this->once())->method('setName')->with('Test Venue')->willReturn($place);
- $place->expects($this->once())->method('setAddressPostal')->with($addressPostal)->willReturn($place);
- $eventMappingBuilder->mapPlaceInformations($place, $freemiumEvent);
- }
- public function testGetPlaceWithExistingPlace(): void
- {
- $eventMappingBuilder = $this->getEventMappingBuilderMockForMethod('getPlace');
- $existingPlace = $this->createMock(Place::class);
- $freemiumEvent = new FreemiumEvent();
- $freemiumEvent->place = $existingPlace;
- $result = $eventMappingBuilder->getPlace($freemiumEvent);
- $this->assertSame($existingPlace, $result);
- }
- public function testGetPlaceCreatesNewPlace(): void
- {
- $eventMappingBuilder = $this->getEventMappingBuilderMockForMethod('getPlace');
- $freemiumEvent = new FreemiumEvent();
- $freemiumEvent->place = null;
- $freemiumEvent->placeName = 'Test Venue';
- $result = $eventMappingBuilder->getPlace($freemiumEvent);
- $this->assertInstanceOf(Place::class, $result);
- }
- public function testGetPlaceReturnsNullWhenNoInformation(): void
- {
- $eventMappingBuilder = $this->getEventMappingBuilderMockForMethod('getPlace');
- $freemiumEvent = new FreemiumEvent();
- $freemiumEvent->place = null;
- $freemiumEvent->placeName = null;
- $freemiumEvent->streetAddress = null;
- $freemiumEvent->streetAddressSecond = null;
- $freemiumEvent->streetAddressThird = null;
- $freemiumEvent->postalCode = null;
- $freemiumEvent->addressCity = null;
- $result = $eventMappingBuilder->getPlace($freemiumEvent);
- $this->assertNull($result);
- }
- public function testGetAddressPostalWithExistingAddress(): void
- {
- $eventMappingBuilder = $this->getEventMappingBuilderMockForMethod('getAddressPostal');
- $existingAddress = $this->createMock(AddressPostal::class);
- $place = $this->createMock(Place::class);
- $place->method('getAddressPostal')->willReturn($existingAddress);
- $result = $eventMappingBuilder->getAddressPostal($place);
- $this->assertSame($existingAddress, $result);
- }
- public function testGetAddressPostalCreatesNew(): void
- {
- $eventMappingBuilder = $this->getEventMappingBuilderMockForMethod('getAddressPostal');
- $place = $this->createMock(Place::class);
- $place->method('getAddressPostal')->willReturn(null);
- $result = $eventMappingBuilder->getAddressPostal($place);
- $this->assertInstanceOf(AddressPostal::class, $result);
- }
- }
|