MobytServiceTest.php 3.9 KB

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