-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d10ad3c
commit f0a1b14
Showing
4 changed files
with
130 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...et/StepupMiddleware/CommandHandlingBundle/Tests/Twig/BackwardsCompatibleExtensionTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright 2024 SURFnet bv | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
namespace Surfnet\StepupMiddleware\CommandHandlingBundle\Tests\Twig; | ||
|
||
use DateTime; | ||
use PHPUnit\Framework\TestCase; | ||
use Surfnet\StepupMiddleware\CommandHandlingBundle\Twig\BackwardsCompatibleExtension; | ||
use Twig\Environment; | ||
use Twig\Extra\Intl\IntlExtension; | ||
use Twig\Loader\ArrayLoader; | ||
|
||
/** | ||
* @requires extension intl | ||
*/ | ||
class BackwardsCompatibleExtensionTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider templateProvider | ||
*/ | ||
public function testLocalizedData(string $template, string $expected, string $locale): void | ||
{ | ||
$dateString = "2024-12-05 13:12:10"; | ||
$date = new DateTime($dateString); | ||
$twig = new Environment(new ArrayLoader(['template' => $template]), ['debug' => true, 'cache' => false, 'autoescape' => 'html', 'optimizations' => 0]); | ||
$twig->addExtension( new BackwardsCompatibleExtension(new IntlExtension())); | ||
|
||
$output = $twig->render('template', ['date' => $date, 'locale' => $locale]); | ||
$this->assertEquals($expected, $output); | ||
|
||
$output = $twig->render('template', ['date' => $dateString, 'locale' => $locale]); | ||
$this->assertEquals($expected, $output); | ||
} | ||
|
||
public function templateProvider(): array | ||
{ | ||
return [ | ||
'date en' => ["{{ date | localizeddate('full', 'none', locale) }}", 'Thursday, 5 December 2024', 'en_GB'], | ||
'date nl' => ["{{ date | localizeddate('full', 'none', locale) }}", 'donderdag 5 december 2024', 'nl_NL'], | ||
'date and time nl' => ["{{ date | localizeddate('full', 'medium', locale) }}", 'Thursday, 5 December 2024 at 13:12:10', 'en_GB'], | ||
'date and time en' => ["{{ date | localizeddate('full', 'medium', locale) }}", 'donderdag 5 december 2024 om 13:12:10', 'nl_NL'], | ||
'time nl' => ["{{ date | localizeddate('none', 'medium', locale) }}", '13:12:10', 'en_GB'], | ||
'time en' => ["{{ date | localizeddate('none', 'medium', locale) }}", '13:12:10', 'nl_NL'], | ||
]; | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
src/Surfnet/StepupMiddleware/CommandHandlingBundle/Twig/BackwardsCompatibleExtension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright 2024 SURFnet bv | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
namespace Surfnet\StepupMiddleware\CommandHandlingBundle\Twig; | ||
|
||
use DateTimeInterface; | ||
use Twig\Environment; | ||
use Twig\Extension\AbstractExtension; | ||
use Twig\Extra\Intl\IntlExtension; | ||
use Twig\TwigFilter; | ||
|
||
/** | ||
* This class is introduced to handle BC twig changes in email templates used in the institution configuration. | ||
* * We need to support both versions to support two versions of the codebase to support rolling updates. | ||
* * An idea was to move the email templates to disk but that would cost too much time and we still should support | ||
* * all historical events due to the nature of event sourcing. | ||
*/ | ||
class BackwardsCompatibleExtension extends AbstractExtension | ||
{ | ||
private IntlExtension $intlExtension; | ||
|
||
public function __construct(IntlExtension $intlExtension) | ||
{ | ||
$this->intlExtension = $intlExtension; | ||
} | ||
|
||
public function getFilters(): array | ||
{ | ||
return [ | ||
new TwigFilter('localizeddate', [$this, 'localizedDate'], ['needs_environment' => true]), | ||
]; | ||
} | ||
|
||
// localizeddate('full', 'none', locale) | ||
public function localizedDate( | ||
Environment $env, | ||
DateTimeInterface|string|null $date, | ||
?string $dateFormat = 'medium', | ||
?string $timeFormat = 'medium', | ||
string $locale = null | ||
): string { | ||
return $this->intlExtension->formatDateTime($env, $date, $dateFormat, $timeFormat, locale: $locale); | ||
} | ||
} |