| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- <?php
- namespace App\Tests\Service\Mobyt;
- use App\Service\Mobyt\MobytService;
- use PHPUnit\Framework\TestCase;
- use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
- use Symfony\Contracts\HttpClient\HttpClientInterface;
- use Symfony\Contracts\HttpClient\ResponseInterface;
- class MobytServiceTest extends TestCase
- {
- private HttpClientInterface $client;
- private MobytService $mobytService;
- public function setUp():void
- {
- $this->client = $this->getMockBuilder(HttpClientInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->mobytService = new MobytService($this->client);
- }
- private function getContentFromFixture(string $filename): string {
- $filepath = dirname(__FILE__) . '/fixtures/' . $filename;
- return file_get_contents($filepath);
- }
- /**
- * @see MobytService::getUserStatus()
- */
- public function testGetUserStatus(): void {
- // Mock the response that will be returned by the connection request
- $connectResponse = $this->getMockBuilder(ResponseInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $connectResponse->method('getContent')->willReturn('userId;sessionKey');
- // Mock the response that will be returned by the user_status request
- $responseContent = $this->getContentFromFixture('user_status.json');
- $response = $this->getMockBuilder(ResponseInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $response->method('getContent')->willReturn($responseContent);
- $this->client
- ->method('request')
- ->withConsecutive(
- ["GET", "login?username=user&password=pwd"],
- ["GET", "status?getMoney=true&typeAliases=true", ['headers' => [ 'user_key' => 'userId', 'Session_key' => 'sessionKey' ]]]
- )
- ->willReturnOnConsecutiveCalls(
- $connectResponse,
- $response
- );
- $data = $this->mobytService->getUserStatus(1, 'user', 'pwd');
- $this->assertEquals(
- $data['money'],
- 33.0
- );
- }
- /**
- * @see MobytService::hasCredentialsCorrect()
- */
- public function testHasCredentialsCorrect(): void {
- // Mock the response that will be returned by the connection request
- $connectResponse = $this->getMockBuilder(ResponseInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $connectResponse->method('getContent')->willReturn('userId;sessionKey');
- $this->client
- ->method('request')
- ->with("GET", "login?username=user&password=pwd")
- ->willReturnOnConsecutiveCalls(
- $connectResponse
- );
- $this->assertTrue($this->mobytService->hasCredentialsCorrect('user', 'pwd'));
- }
- /**
- * @see MobytService::hasCredentialsCorrect()
- */
- public function testHasCredentialsUnCorrect(): void {
- // Mock the response that will be returned by the connection request
- $connectResponse = $this->getMockBuilder(ResponseInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $connectResponse->method('getContent')->willThrowException(new NotFoundHttpException());
- $this->client
- ->method('request')
- ->with("GET", "login?username=user&password=pwd")
- ->willReturnOnConsecutiveCalls(
- $connectResponse
- );
- $this->assertFalse($this->mobytService->hasCredentialsCorrect('user', 'pwd'));
- }
- }
|