OrganizationMappingBuilderTest.php 11 KB

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