Skip to content

Commit f6d0307

Browse files
drauinorbybaru
andauthored
fix: setting default dimensions as empty array (norbybaru#37)
* fix: setting default dimensions as empty array, if not provided (or provided as empty) norbybaru#33 * fix: initialization of $dimensions has moved to class scope + added unit tests for payload builder norbybaru#33 * fix: styleci norbybaru#33 --------- Co-authored-by: Norby Baruani <norby.baruani@gmail.com>
1 parent 2c7dd79 commit f6d0307

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

src/Builder/PayloadBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
final class PayloadBuilder implements PayloadBuilderContract
99
{
10-
protected array $dimensions;
10+
protected array $dimensions = [];
1111

1212
public function __construct(
1313
protected string $measureName,

tests/Unit/PayloadBuilderUnitTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace NorbyBaru\AwsTimestream\Tests\Unit;
4+
5+
use Illuminate\Support\Carbon;
6+
use NorbyBaru\AwsTimestream\Builder\PayloadBuilder;
7+
use NorbyBaru\AwsTimestream\Tests\TestCase;
8+
9+
class PayloadBuilderUnitTest extends TestCase
10+
{
11+
public function test_it_has_initialized_correctly()
12+
{
13+
// create a time of "2024-01-11T15:49:17+00:00" (that equals to 1704988157000)
14+
$time = Carbon::create(2024, 1, 11, 15, 49, 17, 'UTC');
15+
$payloadBuilder = PayloadBuilder::make(
16+
'test',
17+
1,
18+
$time,
19+
'DOUBLE'
20+
);
21+
22+
try {
23+
$metric = $payloadBuilder->toArray(true);
24+
} catch (\Exception $e) {
25+
$this->fail($e->getMessage());
26+
}
27+
28+
$this->assertIsArray($metric);
29+
$this->assertArrayHasKey('MeasureName', $metric);
30+
$this->assertArrayHasKey('MeasureValue', $metric);
31+
$this->assertArrayHasKey('MeasureValueType', $metric);
32+
$this->assertArrayHasKey('Time', $metric);
33+
$this->assertArrayHasKey('Dimensions', $metric);
34+
35+
$this->assertEquals('test', $metric['MeasureName']);
36+
$this->assertEquals('1', $metric['MeasureValue']);
37+
$this->assertEquals('DOUBLE', $metric['MeasureValueType']);
38+
$this->assertEquals("1704988157000", $metric['Time']);
39+
$this->assertEmpty($metric['Dimensions']);
40+
}
41+
}

0 commit comments

Comments
 (0)