Browse Source

fix MobytUserStatusCreator and add unit tests for it

Olivier Massot 4 years ago
parent
commit
1c85fcfda4

+ 3 - 3
src/Service/Mobyt/MobytUserStatusCreator.php

@@ -28,10 +28,10 @@ class MobytUserStatusCreator
         $userStatus->setActive(true);
         $userStatus->setMoney($userStatusData['money']);
 
-        $topQualitySmsAmount = null;
+        $topQualitySmsAmount = 0;
         foreach ($userStatusData['sms'] as $_ => $smsTypeData) {
-            // we only retrieve the 'top quality sms', which are identified by the letter L in the mobyt api
-            if ($smsTypeData['type'] === 'L') {
+            // we only retrieve the 'top quality sms', which are identified by the letter N in the mobyt api
+            if ($smsTypeData['type'] === 'N') {
                 $topQualitySmsAmount = $smsTypeData['quantity'];
             }
         }

+ 73 - 0
tests/Service/Mobyt/MobytUserStatusCreatorTest.php

@@ -0,0 +1,73 @@
+<?php
+
+namespace App\Tests\Service\Mobyt;
+
+use App\Entity\Organization\Organization;
+use App\Entity\Organization\Parameters;
+use App\Repository\Organization\OrganizationRepository;
+use App\Service\Mobyt\MobytService;
+use App\Service\Mobyt\MobytUserStatusCreator;
+use PHPUnit\Framework\TestCase;
+
+class MobytUserStatusCreatorTest extends TestCase
+{
+    private MobytService $mobytService;
+    private OrganizationRepository $organizationRepository;
+    private MobytUserStatusCreator $mobytUserStatusCreator;
+
+    public function setUp(): void {
+        $this->mobytService = $this->getMockBuilder(MobytService::class)
+            ->disableOriginalConstructor()
+            ->getMock();
+
+        $this->organizationRepository = $this->getMockBuilder(OrganizationRepository::class)
+            ->disableOriginalConstructor()
+            ->getMock();
+
+        $this->mobytUserStatusCreator = new MobytUserStatusCreator(
+            $this->mobytService,
+            $this->organizationRepository
+        );
+    }
+
+    private function getJsonContentFromFixture(string $filename): array {
+        $filepath = dirname(__FILE__) . '/fixtures/' . $filename;
+        return json_decode(file_get_contents($filepath), true);
+    }
+
+    public function testGetUserStatus() {
+        $parameters = $this->getMockBuilder(Parameters::class)->getMock();
+        $parameters->expects($this->once())->method('getUsernameSMS')->willReturn('user');
+        $parameters->expects($this->once())->method('getPasswordSMS')->willReturn('pwd');
+
+        $organization = $this->getMockBuilder(Organization::class)->getMock();
+        $organization->expects($this->once())->method('getParameters')->willReturn($parameters);
+
+        $this->organizationRepository
+            ->expects($this->once())
+            ->method('find')
+            ->with(1)
+            ->willReturn($organization);
+
+        $this->mobytService
+            ->expects($this->once())
+            ->method('getUserStatus')
+            ->with(1, 'user', 'pwd')
+            ->willReturn(
+                $this->getJsonContentFromFixture('user_status.json')
+            );
+
+        $mobytUserStatus = $this->mobytUserStatusCreator->getUserStatus(1);
+
+        $this->assertEquals(
+            $mobytUserStatus->getMoney(),
+            33.0
+        );
+
+        // check that top-level amoung is taken in account, and only it
+        $this->assertEquals(
+            $mobytUserStatus->getAmount(),
+            300
+        );
+    }
+}