Skip to content

Commit

Permalink
flashcall/send method
Browse files Browse the repository at this point in the history
  • Loading branch information
HaiD84 committed Apr 5, 2022
1 parent 98872b7 commit e775cd4
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Dto/FlashCallResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Feech\SmsAero\Dto;

use JMS\Serializer\Annotation as JMS;

class FlashCallResponse extends BaseResponse
{
/**
* @var FlashCallStatus|null
* @JMS\Type("Feech\SmsAero\Dto\FlashCallStatus")
*/
public $data;
}
54 changes: 54 additions & 0 deletions src/Dto/FlashCallStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace Feech\SmsAero\Dto;

use JMS\Serializer\Annotation as JMS;

class FlashCallStatus
{
/**
* @var int
* @JMS\Type("int")
*/
public $id;

/**
* @var int
* @JMS\Type("int")
*/
public $status;

/**
* @var string
* @JMS\Type("string")
*/
public $code;

/**
* @var string
* @JMS\Type("string")
*/
public $phone;

/**
* @var float
* @JMS\Type("float")
*/
public $cost;

/**
* @var int
* @JMS\Type("int")
* @JMS\SerializedName("timeCreate")
*/
public $timeCreate;

/**
* @var int
* @JMS\Type("int")
* @JMS\SerializedName("timeUpdate")
*/
public $timeUpdate;
}
33 changes: 33 additions & 0 deletions src/SmsAeroClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

class SmsAeroClient
{
/** @var IClient */
private $client;

/** @var SmsAero */
private $rawJsonClient;

Expand All @@ -21,6 +24,7 @@ class SmsAeroClient

public function __construct(IClient $client, SerializerInterface $serializer)
{
$this->client = $client;
$this->rawJsonClient = new SmsAero($client);
$this->serializer = $serializer;
}
Expand Down Expand Up @@ -280,4 +284,33 @@ public function balance(): Dto\BalanceResponse
* @throws BaseSmsAeroException
*/
//public function operatorNumber(string $number): string

/**
* @param string $number
* @param string $code
*
* @return Dto\FlashCallResponse
* @throws BaseSmsAeroException
* @throws \InvalidArgumentException
*/
public function flashCall(string $number, string $code): Dto\FlashCallResponse
{
try {
$jsonResponse = $this->client->request('/flashcall/send', [
'phone' => $number,
'code' => $code,
]);

$result = $this->serializer->deserialize(
$jsonResponse,
Dto\FlashCallResponse::class,
'json'
);
assert($result instanceof Dto\FlashCallResponse);
} catch (RuntimeException $e) {
throw BadResponseException::becauseOfDeserializationError($e);
}

return $result;
}
}
42 changes: 42 additions & 0 deletions tests/SmsAeroClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,4 +276,46 @@ public function testBalanceWhenUnknownResponseShouldThrowException(): void

$this->client->balance();
}

public function testFlashCallWhenSuccess(): void
{
$this->mockHandler->append(function (Request $request, array $options) {
$this->assertStringContainsString(
'/v2/flashcall/send',
(string) $request->getUri()
);

return new Response(200, [], StubData::flashCallSendSuccessResponse());
});

$result = $this->client->flashCall('79990000000', '1234');

$this->assertTrue($result->success);
$this->assertNull($result->message);

$this->assertInstanceOf(Dto\FlashCallStatus::class, $result->data);
$this->assertFlashCallStatusToTestData($result->data);
}

private function assertFlashCallStatusToTestData(Dto\FlashCallStatus $result): void
{
$this->assertSame(1, $result->id);
$this->assertSame(0, $result->status);
$this->assertSame('1234', $result->code);
$this->assertSame('79990000000', $result->phone);
$this->assertSame(0.59, $result->cost);
$this->assertSame(1646926190, $result->timeCreate);
$this->assertSame(1646926190, $result->timeUpdate);
}

public function testFlashCallWhenUnknownResponseShouldThrowException(): void
{
$this->mockHandler->append(
new Response(200, [], '')
);

$this->expectException(BadResponseException::class);

$this->client->flashCall('79990000000', '1234');
}
}
19 changes: 19 additions & 0 deletions tests/StubData.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,25 @@ public static function balanceSuccessResponse(): string
},
"message": null
}
JSON;
}

public static function flashCallSendSuccessResponse(): string
{
return <<<JSON
{
"success": true,
"data": {
"id": 1,
"status": 0,
"code": "1234",
"phone": "79990000000",
"cost": "0.59",
"timeCreate": 1646926190,
"timeUpdate": 1646926190
},
"message": null
}
JSON;
}
}

0 comments on commit e775cd4

Please sign in to comment.