Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Tests

on:
push:
pull_request:

jobs:
phpunit:
name: PHPUnit (PHP ${{ matrix.php-version }})
runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
php-version:
- '8.1'
- '8.2'
- '8.3'
- '8.4'
- '8.5'

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
coverage: none
tools: composer:v2

- name: Validate composer
run: composer validate --strict

- name: Install dependencies
run: composer update --prefer-dist --no-progress --no-interaction

- name: Run tests
run: composer test
32 changes: 31 additions & 1 deletion src/Response/Models/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,43 @@
*/
class BaseModel
{
/** @var array<string, mixed> */
private array $additionalProperties = [];

public function fill(stdClass $fillData): void
{
$props = get_object_vars($fillData);
$knownProperties = $this->getKnownProperties();

foreach ($props as $key => $value) {
$lowerKey = lcfirst($key);
$this->{$lowerKey} = $value;

if (isset($knownProperties[$lowerKey])) {
$this->{$lowerKey} = $value;

continue;
}

$this->additionalProperties[$lowerKey] = $value;
}
}

/**
* @return array<string, true>
*/
private function getKnownProperties(): array
{
$properties = get_class_vars(static::class);
unset($properties['additionalProperties']);

return array_fill_keys(array_keys($properties), true);
}

/**
* @return array<string, mixed>
*/
public function getAdditionalProperties(): array
{
return $this->additionalProperties;
}
}
9 changes: 9 additions & 0 deletions src/Response/Models/TransactionModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class TransactionModel extends BaseModel
public ?int $paymentCurrencyCode = null;
public ?string $invoiceId = null;
public ?string $accountId = null;
public ?int $trInitiatorCode = null;
public ?string $email = null;
public ?string $description = null;
public ?string $jsonData = null;
Expand Down Expand Up @@ -69,7 +70,15 @@ class TransactionModel extends BaseModel
public ?bool $applePay = null;
public ?bool $androidPay = null;
public ?bool $masterPass = null;
public ?string $walletType = null;
public ?float $totalFee = null;
public ?float $vatAboveTotalFee = null;
public ?float $processorAndPartnerFee = null;
public ?float $vatWithinProcessorFee = null;
public mixed $infoShopData = null;
public mixed $receiver = null;
public mixed $splits = null;
public ?bool $transactionIsInProcess = null;
public ?int $escrowAccumulationId = null;

/**
Expand Down
18 changes: 18 additions & 0 deletions tests/Response/Models/BaseModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,22 @@ public function testFillBaseModel(): void
$this->assertEquals($testObject->b, $model->b);
$this->assertEquals($testObject->c, $model->c);
}

/**
* Проверяем, что неизвестные поля не становятся динамическими свойствами.
*/
public function testFillKeepsUnknownFieldsWithoutDynamicProperties(): void
{
$model = new TestModel();

$model->fill((object) [
'a' => 1,
'b' => 2,
'c' => 3,
'UnknownField' => 'value',
]);

$this->assertSame('value', $model->getAdditionalProperties()['unknownField']);
$this->assertArrayNotHasKey('unknownField', get_object_vars($model));
}
}
49 changes: 49 additions & 0 deletions tests/Response/Models/TransactionModelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Excent\Cloudpayments\Tests\Response\Models;

use Excent\Cloudpayments\Response\Models\TransactionModel;
use PHPUnit\Framework\TestCase;

/**
* Class TransactionModelTest.
*
* @group Cloudpayments
*/
class TransactionModelTest extends TestCase
{
/**
* Проверяем заполнение полей транзакции, которые возвращает CloudPayments.
*/
public function testFillAdditionalTransactionFields(): void
{
$transaction = new TransactionModel();
$receiver = (object) ['inn' => '1234567890'];
$splits = [
(object) ['amount' => 10.50],
];
$infoShopData = (object) ['name' => 'Shop'];

$transaction->fill((object) [
'TrInitiatorCode' => 0,
'WalletType' => 'ApplePay',
'VatAboveTotalFee' => 1.25,
'ProcessorAndPartnerFee' => 2,
'VatWithinProcessorFee' => 0.25,
'InfoShopData' => $infoShopData,
'Receiver' => $receiver,
'Splits' => $splits,
'TransactionIsInProcess' => true,
]);

$this->assertSame(0, $transaction->trInitiatorCode);
$this->assertSame('ApplePay', $transaction->walletType);
$this->assertSame(1.25, $transaction->vatAboveTotalFee);
$this->assertSame(2.0, $transaction->processorAndPartnerFee);
$this->assertSame(0.25, $transaction->vatWithinProcessorFee);
$this->assertSame($infoShopData, $transaction->infoShopData);
$this->assertSame($receiver, $transaction->receiver);
$this->assertSame($splits, $transaction->splits);
$this->assertTrue($transaction->transactionIsInProcess);
}
}
Loading