MinorUnitsConverterTest.php in Commerce Core 8.2        
                          
                  
                        
  
  
  
  
  
File
  modules/price/tests/src/Unit/MinorUnitsConverterTest.php
  
    View source  
  <?php
namespace Drupal\Tests\commerce_price\Unit;
use CommerceGuys\Intl\Exception\UnknownCurrencyException;
use Drupal\commerce_price\Repository\CurrencyRepository;
use Drupal\commerce_price\MinorUnitsConverter;
use Drupal\commerce_price\Price;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Tests\UnitTestCase;
class MinorUnitsConverterTest extends UnitTestCase {
  
  protected $minorUnitsConverter;
  
  protected function setUp() : void {
    parent::setUp();
    $storage = $this
      ->prophesize(EntityStorageInterface::class);
    $entity_type_manager = $this
      ->prophesize(EntityTypeManagerInterface::class);
    $entity_type_manager
      ->getStorage('commerce_currency')
      ->willReturn($storage
      ->reveal());
    $this->minorUnitsConverter = new MinorUnitsConverter(new CurrencyRepository($entity_type_manager
      ->reveal()));
  }
  
  public function testUnknownCurrency() {
    $this
      ->expectException(UnknownCurrencyException::class);
    $this->minorUnitsConverter
      ->toMinorUnits(new Price('10', 'XYZ'));
  }
  
  public function testConversion(Price $price, $expected_amount) {
    $amount = $this->minorUnitsConverter
      ->toMinorUnits($price);
    $this
      ->assertEquals($expected_amount, $amount);
    $this
      ->assertTrue($this->minorUnitsConverter
      ->fromMinorUnits($amount, $price
      ->getCurrencyCode())
      ->equals($price));
  }
  
  public function currencyConversionData() {
    return [
      [
        new Price(10, 'EUR'),
        1000,
      ],
      [
        new Price(1.23, 'USD'),
        123,
      ],
      [
        new Price(0.99, 'NOK'),
        99,
      ],
      [
        new Price(99, 'JPY'),
        99,
      ],
      [
        new Price(99, 'KWD'),
        99000,
      ],
    ];
  }
}