From 019cd3eb28537f3801243bc050224d7b2ca2c8e3 Mon Sep 17 00:00:00 2001 From: ErwannRousseau Date: Sun, 15 Dec 2024 11:41:14 +0100 Subject: [PATCH] feat: use cents --- src/Controller/ProductController.php | 9 ++++----- src/Twig/Components/Price.php | 4 ++-- tests/Twig/Components/PriceTest.php | 6 +++--- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/Controller/ProductController.php b/src/Controller/ProductController.php index 3694ef4..099fc83 100644 --- a/src/Controller/ProductController.php +++ b/src/Controller/ProductController.php @@ -4,7 +4,6 @@ use Symfony\Bridge\Twig\Attribute\Template; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; -use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Attribute\Route; class ProductController extends AbstractController @@ -18,28 +17,28 @@ public function index(): array [ 'title' => 'Smartphone', 'description' => 'Latest model smartphone with advanced features', - 'price' => 699.99, + 'price' => 69999, 'imageUrl' => 'https://unsplash.it/640/425?random', 'linkUrl' => '#' ], [ 'title' => 'Laptop', 'description' => 'High performance laptop for professionals', - 'price' => 1299.99, + 'price' => 129999, 'imageUrl' => 'https://unsplash.it/640/425?random', 'linkUrl' => '#' ], [ 'title' => 'Smartwatch', 'description' => 'Stylish smartwatch with health tracking features', - 'price' => 199.99, + 'price' => 19999, 'imageUrl' => 'https://unsplash.it/640/425?random', 'linkUrl' => '#' ], [ 'title' => 'Headphones', 'description' => 'Noise-cancelling over-ear headphones', - 'price' => 299.99, + 'price' => 29999, 'imageUrl' => 'https://unsplash.it/640/425?random', 'linkUrl' => '#' ], diff --git a/src/Twig/Components/Price.php b/src/Twig/Components/Price.php index 63a2287..96226c5 100644 --- a/src/Twig/Components/Price.php +++ b/src/Twig/Components/Price.php @@ -10,12 +10,12 @@ #[AsTwigComponent('product:price')] final class Price { - public float $price; + public int $price; public string $locale = 'en_US'; public function formatPrice(): string { $formatter = new NumberFormatter($this->locale, NumberFormatter::CURRENCY); - return $formatter->formatCurrency($this->price, 'USD'); + return $formatter->formatCurrency($this->price / 100, 'USD'); } } diff --git a/tests/Twig/Components/PriceTest.php b/tests/Twig/Components/PriceTest.php index 52ef97a..e63c2da 100644 --- a/tests/Twig/Components/PriceTest.php +++ b/tests/Twig/Components/PriceTest.php @@ -14,11 +14,11 @@ public function testComponentMount(): void { $component = $this->mountTwigComponent( name: Price::class, - data: ['price' => 99.99, 'locale' => 'en_US'], + data: ['price' => 9999, 'locale' => 'en_US'], ); $this->assertInstanceOf(Price::class, $component); - $this->assertSame(99.99, $component->price); + $this->assertSame(9999, $component->price); $this->assertSame('en_US', $component->locale); } @@ -26,7 +26,7 @@ public function testComponentRenders(): void { $rendered = $this->renderTwigComponent( name: Price::class, - data: ['price' => 99.99, 'locale' => 'en_US'], + data: ['price' => 9999, 'locale' => 'en_US'], ); $this->assertStringContainsString('$99.99', $rendered);