Browse Source

add unit tests for MobytService

Olivier Massot 4 years ago
parent
commit
d148195d48
1 changed files with 65 additions and 0 deletions
  1. 65 0
      tests/Service/Mobyt/MobytServiceTest.php

+ 65 - 0
tests/Service/Mobyt/MobytServiceTest.php

@@ -0,0 +1,65 @@
+<?php
+
+namespace App\Tests\Service\Mobyt;
+
+use App\Service\Mobyt\MobytService;
+use PHPUnit\Framework\TestCase;
+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
+        );
+    }
+}