public function FormattedPriceTest::testFormattedPrice in Commerce Core 8.2
Test the data type through the price field property.
File
- modules/
price/ tests/ src/ Kernel/ FormattedPriceTest.php, line 44
Class
- FormattedPriceTest
- Tests the formatted price data type for price fields.
Namespace
Drupal\Tests\commerce_price\KernelCode
public function testFormattedPrice() {
/** @var \Drupal\commerce_product\Entity\ProductVariationInterface $variation1 */
$variation1 = ProductVariation::create([
'type' => 'default',
'sku' => strtolower($this
->randomMachineName()),
'price' => new Price('12.00', 'USD'),
]);
$variation1
->save();
/** @var \Drupal\commerce_product\Entity\ProductVariationInterface $variation2 */
$variation2 = ProductVariation::create([
'type' => 'default',
'sku' => strtolower($this
->randomMachineName()),
'price' => new Price('13.00', 'EUR'),
]);
$variation2
->save();
$price_item = $variation1
->get('price')
->first();
$this
->assertInstanceOf(PriceItem::class, $price_item);
$this
->assertEquals('$12.00', $price_item
->get('formatted')
->getValue());
$price_item = $variation2
->get('price')
->first();
$this
->assertInstanceOf(PriceItem::class, $price_item);
$this
->assertEquals('€13.00', $price_item
->get('formatted')
->getValue());
}