MobytUserStatusCreatorTest.php 5.0 KB

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