Skip to content

Commit

Permalink
Merge pull request #1 from HaiD84/new_methods
Browse files Browse the repository at this point in the history
New methods
  • Loading branch information
luza authored Apr 7, 2022
2 parents f076e73 + e775cd4 commit 0eaa79d
Show file tree
Hide file tree
Showing 9 changed files with 271 additions and 10 deletions.
14 changes: 9 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php: [7.2, 7.3, 7.4]
php: [7.2, 7.3, 7.4, 8.0, 8.1]
os: [ubuntu-latest]
env: ['low', 'high']
name: PHP ${{ matrix.php }} Test ${{ matrix.env }} on ${{ matrix.os }}
Expand All @@ -24,7 +24,7 @@ jobs:
- uses: actions/checkout@v2

- name: Install PHP
uses: shivammathur/setup-php@1.7.0
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: json, mbstring
Expand All @@ -39,11 +39,11 @@ jobs:
run: echo "::set-output name=dir::$(composer config cache-files-dir)"

- name: Cache dependencies
uses: actions/cache@v1.0.3
uses: actions/cache@v2
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-composer-
key: ${{ runner.os }}-composer-${{ matrix.env }}-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-composer-${{ matrix.env }}-

- name: Composer update
run: |
Expand All @@ -52,9 +52,13 @@ jobs:
fi;
if [ "$DEPENDENCIES" = 'low' ]; then
composer update $DEFAULT_COMPOSER_FLAGS --prefer-lowest
if [ "$PHP_VERSION" = '8' ] || [ "$PHP_VERSION" = '8.1' ]; then
composer update $DEFAULT_COMPOSER_FLAGS --prefer-lowest --with-all-dependencies jms/serializer:^3.14
fi;
fi;
env:
DEPENDENCIES: ${{ matrix.env }}
PHP_VERSION: ${{ matrix.php }}

- name: Run phpunit
run: ./vendor/bin/phpunit
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
"jms/serializer": "^1.4 || ^2.0 || ^3.0"
},
"require-dev": {
"phpstan/phpstan": "^0.12.5",
"phpstan/phpstan-phpunit": "^0.12.6",
"phpunit/phpunit": "^8.5"
"phpstan/phpstan": "^1.0",
"phpstan/phpstan-phpunit": "^1.0",
"phpunit/phpunit": "^8.5.23 || ^9.0"
},
"autoload": {
"psr-4": {
Expand Down
16 changes: 16 additions & 0 deletions src/Dto/BalanceResponse.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 BalanceResponse extends BaseResponse
{
/**
* @var BalanceResult
* @JMS\Type("Feech\SmsAero\Dto\BalanceResult")
*/
public $data;
}
16 changes: 16 additions & 0 deletions src/Dto/BalanceResult.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 BalanceResult
{
/**
* @var float
* @JMS\Type("float")
*/
public $balance;
}
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;
}
54 changes: 52 additions & 2 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 @@ -210,10 +214,27 @@ public function testSend(
/**
* Запрос баланса
*
* @return string
* @return Dto\BalanceResponse
* @throws BaseSmsAeroException
* @throws \InvalidArgumentException
*/
//public function balance(): string
public function balance(): Dto\BalanceResponse
{
try {
$jsonResponse = $this->rawJsonClient->balance();

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

return $result;
}

/**
* Запрос тарифа
Expand Down Expand Up @@ -263,4 +284,33 @@ public function testSend(
* @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;
}
}
73 changes: 73 additions & 0 deletions tests/SmsAeroClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,4 +245,77 @@ public function testBulkSendWhenSendToSingleNumberShouldThrowException(): void

$this->client->bulkSend($sms);
}

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

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

$result = $this->client->balance();

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

$this->assertInstanceOf(Dto\BalanceResult::class, $result->data);
$this->assertSame(1389.26, $result->data->balance);
}

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

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

$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');
}
}
32 changes: 32 additions & 0 deletions tests/StubData.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,38 @@ public static function sendToMultipleNumbersSuccessResponse(): string
],
"message": null
}
JSON;
}

public static function balanceSuccessResponse(): string
{
return <<<JSON
{
"success": true,
"data": {
"balance": 1389.26
},
"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 0eaa79d

Please sign in to comment.