Skip to content
This repository was archived by the owner on Jan 5, 2023. It is now read-only.

Commit c3036fa

Browse files
author
Mateusz Gostański
committed
Bug fixed in StraightKeyParser + added tests
1 parent 25572ed commit c3036fa

File tree

4 files changed

+86
-4
lines changed

4 files changed

+86
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
All notable changes to `api-client` will be documented in this file
44

5+
## 3.1.1 - 2021-03-09
6+
7+
- Bugs fixed
8+
59
## 3.1.0 - 2021-03-09
610

711
- Bug fixed in OAuthToken

src/Data/StraightKeyParser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function parse(Collection $inputData): Collection
2222

2323
foreach ($data as $key => $value) {
2424
try {
25-
if (Carbon::createFromTimeString($value) == true) {
25+
if (is_string($value) && strlen($value) >= 27 && Carbon::createFromTimeString($value) == true) {
2626
$data[$key] = Carbon::createFromTimeString($value);
2727
}
2828
} catch (\Exception $exception) {

tests/Data/StraightKeyParserTest.php

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,17 @@ public function it_parsing_collection_of_arrays_to_collection_of_dtos()
3131
]
3232
);
3333

34+
$this->basicAssertions($inputData);
35+
}
36+
37+
protected function basicAssertions($inputData): Collection
38+
{
3439
$returnedData = $this->obj->parse($inputData);
3540

3641
$this->assertEquals(Collection::class, $returnedData::class);
3742
$this->assertCount(1, $returnedData);
43+
44+
return $returnedData;
3845
}
3946

4047
/** @test */
@@ -52,9 +59,77 @@ public function it_skips_every_not_array_data()
5259
]
5360
);
5461

55-
$returnedData = $this->obj->parse($inputData);
62+
$this->basicAssertions($inputData);
63+
}
5664

57-
$this->assertEquals(Collection::class, $returnedData::class);
58-
$this->assertCount(1, $returnedData);
65+
/** @test */
66+
public function it_replaces_datetime_to_carbon()
67+
{
68+
$date = now();
69+
$inputData = collect(
70+
[
71+
[
72+
'first' => 'first entry',
73+
'second' => 'second entry',
74+
'third' => 'third entry',
75+
'date' => $date->toISOString(),
76+
],
77+
]
78+
);
79+
80+
81+
82+
$returnedData = $this->basicAssertions($inputData);
83+
84+
$this->assertEquals($date->timestamp, $returnedData->first()->date->timestamp);
85+
}
86+
87+
/** @test */
88+
public function it_do_not_replace_int_to_carbon()
89+
{
90+
$inputData = collect(
91+
[
92+
[
93+
'first' => 'first entry',
94+
'second' => 'second entry',
95+
'third' => 'third entry',
96+
'id' => 10987
97+
],
98+
]
99+
);
100+
101+
$this->basicAssertions($inputData);
102+
}
103+
104+
/** @test */
105+
public function it_do_not_replace_some_numeric_strings_to_carbon()
106+
{
107+
$inputData = collect(
108+
[
109+
[
110+
'first' => 'first entry',
111+
'second' => 'second entry',
112+
'third' => '568845115895',
113+
],
114+
]
115+
);
116+
117+
$this->basicAssertions($inputData);
118+
}
119+
120+
/** @test */
121+
public function it_safely_handles_strings_longer_than_27_charaters()
122+
{
123+
$inputData = collect(
124+
[
125+
[
126+
'first' => 'first entry is very long it could literally over 27 characters',
127+
'second' => 'second entry',
128+
'third' => '568845115895',
129+
],
130+
]
131+
);
132+
133+
$this->basicAssertions($inputData);
59134
}
60135
}

tests/Helpers/ExampleDto.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
namespace Grixu\ApiClient\Tests\Helpers;
44

5+
use Illuminate\Support\Carbon;
56
use Spatie\DataTransferObject\DataTransferObject;
67

78
class ExampleDto extends DataTransferObject
89
{
910
public string $first;
1011
public string $second;
1112
public string $third;
13+
public ?Carbon $date;
14+
public ?int $id;
1215
}

0 commit comments

Comments
 (0)