MobytServiceTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. ['username' => $login, 'password' => $password]
  39. )
  40. ->willReturn('123;456');
  41. $mobytService->connect($login, $password);
  42. $this->assertEquals(
  43. '123',
  44. $mobytService->getUserId()
  45. );
  46. $this->assertEquals(
  47. '456',
  48. $mobytService->getSessionKey()
  49. );
  50. }
  51. /**
  52. * @see MobytService::getUserStatus()
  53. */
  54. public function testGetUserStatus(): void {
  55. $mobytService = $this->getMockBuilder(TestableMobytService::class)
  56. ->setConstructorArgs([$this->client])
  57. ->setMethodsExcept(['getUserStatus'])
  58. ->getMock();
  59. $login = 'foo';
  60. $password = 'bar';
  61. $mobytService->expects(self::once())->method('connect')->with($login, $password);
  62. $mobytService->method('getUserId')->willReturn('123');
  63. $mobytService->method('getSessionKey')->willReturn('456');
  64. $mobytService
  65. ->expects(self::once())
  66. ->method('getJsonContent')
  67. ->with(
  68. 'status',
  69. ['getMoney' => 'true', 'typeAliases' => 'true'],
  70. [
  71. 'headers' => [ 'user_key' => '123', 'Session_key' => '456' ]
  72. ]
  73. );
  74. $mobytService->getUserStatus($login, $password);
  75. }
  76. /**
  77. * @see MobytService::hasCredentialsCorrect()
  78. */
  79. public function testHasCredentialsCorrect(): void {
  80. $mobytService = $this->getMockBuilder(TestableMobytService::class)
  81. ->setConstructorArgs([$this->client])
  82. ->setMethodsExcept(['hasCredentialsCorrect'])
  83. ->getMock();
  84. $login = 'foo';
  85. $password = 'bar';
  86. $mobytService->expects(self::once())->method('connect')->with($login, $password);
  87. $this->assertTrue($mobytService->hasCredentialsCorrect($login, $password));
  88. }
  89. /**
  90. * @see MobytService::hasCredentialsCorrect()
  91. */
  92. public function testHasCredentialsNotCorrect(): void {
  93. $mobytService = $this->getMockBuilder(TestableMobytService::class)
  94. ->setConstructorArgs([$this->client])
  95. ->setMethodsExcept(['hasCredentialsCorrect'])
  96. ->getMock();
  97. $login = 'foo';
  98. $password = 'bar';
  99. $mobytService->expects(self::once())->method('connect')->with($login, $password)->willThrowException(new NotFoundHttpException());
  100. $this->assertFalse($mobytService->hasCredentialsCorrect($login, $password));
  101. }
  102. }