OrganizationMappingBuilderTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Tests\Unit\Service\ApiResourceBuilder\Freemium;
  4. use App\ApiResources\Freemium\FreemiumOrganization;
  5. use App\Entity\Core\AddressPostal;
  6. use App\Entity\Core\ContactPoint;
  7. use App\Entity\Core\Country;
  8. use App\Entity\Core\File;
  9. use App\Entity\Organization\Organization;
  10. use App\Entity\Organization\OrganizationAddressPostal;
  11. use App\Enum\Core\ContactPointTypeEnum;
  12. use App\Service\ApiResourceBuilder\Freemium\OrganizationMappingBuilder;
  13. use libphonenumber\PhoneNumber;
  14. use PHPUnit\Framework\MockObject\MockObject;
  15. use PHPUnit\Framework\TestCase;
  16. class TestableOrganizationMappingBuilder extends OrganizationMappingBuilder
  17. {
  18. public function mapOrganizationInformations(Organization $organization, FreemiumOrganization $freemiumOrganization): void
  19. {
  20. parent::mapOrganizationInformations($organization, $freemiumOrganization);
  21. }
  22. public function mapContactPointInformations(ContactPoint $contactPoint, FreemiumOrganization $freemiumOrganization): void
  23. {
  24. parent::mapContactPointInformations($contactPoint, $freemiumOrganization);
  25. }
  26. public function mapAddressPostalInformations(AddressPostal $address, FreemiumOrganization $freemiumOrganization): void
  27. {
  28. parent::mapAddressPostalInformations($address, $freemiumOrganization);
  29. }
  30. public function getPrincipalContactPointOrCreateNewOne(Organization $organization): ContactPoint
  31. {
  32. return parent::getPrincipalContactPointOrCreateNewOne($organization);
  33. }
  34. public function getPrincipalAddressPostalOrCreateNewOne(Organization $organization): AddressPostal
  35. {
  36. return parent::getPrincipalAddressPostalOrCreateNewOne($organization);
  37. }
  38. }
  39. class OrganizationMappingBuilderTest extends TestCase
  40. {
  41. private function getOrganizationMappingBuilderMockForMethod(string $methodName): TestableOrganizationMappingBuilder|MockObject
  42. {
  43. return $this
  44. ->getMockBuilder(TestableOrganizationMappingBuilder::class)
  45. ->setMethodsExcept([$methodName])
  46. ->getMock();
  47. }
  48. public function testMapInformations(): void
  49. {
  50. $organizationMappingBuilder = $this
  51. ->getMockBuilder(TestableOrganizationMappingBuilder::class)
  52. ->setMethodsExcept(['mapInformations'])
  53. ->getMock();
  54. $organization = $this->createMock(Organization::class);
  55. $freemiumOrganization = $this->createMock(FreemiumOrganization::class);
  56. $contactPoint = $this->createMock(ContactPoint::class);
  57. $addressPostal = $this->createMock(AddressPostal::class);
  58. // Verify that methods are called in the correct order
  59. $organizationMappingBuilder
  60. ->expects($this->once())
  61. ->method('mapOrganizationInformations')
  62. ->with($organization, $freemiumOrganization);
  63. $organizationMappingBuilder
  64. ->expects($this->once())
  65. ->method('getPrincipalContactPointOrCreateNewOne')
  66. ->with($organization)
  67. ->willReturn($contactPoint);
  68. $organizationMappingBuilder
  69. ->expects($this->once())
  70. ->method('mapContactPointInformations')
  71. ->with($contactPoint, $freemiumOrganization);
  72. $organizationMappingBuilder
  73. ->expects($this->once())
  74. ->method('getPrincipalAddressPostalOrCreateNewOne')
  75. ->with($organization)
  76. ->willReturn($addressPostal);
  77. $organizationMappingBuilder
  78. ->expects($this->once())
  79. ->method('mapAddressPostalInformations')
  80. ->with($addressPostal, $freemiumOrganization);
  81. $organizationMappingBuilder->mapInformations($organization, $freemiumOrganization);
  82. }
  83. public function testMapOrganizationInformations(): void
  84. {
  85. $organizationMappingBuilder = $this->getOrganizationMappingBuilderMockForMethod('mapOrganizationInformations');
  86. $organization = $this->createMock(Organization::class);
  87. $logo = $this->createMock(File::class);
  88. $freemiumOrganization = new FreemiumOrganization();
  89. $freemiumOrganization->name = 'Test Organization';
  90. $freemiumOrganization->description = 'Test Description';
  91. $freemiumOrganization->facebook = 'https://facebook.com/test';
  92. $freemiumOrganization->youtube = 'https://youtube.com/test';
  93. $freemiumOrganization->instagram = 'https://instagram.com/test';
  94. $freemiumOrganization->twitter = 'https://twitter.com/test';
  95. $freemiumOrganization->portailVisibility = true;
  96. $freemiumOrganization->logo = $logo;
  97. // Verify setter calls
  98. $organization->expects($this->once())->method('setName')->with('Test Organization');
  99. $organization->expects($this->once())->method('setDescription')->with('Test Description');
  100. $organization->expects($this->once())->method('setFacebook')->with('https://facebook.com/test');
  101. $organization->expects($this->once())->method('setYoutube')->with('https://youtube.com/test');
  102. $organization->expects($this->once())->method('setInstagram')->with('https://instagram.com/test');
  103. $organization->expects($this->once())->method('setTwitter')->with('https://twitter.com/test');
  104. $organization->expects($this->once())->method('setPortailVisibility')->with(true);
  105. $organization->expects($this->once())->method('setLogo')->with($logo);
  106. $organizationMappingBuilder->mapOrganizationInformations($organization, $freemiumOrganization);
  107. }
  108. public function testMapContactPointInformations(): void
  109. {
  110. $organizationMappingBuilder = $this->getOrganizationMappingBuilderMockForMethod('mapContactPointInformations');
  111. $contactPoint = $this->createMock(ContactPoint::class);
  112. $tel = $this->createMock(PhoneNumber::class);
  113. $freemiumOrganization = new FreemiumOrganization();
  114. $freemiumOrganization->tel = $tel;
  115. $freemiumOrganization->email = 'test@example.com';
  116. $contactPoint->expects($this->once())->method('setTelphone')->with($tel);
  117. $contactPoint->expects($this->once())->method('setEmail')->with('test@example.com');
  118. $organizationMappingBuilder->mapContactPointInformations($contactPoint, $freemiumOrganization);
  119. }
  120. public function testMapAddressPostalInformations(): void
  121. {
  122. $organizationMappingBuilder = $this->getOrganizationMappingBuilderMockForMethod('mapAddressPostalInformations');
  123. $address = $this->createMock(AddressPostal::class);
  124. $country = $this->createMock(Country::class);
  125. $freemiumOrganization = new FreemiumOrganization();
  126. $freemiumOrganization->streetAddress = '123 Test Street';
  127. $freemiumOrganization->streetAddressSecond = 'Apt 1';
  128. $freemiumOrganization->streetAddressThird = 'Floor 2';
  129. $freemiumOrganization->postalCode = '12345';
  130. $freemiumOrganization->addressCity = 'Test City';
  131. $freemiumOrganization->addressCountry = $country;
  132. $freemiumOrganization->longitude = 2.3522;
  133. $freemiumOrganization->latitude = 48.8566;
  134. $address->expects($this->once())->method('setStreetAddress')->with('123 Test Street');
  135. $address->expects($this->once())->method('setStreetAddressSecond')->with('Apt 1');
  136. $address->expects($this->once())->method('setStreetAddressThird')->with('Floor 2');
  137. $address->expects($this->once())->method('setPostalCode')->with('12345');
  138. $address->expects($this->once())->method('setAddressCity')->with('Test City');
  139. $address->expects($this->once())->method('setAddressCountry')->with($country);
  140. $address->expects($this->once())->method('setLongitude')->with(2.3522);
  141. $address->expects($this->once())->method('setLatitude')->with(48.8566);
  142. $organizationMappingBuilder->mapAddressPostalInformations($address, $freemiumOrganization);
  143. }
  144. public function testGetPrincipalContactPointOrCreateNewOneWithExisting(): void
  145. {
  146. $organizationMappingBuilder = $this->getOrganizationMappingBuilderMockForMethod('getPrincipalContactPointOrCreateNewOne');
  147. $organization = $this->createMock(Organization::class);
  148. $existingContactPoint = $this->createMock(ContactPoint::class);
  149. $organization->expects($this->once())
  150. ->method('getPrincipalContactPoint')
  151. ->willReturn($existingContactPoint);
  152. $result = $organizationMappingBuilder->getPrincipalContactPointOrCreateNewOne($organization);
  153. $this->assertSame($existingContactPoint, $result);
  154. }
  155. public function testGetPrincipalContactPointOrCreateNewOneCreatesNew(): void
  156. {
  157. $organizationMappingBuilder = $this->getOrganizationMappingBuilderMockForMethod('getPrincipalContactPointOrCreateNewOne');
  158. $organization = $this->createMock(Organization::class);
  159. $organization->expects($this->once())
  160. ->method('getPrincipalContactPoint')
  161. ->willReturn(null);
  162. $organization->expects($this->once())
  163. ->method('addContactPoint')
  164. ->with($this->isInstanceOf(ContactPoint::class));
  165. $result = $organizationMappingBuilder->getPrincipalContactPointOrCreateNewOne($organization);
  166. $this->assertSame(ContactPointTypeEnum::PRINCIPAL, $result->getContactType());
  167. }
  168. public function testGetPrincipalAddressPostalOrCreateNewOneWithExisting(): void
  169. {
  170. $organizationMappingBuilder = $this->getOrganizationMappingBuilderMockForMethod('getPrincipalAddressPostalOrCreateNewOne');
  171. $organization = $this->createMock(Organization::class);
  172. $existingAddressPostal = $this->createMock(AddressPostal::class);
  173. $existingOrganizationAddressPostal = $this->createMock(OrganizationAddressPostal::class);
  174. $existingOrganizationAddressPostal->expects($this->once())
  175. ->method('getAddressPostal')
  176. ->willReturn($existingAddressPostal);
  177. $organization->expects($this->once())
  178. ->method('getPrincipalAddressPostal')
  179. ->willReturn($existingOrganizationAddressPostal);
  180. $result = $organizationMappingBuilder->getPrincipalAddressPostalOrCreateNewOne($organization);
  181. $this->assertSame($existingAddressPostal, $result);
  182. }
  183. public function testGetPrincipalAddressPostalOrCreateNewOneCreatesNew(): void
  184. {
  185. $organizationMappingBuilder = $this->getOrganizationMappingBuilderMockForMethod('getPrincipalAddressPostalOrCreateNewOne');
  186. $organization = $this->createMock(Organization::class);
  187. $organization->expects($this->once())
  188. ->method('getPrincipalAddressPostal')
  189. ->willReturn(null);
  190. $organization->expects($this->once())
  191. ->method('addOrganizationAddressPostal')
  192. ->with($this->isInstanceOf(OrganizationAddressPostal::class));
  193. $result = $organizationMappingBuilder->getPrincipalAddressPostalOrCreateNewOne($organization);
  194. }
  195. }