Skip to content

Commit 037deec

Browse files
committed
Refactored LogLogin entity and test
1 parent 32222b0 commit 037deec

File tree

3 files changed

+24
-101
lines changed

3 files changed

+24
-101
lines changed

src/Entity/LogLogin.php

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class LogLogin implements EntityInterface
6868
'LogLogin',
6969
'LogLogin.id',
7070
])]
71-
private UuidInterface $id;
71+
private readonly UuidInterface $id;
7272

7373
#[ORM\Column(
7474
name: 'username',
@@ -80,7 +80,7 @@ class LogLogin implements EntityInterface
8080
'LogLogin',
8181
'LogLogin.username',
8282
])]
83-
private string $username = '';
83+
private readonly string $username;
8484

8585
#[ORM\Column(
8686
name: 'client_type',
@@ -92,7 +92,7 @@ class LogLogin implements EntityInterface
9292
'LogLogin',
9393
'LogLogin.clientType',
9494
])]
95-
private ?string $clientType = null;
95+
private readonly string $clientType;
9696

9797
#[ORM\Column(
9898
name: 'client_name',
@@ -104,7 +104,7 @@ class LogLogin implements EntityInterface
104104
'LogLogin',
105105
'LogLogin.clientName',
106106
])]
107-
private ?string $clientName = null;
107+
private readonly string $clientName;
108108

109109
#[ORM\Column(
110110
name: 'client_short_name',
@@ -116,7 +116,7 @@ class LogLogin implements EntityInterface
116116
'LogLogin',
117117
'LogLogin.clientShortName',
118118
])]
119-
private ?string $clientShortName = null;
119+
private readonly string $clientShortName;
120120

121121
#[ORM\Column(
122122
name: 'client_version',
@@ -128,7 +128,7 @@ class LogLogin implements EntityInterface
128128
'LogLogin',
129129
'LogLogin.clientVersion',
130130
])]
131-
private ?string $clientVersion = null;
131+
private readonly string $clientVersion;
132132

133133
#[ORM\Column(
134134
name: 'client_engine',
@@ -140,7 +140,7 @@ class LogLogin implements EntityInterface
140140
'LogLogin',
141141
'LogLogin.clientEngine',
142142
])]
143-
private ?string $clientEngine = null;
143+
private readonly string $clientEngine;
144144

145145
#[ORM\Column(
146146
name: 'os_name',
@@ -152,7 +152,7 @@ class LogLogin implements EntityInterface
152152
'LogLogin',
153153
'LogLogin.osName',
154154
])]
155-
private ?string $osName = null;
155+
private readonly string $osName;
156156

157157
#[ORM\Column(
158158
name: 'os_short_name',
@@ -164,7 +164,7 @@ class LogLogin implements EntityInterface
164164
'LogLogin',
165165
'LogLogin.osShortName',
166166
])]
167-
private ?string $osShortName = null;
167+
private readonly string $osShortName;
168168

169169
#[ORM\Column(
170170
name: 'os_version',
@@ -176,7 +176,7 @@ class LogLogin implements EntityInterface
176176
'LogLogin',
177177
'LogLogin.osVersion',
178178
])]
179-
private ?string $osVersion = null;
179+
private readonly string $osVersion;
180180

181181
#[ORM\Column(
182182
name: 'os_platform',
@@ -188,7 +188,7 @@ class LogLogin implements EntityInterface
188188
'LogLogin',
189189
'LogLogin.osPlatform',
190190
])]
191-
private ?string $osPlatform = null;
191+
private readonly string $osPlatform;
192192

193193
#[ORM\Column(
194194
name: 'device_name',
@@ -200,7 +200,7 @@ class LogLogin implements EntityInterface
200200
'LogLogin',
201201
'LogLogin.deviceName',
202202
])]
203-
private ?string $deviceName = null;
203+
private readonly string $deviceName;
204204

205205
#[ORM\Column(
206206
name: 'brand_name',
@@ -212,7 +212,7 @@ class LogLogin implements EntityInterface
212212
'LogLogin',
213213
'LogLogin.brandName',
214214
])]
215-
private ?string $brandName = null;
215+
private readonly string $brandName;
216216

217217
#[ORM\Column(
218218
name: 'model',
@@ -224,9 +224,7 @@ class LogLogin implements EntityInterface
224224
'LogLogin',
225225
'LogLogin.model',
226226
])]
227-
private ?string $model = null;
228-
229-
private DeviceDetector $deviceDetector;
227+
private readonly string $model;
230228

231229
/**
232230
* LogLogin constructor.
@@ -242,9 +240,9 @@ public function __construct(
242240
'LogLogin',
243241
'LogLogin.type',
244242
])]
245-
private string $type,
246-
Request $request,
247-
DeviceDetector $deviceDetector,
243+
private readonly string $type,
244+
private Request $request,
245+
private readonly DeviceDetector $deviceDetector,
248246
#[ORM\ManyToOne(
249247
targetEntity: User::class,
250248
inversedBy: 'logsLogin',
@@ -257,19 +255,15 @@ public function __construct(
257255
'LogLogin',
258256
'LogLogin.user',
259257
])]
260-
private ?User $user = null
258+
private readonly ?User $user = null
261259
) {
262260
$this->id = $this->createUuid();
263261

264-
$this->deviceDetector = $deviceDetector;
265-
266262
$this->processTimeAndDate();
267263
$this->processRequestData($request);
268264
$this->processClientData();
269265

270-
if ($this->user !== null) {
271-
$this->username = $this->user->getUsername();
272-
}
266+
$this->username = $this->user?->getUsername() ?? '';
273267
}
274268

275269
public function getId(): string

src/Entity/Traits/LogEntityTrait.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ trait LogEntityTrait
3838
'LogRequest',
3939
'LogRequest.time',
4040
])]
41-
protected DateTimeImmutable $time;
41+
protected readonly DateTimeImmutable $time;
4242

4343
#[ORM\Column(
4444
name: '`date`',
@@ -51,7 +51,7 @@ trait LogEntityTrait
5151
'LogRequest',
5252
'LogRequest.date',
5353
])]
54-
protected DateTimeImmutable $date;
54+
protected readonly DateTimeImmutable $date;
5555

5656
#[ORM\Column(
5757
name: 'agent',
@@ -64,7 +64,7 @@ trait LogEntityTrait
6464
'LogRequest',
6565
'LogRequest.agent',
6666
])]
67-
protected string $agent = '';
67+
protected readonly string $agent;
6868

6969
#[ORM\Column(
7070
name: 'http_host',
@@ -78,7 +78,7 @@ trait LogEntityTrait
7878
'LogRequest',
7979
'LogRequest.httpHost',
8080
])]
81-
protected string $httpHost = '';
81+
protected readonly string $httpHost;
8282

8383
#[ORM\Column(
8484
name: 'client_ip',
@@ -92,7 +92,7 @@ trait LogEntityTrait
9292
'LogRequest',
9393
'LogRequest.clientIp',
9494
])]
95-
private string $clientIp = '';
95+
private readonly string $clientIp;
9696

9797
public function getTime(): DateTimeImmutable
9898
{

tests/Integration/Entity/LogLoginTest.php

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -34,77 +34,6 @@ class LogLoginTest extends EntityTestCase
3434
*/
3535
protected string $entityName = LogLogin::class;
3636

37-
/** @noinspection PhpMissingParentCallCommonInspection */
38-
/**
39-
* @testdox No setter for `$property` property in read only entity - so cannot test this
40-
*/
41-
public function testThatSetterOnlyAcceptSpecifiedType(
42-
?string $property = null,
43-
?string $type = null,
44-
?array $meta = null
45-
): void {
46-
self::markTestSkipped('There is not setter in read only entity...');
47-
}
48-
49-
/** @noinspection PhpMissingParentCallCommonInspection */
50-
/**
51-
* @testdox No setter for `$property` property in read only entity - so cannot test this
52-
*/
53-
public function testThatSetterReturnsInstanceOfEntity(
54-
?string $property = null,
55-
?string $type = null,
56-
?array $meta = null
57-
): void {
58-
self::markTestSkipped('There is not setter in read only entity...');
59-
}
60-
61-
/** @noinspection PhpMissingParentCallCommonInspection */
62-
/**
63-
* @dataProvider dataProviderTestThatSetterAndGettersWorks
64-
*
65-
* @throws Throwable
66-
*
67-
* @testdox Test that getter method for `$type $property` property returns expected
68-
*/
69-
public function testThatGetterReturnsExpectedValue(string $property, string $type, array $meta): void
70-
{
71-
$getter = 'get' . ucfirst($property);
72-
73-
if (in_array($type, [PhpUnitUtil::TYPE_BOOL, PhpUnitUtil::TYPE_BOOLEAN], true)) {
74-
$getter = 'is' . ucfirst($property);
75-
}
76-
77-
$request = Request::create('');
78-
79-
// Parse user agent data with device detector
80-
$deviceDetector = new DeviceDetector((string)$request->headers->get('User-Agent'));
81-
$deviceDetector->parse();
82-
83-
$logRequest = new LogLogin(
84-
'',
85-
$request,
86-
$deviceDetector,
87-
new User()
88-
);
89-
90-
if (!(array_key_exists('columnName', $meta) || array_key_exists('joinColumns', $meta))) {
91-
$type = ArrayCollection::class;
92-
93-
self::assertInstanceOf($type, $logRequest->{$getter}());
94-
}
95-
96-
try {
97-
$method = 'assertIs' . ucfirst($type);
98-
99-
self::$method($logRequest->{$getter}());
100-
} catch (Throwable $error) {
101-
/**
102-
* @var class-string $type
103-
*/
104-
self::assertInstanceOf($type, $logRequest->{$getter}(), $error->getMessage());
105-
}
106-
}
107-
10837
/**
10938
* @noinspection PhpMissingParentCallCommonInspection
11039
*

0 commit comments

Comments
 (0)