| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- <?php
- declare(strict_types=1);
- namespace App\Tests\Unit\Service\ApiResourceBuilder\Freemium;
- use App\ApiResources\Freemium\FreemiumOrganization;
- use App\Entity\Core\AddressPostal;
- use App\Entity\Core\ContactPoint;
- use App\Entity\Core\Country;
- use App\Entity\Core\File;
- use App\Entity\Organization\Organization;
- use App\Entity\Organization\OrganizationAddressPostal;
- use App\Enum\Core\ContactPointTypeEnum;
- use App\Service\ApiResourceBuilder\Freemium\OrganizationMappingBuilder;
- use libphonenumber\PhoneNumber;
- use PHPUnit\Framework\MockObject\MockObject;
- use PHPUnit\Framework\TestCase;
- class TestableOrganizationMappingBuilder extends OrganizationMappingBuilder
- {
- public function mapOrganizationInformations(Organization $organization, FreemiumOrganization $freemiumOrganization): void
- {
- parent::mapOrganizationInformations($organization, $freemiumOrganization);
- }
- public function updateOrganizationTypeOfPractices(Organization $organization, FreemiumOrganization $freemiumOrganization): void
- {
- parent::updateOrganizationTypeOfPractices($organization, $freemiumOrganization);
- }
- public function mapContactPointInformations(ContactPoint $contactPoint, FreemiumOrganization $freemiumOrganization): void
- {
- parent::mapContactPointInformations($contactPoint, $freemiumOrganization);
- }
- public function mapAddressPostalInformations(AddressPostal $address, FreemiumOrganization $freemiumOrganization): void
- {
- parent::mapAddressPostalInformations($address, $freemiumOrganization);
- }
- public function getPrincipalContactPointOrCreateNewOne(Organization $organization): ContactPoint
- {
- return parent::getPrincipalContactPointOrCreateNewOne($organization);
- }
- public function getPrincipalAddressPostalOrCreateNewOne(Organization $organization): AddressPostal
- {
- return parent::getPrincipalAddressPostalOrCreateNewOne($organization);
- }
- }
- class OrganizationMappingBuilderTest extends TestCase
- {
- private function getOrganizationMappingBuilderMockForMethod(string $methodName): TestableOrganizationMappingBuilder|MockObject
- {
- return $this
- ->getMockBuilder(TestableOrganizationMappingBuilder::class)
- ->setMethodsExcept([$methodName])
- ->getMock();
- }
- public function testMapInformations(): void
- {
- $organizationMappingBuilder = $this
- ->getMockBuilder(TestableOrganizationMappingBuilder::class)
- ->setMethodsExcept(['mapInformations'])
- ->getMock();
- $organization = $this->createMock(Organization::class);
- $freemiumOrganization = $this->createMock(FreemiumOrganization::class);
- $contactPoint = $this->createMock(ContactPoint::class);
- $addressPostal = $this->createMock(AddressPostal::class);
- // Verify that methods are called in the correct order
- $organizationMappingBuilder
- ->expects($this->once())
- ->method('mapOrganizationInformations')
- ->with($organization, $freemiumOrganization);
- $organizationMappingBuilder
- ->expects($this->once())
- ->method('updateOrganizationTypeOfPractices')
- ->with($organization, $freemiumOrganization);
- $organizationMappingBuilder
- ->expects($this->once())
- ->method('getPrincipalContactPointOrCreateNewOne')
- ->with($organization)
- ->willReturn($contactPoint);
- $organizationMappingBuilder
- ->expects($this->once())
- ->method('mapContactPointInformations')
- ->with($contactPoint, $freemiumOrganization);
- $organizationMappingBuilder
- ->expects($this->once())
- ->method('getPrincipalAddressPostalOrCreateNewOne')
- ->with($organization)
- ->willReturn($addressPostal);
- $organizationMappingBuilder
- ->expects($this->once())
- ->method('mapAddressPostalInformations')
- ->with($addressPostal, $freemiumOrganization);
- $organizationMappingBuilder->mapInformations($organization, $freemiumOrganization);
- }
- public function testMapOrganizationInformations(): void
- {
- $organizationMappingBuilder = $this->getOrganizationMappingBuilderMockForMethod('mapOrganizationInformations');
- $organization = $this->createMock(Organization::class);
- $logo = $this->createMock(File::class);
- $freemiumOrganization = new FreemiumOrganization();
- $freemiumOrganization->name = 'Test Organization';
- $freemiumOrganization->description = 'Test Description';
- $freemiumOrganization->facebook = 'https://facebook.com/test';
- $freemiumOrganization->youtube = 'https://youtube.com/test';
- $freemiumOrganization->instagram = 'https://instagram.com/test';
- $freemiumOrganization->twitter = 'https://twitter.com/test';
- $freemiumOrganization->portailVisibility = true;
- $freemiumOrganization->logo = $logo;
- // Verify setter calls
- $organization->expects($this->once())->method('setName')->with('Test Organization');
- $organization->expects($this->once())->method('setDescription')->with('Test Description');
- $organization->expects($this->once())->method('setFacebook')->with('https://facebook.com/test');
- $organization->expects($this->once())->method('setYoutube')->with('https://youtube.com/test');
- $organization->expects($this->once())->method('setInstagram')->with('https://instagram.com/test');
- $organization->expects($this->once())->method('setTwitter')->with('https://twitter.com/test');
- $organization->expects($this->once())->method('setPortailVisibility')->with(true);
- $organization->expects($this->once())->method('setLogo')->with($logo);
- $organizationMappingBuilder->mapOrganizationInformations($organization, $freemiumOrganization);
- }
- public function testMapContactPointInformations(): void
- {
- $organizationMappingBuilder = $this->getOrganizationMappingBuilderMockForMethod('mapContactPointInformations');
- $contactPoint = $this->createMock(ContactPoint::class);
- $tel = $this->createMock(PhoneNumber::class);
- $freemiumOrganization = new FreemiumOrganization();
- $freemiumOrganization->tel = $tel;
- $freemiumOrganization->email = 'test@example.com';
- $contactPoint->expects($this->once())->method('setTelphone')->with($tel);
- $contactPoint->expects($this->once())->method('setEmail')->with('test@example.com');
- $organizationMappingBuilder->mapContactPointInformations($contactPoint, $freemiumOrganization);
- }
- public function testMapAddressPostalInformations(): void
- {
- $organizationMappingBuilder = $this->getOrganizationMappingBuilderMockForMethod('mapAddressPostalInformations');
- $address = $this->createMock(AddressPostal::class);
- $country = $this->createMock(Country::class);
- $freemiumOrganization = new FreemiumOrganization();
- $freemiumOrganization->streetAddress = '123 Test Street';
- $freemiumOrganization->streetAddressSecond = 'Apt 1';
- $freemiumOrganization->streetAddressThird = 'Floor 2';
- $freemiumOrganization->postalCode = '12345';
- $freemiumOrganization->addressCity = 'Test City';
- $freemiumOrganization->addressCountry = $country;
- $freemiumOrganization->longitude = 2.3522;
- $freemiumOrganization->latitude = 48.8566;
- $address->expects($this->once())->method('setStreetAddress')->with('123 Test Street');
- $address->expects($this->once())->method('setStreetAddressSecond')->with('Apt 1');
- $address->expects($this->once())->method('setStreetAddressThird')->with('Floor 2');
- $address->expects($this->once())->method('setPostalCode')->with('12345');
- $address->expects($this->once())->method('setAddressCity')->with('Test City');
- $address->expects($this->once())->method('setAddressCountry')->with($country);
- $address->expects($this->once())->method('setLongitude')->with(2.3522);
- $address->expects($this->once())->method('setLatitude')->with(48.8566);
- $organizationMappingBuilder->mapAddressPostalInformations($address, $freemiumOrganization);
- }
- public function testGetPrincipalContactPointOrCreateNewOneWithExisting(): void
- {
- $organizationMappingBuilder = $this->getOrganizationMappingBuilderMockForMethod('getPrincipalContactPointOrCreateNewOne');
- $organization = $this->createMock(Organization::class);
- $existingContactPoint = $this->createMock(ContactPoint::class);
- $organization->expects($this->once())
- ->method('getPrincipalContactPoint')
- ->willReturn($existingContactPoint);
- $result = $organizationMappingBuilder->getPrincipalContactPointOrCreateNewOne($organization);
- $this->assertSame($existingContactPoint, $result);
- }
- public function testGetPrincipalContactPointOrCreateNewOneCreatesNew(): void
- {
- $organizationMappingBuilder = $this->getOrganizationMappingBuilderMockForMethod('getPrincipalContactPointOrCreateNewOne');
- $organization = $this->createMock(Organization::class);
- $organization->expects($this->once())
- ->method('getPrincipalContactPoint')
- ->willReturn(null);
- $organization->expects($this->once())
- ->method('addContactPoint')
- ->with($this->isInstanceOf(ContactPoint::class));
- $result = $organizationMappingBuilder->getPrincipalContactPointOrCreateNewOne($organization);
- $this->assertSame(ContactPointTypeEnum::PRINCIPAL, $result->getContactType());
- }
- public function testGetPrincipalAddressPostalOrCreateNewOneWithExisting(): void
- {
- $organizationMappingBuilder = $this->getOrganizationMappingBuilderMockForMethod('getPrincipalAddressPostalOrCreateNewOne');
- $organization = $this->createMock(Organization::class);
- $existingAddressPostal = $this->createMock(AddressPostal::class);
- $existingOrganizationAddressPostal = $this->createMock(OrganizationAddressPostal::class);
- $existingOrganizationAddressPostal->expects($this->once())
- ->method('getAddressPostal')
- ->willReturn($existingAddressPostal);
- $organization->expects($this->once())
- ->method('getPrincipalAddressPostal')
- ->willReturn($existingOrganizationAddressPostal);
- $result = $organizationMappingBuilder->getPrincipalAddressPostalOrCreateNewOne($organization);
- $this->assertSame($existingAddressPostal, $result);
- }
- public function testGetPrincipalAddressPostalOrCreateNewOneCreatesNew(): void
- {
- $organizationMappingBuilder = $this->getOrganizationMappingBuilderMockForMethod('getPrincipalAddressPostalOrCreateNewOne');
- $organization = $this->createMock(Organization::class);
- $organization->expects($this->once())
- ->method('getPrincipalAddressPostal')
- ->willReturn(null);
- $organization->expects($this->once())
- ->method('addOrganizationAddressPostal')
- ->with($this->isInstanceOf(OrganizationAddressPostal::class));
- $result = $organizationMappingBuilder->getPrincipalAddressPostalOrCreateNewOne($organization);
- }
- }
|