MobytServiceTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  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 MobytServiceTest extends TestCase
  9. {
  10. private HttpClientInterface $client;
  11. private MobytService $mobytService;
  12. public function setUp():void
  13. {
  14. $this->client = $this->getMockBuilder(HttpClientInterface::class)
  15. ->disableOriginalConstructor()
  16. ->getMock();
  17. $this->mobytService = new MobytService($this->client);
  18. }
  19. private function getContentFromFixture(string $filename): string {
  20. $filepath = dirname(__FILE__) . '/fixtures/' . $filename;
  21. return file_get_contents($filepath);
  22. }
  23. /**
  24. * @see MobytService::getUserStatus()
  25. */
  26. public function testGetUserStatus(): void {
  27. // Mock the response that will be returned by the connection request
  28. $connectResponse = $this->getMockBuilder(ResponseInterface::class)
  29. ->disableOriginalConstructor()
  30. ->getMock();
  31. $connectResponse->method('getContent')->willReturn('userId;sessionKey');
  32. // Mock the response that will be returned by the user_status request
  33. $responseContent = $this->getContentFromFixture('user_status.json');
  34. $response = $this->getMockBuilder(ResponseInterface::class)
  35. ->disableOriginalConstructor()
  36. ->getMock();
  37. $response->method('getContent')->willReturn($responseContent);
  38. $this->client
  39. ->method('request')
  40. ->withConsecutive(
  41. ["GET", "login?username=user&password=pwd"],
  42. ["GET", "status?getMoney=true&typeAliases=true", ['headers' => [ 'user_key' => 'userId', 'Session_key' => 'sessionKey' ]]]
  43. )
  44. ->willReturnOnConsecutiveCalls(
  45. $connectResponse,
  46. $response
  47. );
  48. $data = $this->mobytService->getUserStatus(1, 'user', 'pwd');
  49. $this->assertEquals(
  50. $data['money'],
  51. 33.0
  52. );
  53. }
  54. /**
  55. * @see MobytService::hasCredentialsCorrect()
  56. */
  57. public function testHasCredentialsCorrect(): void {
  58. // Mock the response that will be returned by the connection request
  59. $connectResponse = $this->getMockBuilder(ResponseInterface::class)
  60. ->disableOriginalConstructor()
  61. ->getMock();
  62. $connectResponse->method('getContent')->willReturn('userId;sessionKey');
  63. $this->client
  64. ->method('request')
  65. ->with("GET", "login?username=user&password=pwd")
  66. ->willReturnOnConsecutiveCalls(
  67. $connectResponse
  68. );
  69. $this->assertTrue($this->mobytService->hasCredentialsCorrect('user', 'pwd'));
  70. }
  71. /**
  72. * @see MobytService::hasCredentialsCorrect()
  73. */
  74. public function testHasCredentialsUnCorrect(): void {
  75. // Mock the response that will be returned by the connection request
  76. $connectResponse = $this->getMockBuilder(ResponseInterface::class)
  77. ->disableOriginalConstructor()
  78. ->getMock();
  79. $connectResponse->method('getContent')->willThrowException(new NotFoundHttpException());
  80. $this->client
  81. ->method('request')
  82. ->with("GET", "login?username=user&password=pwd")
  83. ->willReturnOnConsecutiveCalls(
  84. $connectResponse
  85. );
  86. $this->assertFalse($this->mobytService->hasCredentialsCorrect('user', 'pwd'));
  87. }
  88. }