class PurchasableEntityTypeRepositoryTest in Commerce Core 8.2
@coversDefaultClass \Drupal\commerce\PurchasableEntityTypeRepository @group commerce
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\commerce\Unit\PurchasableEntityTypeRepositoryTest
Expanded class hierarchy of PurchasableEntityTypeRepositoryTest
File
- tests/
src/ Unit/ PurchasableEntityTypeRepositoryTest.php, line 15
Namespace
Drupal\Tests\commerce\UnitView 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
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
PurchasableEntityTypeRepositoryTest:: |
private | function | Creates mocked entity type definitions. | |
PurchasableEntityTypeRepositoryTest:: |
public | function | @covers ::getDefaultPurchasableEntityType | |
PurchasableEntityTypeRepositoryTest:: |
public | function | @covers ::getPurchasableEntityTypeLabels | |
PurchasableEntityTypeRepositoryTest:: |
public | function | @covers ::getPurchasableEntityTypes | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. | |
UnitTestCase:: |
protected | function | 340 |