Skip to content

Commit 33f192d

Browse files
Copilotsoyuka
andcommitted
Address PR feedback: simplify provider and improve date validation
- Move provider logic to static method inside DateTimeNormalizationIssue fixture - Remove separate DateTimeNormalizationIssueProvider class - Use short class name for provider reference with proper import - Replace regex validation with DateTime::createFromFormat for better date testing Co-authored-by: soyuka <[email protected]>
1 parent 00418a4 commit 33f192d

File tree

3 files changed

+28
-46
lines changed

3 files changed

+28
-46
lines changed

tests/Fixtures/TestBundle/ApiResource/DateTimeNormalizationIssue.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use ApiPlatform\Metadata\ApiResource;
1717
use ApiPlatform\Metadata\Get;
18+
use ApiPlatform\Metadata\Operation;
1819

1920
/**
2021
* Resource to test DateTimeImmutable normalization priority.
@@ -26,7 +27,7 @@
2627
new Get(
2728
uriTemplate: '/datetime_normalization_issues/{id}',
2829
normalizationContext: ['groups' => ['read']],
29-
provider: \ApiPlatform\Tests\Fixtures\TestBundle\State\DateTimeNormalizationIssueProvider::class
30+
provider: [self::class, 'provide']
3031
),
3132
]
3233
)]
@@ -38,4 +39,15 @@ public function __construct(
3839
public ?\DateTimeImmutable $updatedAt = null
3940
) {
4041
}
42+
43+
public static function provide(Operation $operation, array $uriVariables = [], array $context = []): self
44+
{
45+
$id = $uriVariables['id'] ?? 1;
46+
47+
return new self(
48+
id: (int) $id,
49+
name: 'Test Resource',
50+
updatedAt: new \DateTimeImmutable('2024-01-15 10:30:00')
51+
);
52+
}
4153
}

tests/Fixtures/TestBundle/State/DateTimeNormalizationIssueProvider.php

Lines changed: 0 additions & 35 deletions
This file was deleted.

tests/Functional/DateTimeNormalizerPriorityTest.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,9 @@ public function testDateTimeImmutableIsNormalizedCorrectly(): void
6262
$this->assertIsString($data['updatedAt']);
6363
$this->assertStringContainsString('2024-01-15', $data['updatedAt']);
6464

65-
// Verify it's a valid ISO 8601 datetime string
66-
$this->assertMatchesRegularExpression(
67-
'/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/',
68-
$data['updatedAt'],
69-
'updatedAt should be in ISO 8601 format (YYYY-MM-DDTHH:MM:SS)'
70-
);
65+
// Verify it's a valid ISO 8601 datetime string by parsing it
66+
$date = \DateTimeImmutable::createFromFormat(\DateTimeInterface::ATOM, $data['updatedAt']);
67+
$this->assertNotFalse($date, 'updatedAt should be in ISO 8601 format');
7168
}
7269

7370
/**
@@ -88,10 +85,10 @@ public function testDateTimeImmutableIsNormalizedCorrectlyInJson(): void
8885
$this->assertArrayHasKey('updatedAt', $data);
8986
$this->assertIsString($data['updatedAt']);
9087
$this->assertStringContainsString('2024-01-15', $data['updatedAt']);
91-
$this->assertMatchesRegularExpression(
92-
'/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/',
93-
$data['updatedAt']
94-
);
88+
89+
// Verify it's a valid ISO 8601 datetime string by parsing it
90+
$date = \DateTimeImmutable::createFromFormat(\DateTimeInterface::ATOM, $data['updatedAt']);
91+
$this->assertNotFalse($date, 'updatedAt should be in ISO 8601 format');
9592
}
9693

9794
/**
@@ -111,6 +108,10 @@ public function testDateTimeImmutableIsNormalizedCorrectlyInHal(): void
111108
$this->assertArrayHasKey('updatedAt', $data);
112109
$this->assertIsString($data['updatedAt']);
113110
$this->assertStringContainsString('2024-01-15', $data['updatedAt']);
111+
112+
// Verify it's a valid ISO 8601 datetime string by parsing it
113+
$date = \DateTimeImmutable::createFromFormat(\DateTimeInterface::ATOM, $data['updatedAt']);
114+
$this->assertNotFalse($date, 'updatedAt should be in ISO 8601 format');
114115
}
115116

116117
/**
@@ -132,5 +133,9 @@ public function testDateTimeImmutableIsNormalizedCorrectlyInJsonApi(): void
132133
$this->assertArrayHasKey('updatedAt', $data['data']['attributes']);
133134
$this->assertIsString($data['data']['attributes']['updatedAt']);
134135
$this->assertStringContainsString('2024-01-15', $data['data']['attributes']['updatedAt']);
136+
137+
// Verify it's a valid ISO 8601 datetime string by parsing it
138+
$date = \DateTimeImmutable::createFromFormat(\DateTimeInterface::ATOM, $data['data']['attributes']['updatedAt']);
139+
$this->assertNotFalse($date, 'updatedAt should be in ISO 8601 format');
135140
}
136141
}

0 commit comments

Comments
 (0)