MobytServiceTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /** @noinspection PhpUnhandledExceptionInspection */
  3. namespace App\Tests\Unit\Service\Mobyt;
  4. use App\Service\Mobyt\MobytService;
  5. use PHPUnit\Framework\TestCase;
  6. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  7. use Symfony\Contracts\HttpClient\HttpClientInterface;
  8. class TestableMobytService extends MobytService
  9. {
  10. public function connect(string $login, string $password): void
  11. {
  12. parent::connect($login, $password);
  13. }
  14. public function getUserId(): string
  15. {
  16. return parent::getUserId();
  17. }
  18. public function getSessionKey(): string
  19. {
  20. return parent::getSessionKey();
  21. }
  22. }
  23. class MobytServiceTest extends TestCase
  24. {
  25. private HttpClientInterface $client;
  26. public function setUp(): void
  27. {
  28. $this->client = $this->getMockBuilder(HttpClientInterface::class)
  29. ->disableOriginalConstructor()
  30. ->getMock();
  31. }
  32. /**
  33. * @see MobytService::connect()
  34. * @see MobytService::getUserId()
  35. * @see MobytService::getSessionKey()
  36. */
  37. public function testConnect(): void
  38. {
  39. $mobytService = $this->getMockBuilder(TestableMobytService::class)
  40. ->setConstructorArgs([$this->client])
  41. ->setMethodsExcept(['connect', 'getUserId', 'getSessionKey'])
  42. ->getMock();
  43. $login = 'foo';
  44. $password = 'bar';
  45. $mobytService
  46. ->expects(self::once())
  47. ->method('getContent')
  48. ->with(
  49. 'login',
  50. [],
  51. [
  52. 'headers' => [
  53. 'Content-Type: application/json',
  54. 'Authorization: Basic '.base64_encode($login.':'.$password),
  55. ],
  56. ]
  57. )
  58. ->willReturn('123;456');
  59. $mobytService->connect($login, $password);
  60. $this->assertEquals(
  61. '123',
  62. $mobytService->getUserId()
  63. );
  64. $this->assertEquals(
  65. '456',
  66. $mobytService->getSessionKey()
  67. );
  68. }
  69. /**
  70. * @see MobytService::getUserStatus()
  71. */
  72. public function testGetUserStatus(): void
  73. {
  74. $mobytService = $this->getMockBuilder(TestableMobytService::class)
  75. ->setConstructorArgs([$this->client])
  76. ->setMethodsExcept(['getUserStatus'])
  77. ->getMock();
  78. $login = 'foo';
  79. $password = 'bar';
  80. $mobytService->expects(self::once())->method('connect')->with($login, $password);
  81. $mobytService->method('getUserId')->willReturn('123');
  82. $mobytService->method('getSessionKey')->willReturn('456');
  83. $mobytService
  84. ->expects(self::once())
  85. ->method('getJsonContent')
  86. ->with(
  87. 'status',
  88. ['getMoney' => 'true', 'typeAliases' => 'true'],
  89. [
  90. 'headers' => ['user_key' => '123', 'Session_key' => '456'],
  91. ]
  92. );
  93. $mobytService->getUserStatus($login, $password);
  94. }
  95. /**
  96. * @see MobytService::hasCredentialsCorrect()
  97. */
  98. public function testHasCredentialsCorrect(): void
  99. {
  100. $mobytService = $this->getMockBuilder(TestableMobytService::class)
  101. ->setConstructorArgs([$this->client])
  102. ->setMethodsExcept(['hasCredentialsCorrect'])
  103. ->getMock();
  104. $login = 'foo';
  105. $password = 'bar';
  106. $mobytService->expects(self::once())->method('connect')->with($login, $password);
  107. $this->assertTrue($mobytService->hasCredentialsCorrect($login, $password));
  108. }
  109. /**
  110. * @see MobytService::hasCredentialsCorrect()
  111. */
  112. public function testHasCredentialsNotCorrect(): void
  113. {
  114. $mobytService = $this->getMockBuilder(TestableMobytService::class)
  115. ->setConstructorArgs([$this->client])
  116. ->setMethodsExcept(['hasCredentialsCorrect'])
  117. ->getMock();
  118. $login = 'foo';
  119. $password = 'bar';
  120. $mobytService->expects(self::once())->method('connect')->with($login, $password)->willThrowException(new NotFoundHttpException());
  121. $this->assertFalse($mobytService->hasCredentialsCorrect($login, $password));
  122. }
  123. }