Skip to content

Commit 1042f27

Browse files
committed
#3 Parse seconds, weeks and spent_at
1 parent 5568a3f commit 1042f27

File tree

2 files changed

+42
-11
lines changed

2 files changed

+42
-11
lines changed

app/Model/Service/Eloquent/EloquentNoteService.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,18 @@ public function parseSpentTime(\Traversable $list)
6262
protected function parseSpentTimeItem(Note $item, Note $prev = null): array
6363
{
6464
$hours = 0;
65+
$spentAt = null;
6566

66-
$pattern = '/(added|subtracted) (((\d{1,3}[dhm])\s+)+)of time spent at \d{4}-\d{2}-\d{2}/';
67+
$pattern = '/(added|subtracted) ((?:(?:\d{1,3}[wdhms])\s+)+)of time spent at (\d{4}-\d{2}-\d{2})/';
6768
preg_match($pattern, $item->body, $match);
6869

69-
if (!empty($match) && count($match) >= 2) {
70+
if (!empty($match) && count($match) === 4) {
7071
$sign = $match[1] === 'added' ? 1 : -1;
7172
$times = array_filter(explode(' ', $match[2]));
7273
foreach ($times as $time) {
7374
$hours += $sign * $this->parseTime(trim($time));
7475
}
76+
$spentAt = $match[3];
7577
}
7678

7779
// Get description from previous comment if its corresponds to
@@ -89,6 +91,7 @@ protected function parseSpentTimeItem(Note $item, Note $prev = null): array
8991

9092
$row = [
9193
'note_id' => $item->id,
94+
'spent_at' => $spentAt,
9295
'gitlab_created_at' => $item->gitlab_created_at,
9396
'hours' => $hours,
9497
'description' => $description,
@@ -105,6 +108,9 @@ protected function parseTime(string $time)
105108
{
106109
$value = mb_substr($time, 0, -1);
107110
$period = mb_substr($time, -1);
111+
if ($period === 's') {
112+
return $value / 3600;
113+
}
108114
if ($period === 'm') {
109115
return $value / 60;
110116
}
Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use App\Providers\AppServiceProvider;
88
use Tests\TestCase;
99

10-
class TestEloquentNoteService extends TestCase
10+
class EloquentNoteServiceTest extends TestCase
1111
{
1212

1313
public function setUp()
@@ -34,6 +34,18 @@ public function testParseTime(string $time, float $expectedHours)
3434
public function parseTimeDataProvider()
3535
{
3636
return [
37+
[
38+
'time' => '0s',
39+
'hours' => 0,
40+
],
41+
[
42+
'time' => '36s',
43+
'hours' => 0.01,
44+
],
45+
[
46+
'time' => '360s',
47+
'hours' => 0.1,
48+
],
3749
[
3850
'time' => '0m',
3951
'hours' => 0,
@@ -99,7 +111,7 @@ public function testParseSpentTimeItem(array $itemData, array $prevItemData = nu
99111
$prev = $prevItemData ? new Note($prevItemData) : null;
100112
$row = $method->invokeArgs($service, [$item, $prev]);
101113
$this->assertEquals($expectedRow['hours'], $row['hours']);
102-
// $this->assertEquals($expectedRow['spent_at'], $row['spent_at']);
114+
$this->assertEquals($expectedRow['spent_at'], $row['spent_at']);
103115
$this->assertEquals($expectedRow['gitlab_created_at'], $row['gitlab_created_at']);
104116
$this->assertEquals($expectedRow['description'], $row['description']);
105117
}
@@ -125,7 +137,20 @@ public function parseSpentTimeItemDataProvider()
125137
'prevItemData' => null,
126138
'expectedRow' => [
127139
'hours' => 1.5,
128-
'spent_at' => null,
140+
'spent_at' => '2018-11-24',
141+
'gitlab_created_at' => '2018-01-01T12:12:13',
142+
'description' => null,
143+
],
144+
],
145+
[
146+
'itemData' => [
147+
'body' => 'added 1h 30m 36s of time spent at 2018-11-14 just now ',
148+
'gitlab_created_at' => '2018-01-01T12:12:13',
149+
],
150+
'prevItemData' => null,
151+
'expectedRow' => [
152+
'hours' => 1.51,
153+
'spent_at' => '2018-11-14',
129154
'gitlab_created_at' => '2018-01-01T12:12:13',
130155
'description' => null,
131156
],
@@ -199,7 +224,7 @@ public function parseSpentTimeItemDataProvider()
199224

200225
[
201226
'itemData' => [
202-
'body' => 'added 2h of time spent at 2018-11-24 just now',
227+
'body' => 'added 2h of time spent at 2018-11-21 just now',
203228
'gitlab_created_at' => '2018-01-01T12:12:12',
204229
],
205230
'prevItemData' => [
@@ -208,14 +233,14 @@ public function parseSpentTimeItemDataProvider()
208233
],
209234
'expectedRow' => [
210235
'hours' => 2,
211-
'spent_at' => null,
236+
'spent_at' => '2018-11-21',
212237
'gitlab_created_at' => '2018-01-01T12:12:12',
213238
'description' => 'added 1h 30m of time spent at 2018-11-24 just now',
214239
],
215240
],
216241
[
217242
'itemData' => [
218-
'body' => 'added 2h of time spent at 2018-11-24 just now',
243+
'body' => 'added 2h of time spent at 2018-11-23 just now',
219244
'gitlab_created_at' => '2018-01-02T12:12:12',
220245
],
221246
'prevItemData' => [
@@ -224,7 +249,7 @@ public function parseSpentTimeItemDataProvider()
224249
],
225250
'expectedRow' => [
226251
'hours' => 2,
227-
'spent_at' => null,
252+
'spent_at' => '2018-11-23',
228253
'gitlab_created_at' => '2018-01-02T12:12:12',
229254
'description' => null,
230255
],
@@ -241,7 +266,7 @@ public function parseSpentTimeItemDataProvider()
241266
],
242267
'expectedRow' => [
243268
'hours' => 3.5,
244-
'spent_at' => null,
269+
'spent_at' => '2018-11-24',
245270
'gitlab_created_at' => '2018-01-01T12:12:12',
246271
'description' => 'test primary with hours',
247272
],
@@ -257,7 +282,7 @@ public function parseSpentTimeItemDataProvider()
257282
],
258283
'expectedRow' => [
259284
'hours' => 4.5,
260-
'spent_at' => null,
285+
'spent_at' => '2018-11-24',
261286
'gitlab_created_at' => '2018-01-02T12:12:12',
262287
'description' => null,
263288
],

0 commit comments

Comments
 (0)