OrganizationMappingBuilderTest.php 11 KB

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