Skip to content

Commit 3e34114

Browse files
authored
Merge pull request #96 from itk-dev/feature/easyadmin-form-addons
Show the Service Agreements amount in Danish kroner
2 parents 77699ad + 630b266 commit 3e34114

3 files changed

Lines changed: 97 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
- [#96](https://github.com/itk-dev/devops_itksites/pull/96)
11+
Show the Service Agreements monthly price as Danish kroner,
12+
`12.500,50 kr.`, on index and detail
1013
- [#95](https://github.com/itk-dev/devops_itksites/pull/95)
1114
Update `vincentlanglet/twig-cs-fixer` to 4.0. Every other dependency is
1215
already at its latest minor; the remaining majors are held back by their

src/Controller/Admin/SecurityContractCrudController.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,12 @@ public function configureFields(string $pageName): iterable
7272
yield TextField::new('clientContactEmail')->hideOnIndex();
7373

7474
yield FormField::addFieldset('Budget');
75-
yield NumberField::new('monthlyPrice')->setTextAlign('right')->setColumns(6);
75+
// The amount is Danish kroner, which the admin never said anywhere.
76+
// 5.5's prepend()/append() addons would be the way to show a unit inside
77+
// an input, but they render on form pages only and this CRUD disables
78+
// NEW and EDIT (see configureActions), so index and detail are the only
79+
// pages it has. Hence formatting instead of an addon.
80+
yield NumberField::new('monthlyPrice')->setTextAlign('right')->setColumns(6)->formatValue(self::formatKroner(...));
7681

7782
yield FormField::addFieldset('Infrastructure');
7883
yield BooleanField::new('dedicatedServer')->renderAsSwitch(false)->hideOnIndex();
@@ -83,6 +88,29 @@ public function configureFields(string $pageName): iterable
8388
yield DateField::new('validTo')->setColumns(6);
8489
}
8590

91+
/**
92+
* An amount as Danish kroner: 12.500,50 kr.
93+
*
94+
* Through Intl rather than by pasting a suffix on, so the grouping and the
95+
* decimal separator are Danish too. The application locale is `en`, which
96+
* would otherwise render 12,500.5 with no currency at all.
97+
*/
98+
public static function formatKroner(?float $value): ?string
99+
{
100+
if (null === $value) {
101+
return null;
102+
}
103+
104+
return (new \NumberFormatter('da_DK', \NumberFormatter::CURRENCY))->formatCurrency($value, 'DKK') ?: null;
105+
}
106+
107+
/**
108+
* The attribute is what makes this method reachable as a CRUD action.
109+
*
110+
* Without it EasyAdmin throws while rendering the "Sync all" button, which
111+
* took the whole index page with it — see the "Custom CRUD Actions" section
112+
* of the bundle's UPGRADE.md.
113+
*/
86114
#[AdminRoute]
87115
public function syncAll(): RedirectResponse
88116
{
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Tests\Controller\Admin;
6+
7+
use App\Controller\Admin\SecurityContractCrudController;
8+
use App\Entity\Project;
9+
use App\Entity\SecurityContract;
10+
use App\Entity\User;
11+
use Doctrine\ORM\EntityManagerInterface;
12+
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
13+
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
14+
use Hautelook\AliceBundle\PhpUnit\RefreshDatabaseTrait;
15+
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
16+
17+
/**
18+
* Service agreement amounts are Danish kroner, and the admin has to say so.
19+
*
20+
* The values come from Economics with no unit attached, and the CRUD has no
21+
* form pages — NEW and EDIT are disabled — so index and detail are the only
22+
* places a reader ever sees them.
23+
*/
24+
class SecurityContractCurrencyTest extends WebTestCase
25+
{
26+
use RefreshDatabaseTrait;
27+
28+
public function testAmountsRenderAsDanishKroner(): void
29+
{
30+
$client = static::createClient();
31+
32+
$entityManager = static::getContainer()->get(EntityManagerInterface::class);
33+
$client->loginUser($entityManager->getRepository(User::class)->findOneBy([]));
34+
35+
$project = new Project();
36+
$project->setEconomicsId(4711);
37+
$project->setName('Kroner probe');
38+
39+
$contract = new SecurityContract();
40+
$contract->setEconomicsId(4711);
41+
$contract->setProject($project);
42+
$contract->setMonthlyPrice(12500.5);
43+
44+
$entityManager->persist($project);
45+
$entityManager->persist($contract);
46+
$entityManager->flush();
47+
48+
$url = static::getContainer()->get(AdminUrlGenerator::class)
49+
->setController(SecurityContractCrudController::class)
50+
->setAction(Crud::PAGE_INDEX)
51+
->generateUrl();
52+
53+
$client->request('GET', $url);
54+
55+
$this->assertResponseIsSuccessful();
56+
57+
$content = (string) $client->getResponse()->getContent();
58+
59+
// Danish grouping and separator, not the application locale's 12,500.5.
60+
// Amount and unit are asserted apart because Intl joins them with a
61+
// non-breaking space.
62+
$this->assertStringContainsString('12.500,50', $content);
63+
$this->assertStringContainsString('kr.', $content);
64+
}
65+
}

0 commit comments

Comments
 (0)