You are here

public function BundlePriceResolverTest::testResolves in Commerce Product Bundle 8

Tests price revolving.

::covers resolve.

File

tests/src/Kernel/Resolver/BundlePriceResolverTest.php, line 24

Class

BundlePriceResolverTest
Tests the bundle price resolver.

Namespace

Drupal\Tests\commerce_product_bundle\Kernel\Resolver

Code

public function testResolves() {
  $store = $this
    ->prophesize('\\Drupal\\commerce_store\\Entity\\StoreInterface');
  $store
    ->getDefaultCurrencyCode()
    ->willReturn('EUR');
  $store = $store
    ->reveal();
  $currentStore = $this
    ->prophesize('Drupal\\commerce_store\\CurrentStoreInterface');
  $currentStore
    ->getStore()
    ->willReturn($store);
  $notOurEntity = $this
    ->prophesize('Drupal\\commerce_product\\Entity\\ProductVariation');
  $context = new Context($this->user, $store);
  $resolver = new BundlePriceResolver($currentStore
    ->reveal());
  self::assertNull($resolver
    ->resolve($notOurEntity
    ->reveal(), 1, $context));
  $bundle = $this
    ->prophesize('Drupal\\commerce_product_bundle\\Entity\\BundleInterface');
  $bundle
    ->getPrice()
    ->willReturn(new Price('0.00', 'USD'))
    ->shouldBeCalledTimes(1);
  self::assertEquals(new Price('0.00', 'USD'), $resolver
    ->resolve($bundle
    ->reveal(), 1, $context));

  // Wether the getPrice() method gets called.
  $bundle
    ->checkProphecyMethodsPredictions();
  $bundle = $this
    ->prophesize('Drupal\\commerce_product_bundle\\Entity\\BundleInterface');
  $bundle
    ->getPrice()
    ->willReturn(new Price('5.55', 'USD'))
    ->shouldBeCalledTimes(1);
  self::assertEquals(new Price('5.55', 'USD'), $resolver
    ->resolve($bundle
    ->reveal(), 1, $context));
  $bundle = $this
    ->prophesize('Drupal\\commerce_product_bundle\\Entity\\BundleInterface');
  $bundle
    ->getPrice()
    ->willReturn(NULL);
  $bundleItem = $this
    ->prophesize('Drupal\\commerce_product_bundle\\Entity\\BundleItemInterface');
  $bundleItem
    ->getUnitPrice()
    ->willReturn(new Price('11.11', 'EUR'))
    ->shouldBeCalledTimes(5);
  $items = array_fill(0, 5, $bundleItem
    ->reveal());
  $bundle
    ->getBundleItems()
    ->willReturn($items)
    ->shouldBeCalled();
  self::assertEquals(new Price('55.55', 'EUR'), $resolver
    ->resolve($bundle
    ->reveal(), 1, $context));
  $bundle
    ->checkProphecyMethodsPredictions();
}