You are here

public function AvailabilityManagerTest::testLegacyCheckers in Commerce Core 8.2

::covers addChecker ::covers addLegacyChecker ::covers check.

@group legacy

File

modules/order/tests/src/Unit/AvailabilityManagerTest.php, line 116

Class

AvailabilityManagerTest
@coversDefaultClass \Drupal\commerce_order\AvailabilityManager @group commerce

Namespace

Drupal\Tests\commerce_order\Unit

Code

public function testLegacyCheckers() {
  $order_item = $this
    ->createMock(OrderItemInterface::class);
  $product_variation = $this
    ->createMock(ProductVariationInterface::class);
  $order_item
    ->method('getPurchasedEntity')
    ->willReturn($product_variation);
  $order_item
    ->method('getQuantity')
    ->willReturn(1);
  $legacy_mock_builder = $this
    ->getMockBuilder(LegacyAvailabilityCheckerInterface::class)
    ->disableOriginalConstructor();
  $first_legacy_checker = $legacy_mock_builder
    ->getMock();
  $first_legacy_checker
    ->expects($this
    ->any())
    ->method('applies')
    ->with($product_variation)
    ->willReturn(TRUE);
  $first_legacy_checker
    ->expects($this
    ->any())
    ->method('check')
    ->with($product_variation, 1)
    ->willReturn(NULL);
  $second_legacy_checker = $legacy_mock_builder
    ->getMock();
  $second_legacy_checker
    ->expects($this
    ->any())
    ->method('applies')
    ->with($product_variation)
    ->willReturn(TRUE);
  $second_legacy_checker
    ->expects($this
    ->any())
    ->method('check')
    ->with($product_variation, 1)
    ->willReturn(TRUE);
  $third_legacy_checker = $legacy_mock_builder
    ->getMock();
  $third_legacy_checker
    ->expects($this
    ->any())
    ->method('applies')
    ->with($product_variation)
    ->willReturn(TRUE);
  $third_legacy_checker
    ->expects($this
    ->any())
    ->method('check')
    ->with($product_variation, 1)
    ->willReturn(FALSE);
  $user = $this
    ->createMock(AccountInterface::class);
  $store = $this
    ->createMock(StoreInterface::class);
  $context = new Context($user, $store);
  $this->availabilityManager
    ->addLegacyChecker($first_legacy_checker);
  $result = $this->availabilityManager
    ->check($order_item, $context);
  $this
    ->assertEquals(AvailabilityResult::neutral(), $result, 'The checked order item is available when a legacy checker returns NULL.');
  $this->availabilityManager
    ->addLegacyChecker($second_legacy_checker);
  $result = $this->availabilityManager
    ->check($order_item, $context);
  $this
    ->assertEquals(AvailabilityResult::neutral(), $result, 'The checked order item is available when a legacy checker returns TRUE.');
  $this->availabilityManager
    ->addLegacyChecker($third_legacy_checker);
  $result = $this->availabilityManager
    ->check($order_item, $context);
  $this
    ->assertEquals(AvailabilityResult::unavailable(), $result, 'The checked order item is unavailable when a legacy checker returns FALSE.');

  // Test the integration with both legacy checkers and new checkers.
  $mock_builder = $this
    ->getMockBuilder(AvailabilityCheckerInterface::class)
    ->disableOriginalConstructor();
  $checker = $mock_builder
    ->getMock();
  $checker
    ->expects($this
    ->any())
    ->method('applies')
    ->with($order_item)
    ->willReturn(TRUE);
  $checker
    ->expects($this
    ->any())
    ->method('check')
    ->with($order_item)
    ->willReturn(AvailabilityResult::unavailable('The product is not available'));
  $this->availabilityManager
    ->addChecker($checker);
  $result = $this->availabilityManager
    ->check($order_item, $context);
  $this
    ->assertEquals(AvailabilityResult::unavailable('The product is not available'), $result, 'The checked order item is unavailable when a new checker returns an "unavailable" availability result.');
}