-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathSaveEntryClassesAndNormalizationTest.php
More file actions
175 lines (142 loc) · 5.68 KB
/
Copy pathSaveEntryClassesAndNormalizationTest.php
File metadata and controls
175 lines (142 loc) · 5.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
/*
* Copyright (c) 2025-2026 Netresearch DTT GmbH
* SPDX-License-Identifier: AGPL-3.0-only
*/
declare(strict_types=1);
namespace Tests\Controller;
use App\Entity\Entry;
use App\Enum\EntryClass;
use Symfony\Component\HttpFoundation\Request;
use Tests\AbstractWebTestCase;
/**
* Day-class recalculation (day break / pause / overlap, see #305) and
* v4-parity ticket normalization on /tracking/save.
*
* @internal
*
* @coversNothing
*/
final class SaveEntryClassesAndNormalizationTest extends AbstractWebTestCase
{
private const string NINE = '09:00:00';
private const string TEN = '10:00:00';
/**
* @param array<string, string|int> $overrides
*
* @return array<string, string|int>
*/
private function saveParameters(array $overrides = []): array
{
return $overrides + [
'date' => '2024-03-11',
'project_id' => 1,
'customer_id' => 1,
'activity_id' => 1,
];
}
/**
* @param array<string, string|int> $parameters
*
* @return array<mixed>
*/
private function saveEntry(array $parameters): array
{
$this->client->request(Request::METHOD_POST, '/tracking/save', $parameters, [], ['HTTP_ACCEPT' => 'application/json']);
$this->assertStatusCode(200);
$data = json_decode((string) $this->client->getResponse()->getContent(), true);
self::assertIsArray($data);
self::assertArrayHasKey('result', $data);
self::assertIsArray($data['result']);
return $data['result'];
}
public function testFirstEntryOfDayGetsDaybreakClass(): void
{
$this->logInSession('unittest');
$result = $this->saveEntry($this->saveParameters(['start' => self::NINE, 'end' => self::TEN]));
self::assertSame(EntryClass::DAYBREAK->value, $result['class']);
}
public function testSeamlessFollowUpEntryGetsPlainClass(): void
{
$this->logInSession('unittest');
$this->saveEntry($this->saveParameters(['start' => self::NINE, 'end' => self::TEN]));
$result = $this->saveEntry($this->saveParameters(['start' => self::TEN, 'end' => '10:30:00']));
self::assertSame(EntryClass::PLAIN->value, $result['class']);
}
public function testEntryAfterGapGetsPauseClass(): void
{
$this->logInSession('unittest');
$this->saveEntry($this->saveParameters(['start' => self::NINE, 'end' => self::TEN]));
$result = $this->saveEntry($this->saveParameters(['start' => '10:30:00', 'end' => '11:00:00']));
self::assertSame(EntryClass::PAUSE->value, $result['class']);
}
public function testOverlappingEntryGetsOverlapClass(): void
{
$this->logInSession('unittest');
$this->saveEntry($this->saveParameters(['start' => self::NINE, 'end' => self::TEN]));
$result = $this->saveEntry($this->saveParameters(['start' => '09:30:00', 'end' => '10:30:00']));
self::assertSame(EntryClass::OVERLAP->value, $result['class']);
}
public function testTicketIsUpperCasedAndTrimmedOnSave(): void
{
$this->logInSession('unittest');
// fixture project 1 has jira_id 'SA'; lower-case input must pass the
// prefix validation and be stored normalized (v4 parity)
$result = $this->saveEntry($this->saveParameters([
'start' => self::NINE,
'end' => self::TEN,
'ticket' => 'sa-123',
]));
self::assertSame('SA-123', $result['ticket']);
}
public function testExtTicketIsPersistedAsInternalJiraTicketOriginalKey(): void
{
$this->logInSession('unittest');
$result = $this->saveEntry($this->saveParameters([
'start' => self::NINE,
'end' => self::TEN,
'ticket' => 'SA-77',
'extTicket' => 'EXT-77',
]));
$entryId = $result['id'];
self::assertIsInt($entryId);
$container = $this->client->getContainer();
/** @var \Doctrine\Bundle\DoctrineBundle\Registry $doctrine */
$doctrine = $container->get('doctrine');
$entry = $doctrine->getManager()->getRepository(Entry::class)->find($entryId);
self::assertInstanceOf(Entry::class, $entry);
self::assertSame('EXT-77', $entry->getInternalJiraTicketOriginalKey());
}
public function testClearingTicketAndDescriptionPersists(): void
{
$this->logInSession('unittest');
// create an entry carrying both a ticket and a description ...
$created = $this->saveEntry($this->saveParameters([
'start' => self::NINE,
'end' => self::TEN,
'ticket' => 'SA-123',
'description' => 'initial note',
]));
$entryId = $created['id'];
self::assertIsInt($entryId);
self::assertSame('SA-123', $created['ticket']);
// ... then re-save the same entry with both fields cleared. The clear
// must persist instead of silently reverting on the next refetch.
$this->saveEntry($this->saveParameters([
'id' => $entryId,
'start' => self::NINE,
'end' => self::TEN,
'ticket' => '',
'description' => '',
]));
$container = $this->client->getContainer();
/** @var \Doctrine\Bundle\DoctrineBundle\Registry $doctrine */
$doctrine = $container->get('doctrine');
$manager = $doctrine->getManager();
$manager->clear(); // re-read the persisted state, not the unit-of-work cache
$entry = $manager->getRepository(Entry::class)->find($entryId);
self::assertInstanceOf(Entry::class, $entry);
self::assertSame('', $entry->getTicket());
self::assertSame('', $entry->getDescription());
}
}