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
2 changes: 1 addition & 1 deletion Helpers/RefundHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ private function buildPayload(object $order, array $built): array

$payload = [
'client_id' => (string)$cid,
'timestamp_micros' => (string)microtime(true) * 1000000,
'timestamp_micros' => (string)(int)(microtime(true) * 1000000),
'non_personalized_ads' => false,
'events' => [
[
Expand Down
2 changes: 1 addition & 1 deletion Init/module.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"Okay": "4.5.3",
"version": "1.1.0",
"version": "1.1.1",
"moduleName": "Аналітика GA4",
"vendor": {
"email": "devsviat@proton.me",
Expand Down
48 changes: 48 additions & 0 deletions tests/RefundTimestampTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Modules\Sviat\Analytics;

use Okay\Core\EntityFactory;
use Okay\Core\Settings;
use Okay\Entities\CurrenciesEntity;
use Okay\Modules\Sviat\Analytics\Helpers\AnalyticsHelper;
use Okay\Modules\Sviat\Analytics\Helpers\RefundHelper;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use ReflectionMethod;

/**
* `(string)microtime(true) * 1000000` множить уже рядок, тож у JSON ішло
* число з плаваючою комою (1.7e+15), а не ціле число мікросекунд.
*/
class RefundTimestampTest extends TestCase
{
public function testTimestampMicrosIsAnIntegerString(): void
{
$currencies = $this->createStub(CurrenciesEntity::class);
$currencies->method('getMainCurrency')->willReturn((object) ['code' => 'UAH']);
$entityFactory = $this->createStub(EntityFactory::class);
$entityFactory->method('get')->willReturn($currencies);

$helper = new RefundHelper(
$entityFactory,
new ReflectionClass(AnalyticsHelper::class)->newInstanceWithoutConstructor(),
new ReflectionClass(Settings::class)->newInstanceWithoutConstructor(),
);

$before = (int) (microtime(true) * 1000000);
$payload = new ReflectionMethod(RefundHelper::class, 'buildPayload')->invoke(
$helper,
(object) ['id' => 7, 'cid' => '1.2', 'sid' => '', 'user_id' => 0],
['items' => [], 'shipping' => 0, 'value' => 10],
);
$after = (int) (microtime(true) * 1000000);

$micros = $payload['timestamp_micros'];
self::assertIsString($micros);
self::assertMatchesRegularExpression('/^\d{16}$/', $micros);
self::assertGreaterThanOrEqual($before, (int) $micros);
self::assertLessThanOrEqual($after, (int) $micros);
self::assertMatchesRegularExpression('/"timestamp_micros":"\d{16}"/', json_encode($payload));
}
}