Skip to content

Commit

Permalink
feat: use cents
Browse files Browse the repository at this point in the history
  • Loading branch information
ErwannRousseau committed Dec 15, 2024
1 parent 40dd6b2 commit 019cd3e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 10 deletions.
9 changes: 4 additions & 5 deletions src/Controller/ProductController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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' => '#'
],
Expand Down
4 changes: 2 additions & 2 deletions src/Twig/Components/Price.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
6 changes: 3 additions & 3 deletions tests/Twig/Components/PriceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ 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);
}

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);
Expand Down

0 comments on commit 019cd3e

Please sign in to comment.