You are here

class AvailabilityManagerTest in Commerce Core 8.2

Same name in this branch
  1. 8.2 tests/src/Unit/AvailabilityManagerTest.php \Drupal\Tests\commerce\Unit\AvailabilityManagerTest
  2. 8.2 modules/order/tests/src/Unit/AvailabilityManagerTest.php \Drupal\Tests\commerce_order\Unit\AvailabilityManagerTest

@coversDefaultClass \Drupal\commerce_order\AvailabilityManager @group commerce

Hierarchy

Expanded class hierarchy of AvailabilityManagerTest

File

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

Namespace

Drupal\Tests\commerce_order\Unit
View source
class AvailabilityManagerTest extends UnitTestCase {

  /**
   * The availability manager.
   *
   * @var \Drupal\commerce_order\AvailabilityManager
   */
  protected $availabilityManager;

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this->availabilityManager = new AvailabilityManager();
  }

  /**
   * ::covers addChecker
   * ::covers check.
   */
  public function testCheck() {
    $mock_builder = $this
      ->getMockBuilder(AvailabilityCheckerInterface::class)
      ->disableOriginalConstructor();
    $order_item = $this
      ->createMock(OrderItemInterface::class);
    $first_checker = $mock_builder
      ->getMock();
    $first_checker
      ->expects($this
      ->any())
      ->method('applies')
      ->with($order_item)
      ->willReturn(TRUE);
    $first_checker
      ->expects($this
      ->any())
      ->method('check')
      ->with($order_item)
      ->willReturn(NULL);
    $second_checker = $mock_builder
      ->getMock();
    $second_checker
      ->expects($this
      ->any())
      ->method('applies')
      ->with($order_item)
      ->willReturn(TRUE);
    $second_checker
      ->expects($this
      ->any())
      ->method('check')
      ->with($order_item)
      ->willReturn(AvailabilityResult::neutral());
    $third_checker = $mock_builder
      ->getMock();
    $third_checker
      ->expects($this
      ->any())
      ->method('applies')
      ->with($order_item)
      ->willReturn(FALSE);
    $third_checker
      ->expects($this
      ->any())
      ->method('check')
      ->with($order_item)
      ->willReturn(AvailabilityResult::unavailable());
    $fourth_checker = $mock_builder
      ->getMock();
    $fourth_checker
      ->expects($this
      ->any())
      ->method('applies')
      ->with($order_item)
      ->willReturn(TRUE);
    $fourth_checker
      ->expects($this
      ->any())
      ->method('check')
      ->with($order_item)
      ->willReturn(AvailabilityResult::unavailable('The product is not available'));
    $user = $this
      ->createMock(AccountInterface::class);
    $store = $this
      ->createMock(StoreInterface::class);
    $context = new Context($user, $store);

    // Test the new availability checkers first.
    $this->availabilityManager
      ->addChecker($first_checker);
    $result = $this->availabilityManager
      ->check($order_item, $context);
    $this
      ->assertEquals(AvailabilityResult::neutral(), $result, 'The checked order item is available when a checker returns NULL.');
    $this->availabilityManager
      ->addChecker($second_checker);
    $result = $this->availabilityManager
      ->check($order_item, $context);
    $this
      ->assertEquals(AvailabilityResult::neutral(), $result, 'The checked order item is available when a checker returns a "neutral" availability result.');
    $this->availabilityManager
      ->addChecker($third_checker);
    $result = $this->availabilityManager
      ->check($order_item, $context);
    $this
      ->assertEquals(AvailabilityResult::neutral(), $result, 'The checked order item is available when a checker that would return an "unavailable" availability result does not apply.');
    $this->availabilityManager
      ->addChecker($fourth_checker);
    $result = $this->availabilityManager
      ->check($order_item, $context);
    $this
      ->assertEquals(AvailabilityResult::unavailable('The product is not available'), $result, 'The checked order item is not available when a checker returning an"unavailable" availability result applies.');
  }

  /**
   * ::covers addChecker
   * ::covers addLegacyChecker
   * ::covers check.
   *
   * @group legacy
   */
  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.');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AvailabilityManagerTest::$availabilityManager protected property The availability manager.
AvailabilityManagerTest::setUp protected function Overrides UnitTestCase::setUp
AvailabilityManagerTest::testCheck public function ::covers addChecker ::covers check.
AvailabilityManagerTest::testLegacyCheckers public function ::covers addChecker ::covers addLegacyChecker ::covers check.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.