MobytServiceTest.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace App\Tests\Service\Mobyt;
  3. use App\Service\Mobyt\MobytService;
  4. use PHPUnit\Framework\TestCase;
  5. use Symfony\Contracts\HttpClient\HttpClientInterface;
  6. use Symfony\Contracts\HttpClient\ResponseInterface;
  7. class MobytServiceTest extends TestCase
  8. {
  9. private HttpClientInterface $client;
  10. private MobytService $mobytService;
  11. public function setUp():void
  12. {
  13. $this->client = $this->getMockBuilder(HttpClientInterface::class)
  14. ->disableOriginalConstructor()
  15. ->getMock();
  16. $this->mobytService = new MobytService($this->client);
  17. }
  18. private function getContentFromFixture(string $filename): string {
  19. $filepath = dirname(__FILE__) . '/fixtures/' . $filename;
  20. return file_get_contents($filepath);
  21. }
  22. /**
  23. * @see MobytService::getUserStatus()
  24. */
  25. public function testGetUserStatus(): void {
  26. // Mock the response that will be returned by the connection request
  27. $connectResponse = $this->getMockBuilder(ResponseInterface::class)
  28. ->disableOriginalConstructor()
  29. ->getMock();
  30. $connectResponse->method('getContent')->willReturn('userId;sessionKey');
  31. // Mock the response that will be returned by the user_status request
  32. $responseContent = $this->getContentFromFixture('user_status.json');
  33. $response = $this->getMockBuilder(ResponseInterface::class)
  34. ->disableOriginalConstructor()
  35. ->getMock();
  36. $response->method('getContent')->willReturn($responseContent);
  37. $this->client
  38. ->method('request')
  39. ->withConsecutive(
  40. ["GET", "login?username=user&password=pwd"],
  41. ["GET", "status?getMoney=true&typeAliases=true", ['headers' => [ 'user_key' => 'userId', 'Session_key' => 'sessionKey' ]]]
  42. )
  43. ->willReturnOnConsecutiveCalls(
  44. $connectResponse,
  45. $response
  46. );
  47. $data = $this->mobytService->getUserStatus(1, 'user', 'pwd');
  48. $this->assertEquals(
  49. $data['money'],
  50. 33.0
  51. );
  52. }
  53. }