MobytUserStatusCreatorTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace App\Tests\Unit\Service\Mobyt;
  3. use App\Entity\Organization\Organization;
  4. use App\Entity\Organization\Parameters;
  5. use App\Repository\Organization\OrganizationRepository;
  6. use App\Service\ApiResourceBuilder\Mobyt\MobytUserStatusBuilder;
  7. use App\Service\Mobyt\MobytService;
  8. use PHPUnit\Framework\TestCase;
  9. class MobytUserStatusCreatorTest extends TestCase
  10. {
  11. private MobytService $mobytService;
  12. private OrganizationRepository $organizationRepository;
  13. public function setUp(): void
  14. {
  15. $this->mobytService = $this->getMockBuilder(MobytService::class)
  16. ->disableOriginalConstructor()
  17. ->getMock();
  18. $this->organizationRepository = $this->getMockBuilder(OrganizationRepository::class)
  19. ->disableOriginalConstructor()
  20. ->getMock();
  21. }
  22. /**
  23. * @see MobytUserStatusBuilder::getUserStatus()
  24. */
  25. public function testGetUserStatus(): void
  26. {
  27. $mobytUserStatusCreator = $this->getMockBuilder(MobytUserStatusBuilder::class)
  28. ->setConstructorArgs([$this->mobytService, $this->organizationRepository])
  29. ->setMethodsExcept(['getUserStatus'])
  30. ->getMock();
  31. $organizationId = 123;
  32. $mobytLogin = 'foo';
  33. $mobytPassword = 'bar';
  34. $organization = $this->getMockBuilder(Organization::class)->getMock();
  35. $this->organizationRepository
  36. ->expects($this->once())
  37. ->method('find')
  38. ->with($organizationId)
  39. ->willReturn($organization);
  40. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  41. $organization->expects($this->once())->method('getParameters')->willReturn($parameters);
  42. $parameters->expects($this->once())->method('getUsernameSMS')->willReturn($mobytLogin);
  43. $parameters->expects($this->once())->method('getPasswordSMS')->willReturn($mobytPassword);
  44. $this->mobytService
  45. ->expects($this->once())
  46. ->method('getUserStatus')
  47. ->with($mobytLogin, $mobytPassword)
  48. ->willReturn(
  49. [
  50. 'money' => 33.0,
  51. 'sms' => [
  52. ['type' => 'N', 'quantity' => '300'],
  53. ['type' => 'EE', 'quantity' => '301'], // this type of sms should be ignored by the method
  54. ],
  55. 'email' => null,
  56. ]
  57. );
  58. $mobytUserStatus = $mobytUserStatusCreator->getUserStatus($organizationId);
  59. $this->assertEquals(
  60. 33.0,
  61. $mobytUserStatus->getMoney()
  62. );
  63. // check that top-level amount is taken in account, and only it
  64. $this->assertEquals(
  65. 300,
  66. $mobytUserStatus->getAmount()
  67. );
  68. }
  69. /**
  70. * @see MobytUserStatusBuilder::getUserStatus()
  71. */
  72. public function testGetUserStatusInvalidOrganization(): void
  73. {
  74. $mobytUserStatusCreator = $this->getMockBuilder(MobytUserStatusBuilder::class)
  75. ->setConstructorArgs([$this->mobytService, $this->organizationRepository])
  76. ->setMethodsExcept(['getUserStatus'])
  77. ->getMock();
  78. $organizationId = 123;
  79. $this->organizationRepository
  80. ->expects($this->once())
  81. ->method('find')
  82. ->with($organizationId)
  83. ->willReturn(null);
  84. $this->expectException(\RuntimeException::class);
  85. $this->expectExceptionMessage('Organization not found for user');
  86. $mobytUserStatusCreator->getUserStatus($organizationId);
  87. }
  88. /**
  89. * @see MobytUserStatusBuilder::getUserStatus()
  90. */
  91. public function testGetUserStatusMissingAuthInformation(): void
  92. {
  93. $mobytUserStatusCreator = $this->getMockBuilder(MobytUserStatusBuilder::class)
  94. ->setConstructorArgs([$this->mobytService, $this->organizationRepository])
  95. ->setMethodsExcept(['getUserStatus'])
  96. ->getMock();
  97. $organizationId = 123;
  98. $organization = $this->getMockBuilder(Organization::class)->getMock();
  99. $this->organizationRepository
  100. ->expects($this->once())
  101. ->method('find')
  102. ->with($organizationId)
  103. ->willReturn($organization);
  104. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  105. $organization->expects($this->once())->method('getParameters')->willReturn($parameters);
  106. $parameters->expects($this->once())->method('getUsernameSMS')->willReturn(null);
  107. $this->mobytService
  108. ->expects($this->never())
  109. ->method('getUserStatus');
  110. $mobytUserStatus = $mobytUserStatusCreator->getUserStatus($organizationId);
  111. $this->assertEquals(
  112. 123,
  113. $mobytUserStatus->getOrganizationId()
  114. );
  115. $this->assertEquals(
  116. 0,
  117. $mobytUserStatus->getMoney()
  118. );
  119. // check that top-level amount is taken in account, and only it
  120. $this->assertEquals(
  121. 0,
  122. $mobytUserStatus->getAmount()
  123. );
  124. }
  125. }