You are here

class PurchasableEntityTypeRepositoryTest in Commerce Core 8.2

@coversDefaultClass \Drupal\commerce\PurchasableEntityTypeRepository @group commerce

Hierarchy

Expanded class hierarchy of PurchasableEntityTypeRepositoryTest

File

tests/src/Unit/PurchasableEntityTypeRepositoryTest.php, line 15

Namespace

Drupal\Tests\commerce\Unit
View source
class PurchasableEntityTypeRepositoryTest extends UnitTestCase {

  /**
   * @covers ::getPurchasableEntityTypes
   */
  public function testGetPurchasableEntityTypes() {
    $node_entity_type = $this
      ->prophesize(EntityTypeInterface::class);
    $node_entity_type
      ->entityClassImplements(PurchasableEntityInterface::class)
      ->willReturn(FALSE);
    $product_entity_type = $this
      ->prophesize(EntityTypeInterface::class);
    $product_entity_type
      ->entityClassImplements(PurchasableEntityInterface::class)
      ->willReturn(FALSE);
    $product_variation_entity_type = $this
      ->prophesize(EntityTypeInterface::class);
    $product_variation_entity_type
      ->entityClassImplements(PurchasableEntityInterface::class)
      ->willReturn(TRUE);
    $etm = $this
      ->prophesize(EntityTypeManagerInterface::class);
    $etm
      ->getDefinitions()
      ->willReturn($this
      ->createMockedDefinitions([
      'node' => [
        'is_purchasable' => FALSE,
        'label' => 'Node',
      ],
      'commerce_product' => [
        'is_purchasable' => FALSE,
        'label' => 'Product',
      ],
      'commerce_product_variation' => [
        'is_purchasable' => TRUE,
        'label' => 'Product variation',
      ],
    ]));
    $sut = new PurchasableEntityTypeRepository($etm
      ->reveal());
    $purchasable_entity_types = $sut
      ->getPurchasableEntityTypes();
    $this
      ->assertCount(1, $purchasable_entity_types);
    $this
      ->assertEquals([
      'commerce_product_variation',
    ], array_keys($purchasable_entity_types));
    $widget_entity_type = $this
      ->prophesize(EntityTypeInterface::class);
    $widget_entity_type
      ->entityClassImplements(PurchasableEntityInterface::class)
      ->willReturn(TRUE);
    $etm
      ->getDefinitions()
      ->willReturn($this
      ->createMockedDefinitions([
      'node' => [
        'is_purchasable' => FALSE,
        'label' => 'Node',
      ],
      'commerce_product' => [
        'is_purchasable' => FALSE,
        'label' => 'Product',
      ],
      'commerce_product_variation' => [
        'is_purchasable' => TRUE,
        'label' => 'Product variation',
      ],
      'widget' => [
        'is_purchasable' => TRUE,
        'label' => 'Widget',
      ],
    ]));
    $purchasable_entity_types = $sut
      ->getPurchasableEntityTypes();
    $this
      ->assertCount(2, $purchasable_entity_types);
    $this
      ->assertEquals([
      'commerce_product_variation',
      'widget',
    ], array_keys($purchasable_entity_types));
  }

  /**
   * @covers ::getPurchasableEntityTypeLabels
   */
  public function testGetPurchasableEntityTypeLabels() {
    $etm = $this
      ->prophesize(EntityTypeManagerInterface::class);
    $etm
      ->getDefinitions()
      ->willReturn($this
      ->createMockedDefinitions([
      'node' => [
        'is_purchasable' => FALSE,
        'label' => 'Node',
      ],
      'commerce_product' => [
        'is_purchasable' => FALSE,
        'label' => 'Product',
      ],
      'commerce_product_variation' => [
        'is_purchasable' => TRUE,
        'label' => 'Product variation',
      ],
      'widget' => [
        'is_purchasable' => TRUE,
        'label' => 'Widget',
      ],
    ]));
    $sut = new PurchasableEntityTypeRepository($etm
      ->reveal());
    $this
      ->assertEquals([
      'commerce_product_variation' => 'Product variation',
      'widget' => 'Widget',
    ], $sut
      ->getPurchasableEntityTypeLabels());
  }

  /**
   * @covers ::getDefaultPurchasableEntityType
   */
  public function testGetDefaultPurchasableEntityType() {
    $etm = $this
      ->prophesize(EntityTypeManagerInterface::class);
    $etm
      ->getDefinitions()
      ->willReturn($this
      ->createMockedDefinitions([
      'node' => [
        'is_purchasable' => FALSE,
        'label' => 'Node',
      ],
      'commerce_product' => [
        'is_purchasable' => FALSE,
        'label' => 'Product',
      ],
      'commerce_product_variation' => [
        'is_purchasable' => TRUE,
        'label' => 'Product variation',
      ],
      'widget' => [
        'is_purchasable' => TRUE,
        'label' => 'Widget',
      ],
    ]));
    $sut = new PurchasableEntityTypeRepository($etm
      ->reveal());
    $default = $sut
      ->getDefaultPurchasableEntityType();
    $this
      ->assertEquals($default
      ->getLabel(), 'Product variation');
    $etm
      ->getDefinitions()
      ->willReturn($this
      ->createMockedDefinitions([
      'node' => [
        'is_purchasable' => FALSE,
        'label' => 'Node',
      ],
      'widget' => [
        'is_purchasable' => TRUE,
        'label' => 'Widget',
      ],
    ]));
    $sut = new PurchasableEntityTypeRepository($etm
      ->reveal());
    $default = $sut
      ->getDefaultPurchasableEntityType();
    $this
      ->assertEquals($default
      ->getLabel(), 'Widget');
  }

  /**
   * Creates mocked entity type definitions.
   *
   * @param array $definition_items
   *   The definition items.
   *
   * @return array
   *   The mocked definitions.
   */
  private function createMockedDefinitions(array $definition_items) : array {
    $definitions = [];
    foreach ($definition_items as $entity_type_id => $data) {
      $mock = $this
        ->prophesize(EntityTypeInterface::class);
      $mock
        ->entityClassImplements(PurchasableEntityInterface::class)
        ->willReturn($data['is_purchasable']);
      $mock
        ->getLabel()
        ->willReturn($data['label']);
      $definitions[$entity_type_id] = $mock
        ->reveal();
    }
    return $definitions;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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.
PurchasableEntityTypeRepositoryTest::createMockedDefinitions private function Creates mocked entity type definitions.
PurchasableEntityTypeRepositoryTest::testGetDefaultPurchasableEntityType public function @covers ::getDefaultPurchasableEntityType
PurchasableEntityTypeRepositoryTest::testGetPurchasableEntityTypeLabels public function @covers ::getPurchasableEntityTypeLabels
PurchasableEntityTypeRepositoryTest::testGetPurchasableEntityTypes public function @covers ::getPurchasableEntityTypes
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.
UnitTestCase::setUp protected function 340