MobytServiceTest.php 4.1 KB

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