class AvailabilityManagerTest in Commerce Core 8.2
Same name in this branch
- 8.2 tests/src/Unit/AvailabilityManagerTest.php \Drupal\Tests\commerce\Unit\AvailabilityManagerTest
- 8.2 modules/order/tests/src/Unit/AvailabilityManagerTest.php \Drupal\Tests\commerce_order\Unit\AvailabilityManagerTest
@coversDefaultClass \Drupal\commerce\AvailabilityManager @group commerce
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\commerce\Unit\AvailabilityManagerTest
Expanded class hierarchy of AvailabilityManagerTest
File
- tests/
src/ Unit/ AvailabilityManagerTest.php, line 13
Namespace
Drupal\Tests\commerce\UnitView source
class AvailabilityManagerTest extends UnitTestCase {
/**
* The availability manager.
*
* @var \Drupal\commerce\AvailabilityManager
*/
protected $availabilityManager;
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
$this->availabilityManager = new AvailabilityManager();
}
/**
* ::covers addChecker
* ::covers getCheckers
* ::covers check.
*
* @group legacy
*/
public function testCheck() {
$mock_builder = $this
->getMockBuilder('Drupal\\commerce\\AvailabilityCheckerInterface')
->disableOriginalConstructor();
$entity = $this
->createMock('Drupal\\commerce_product\\Entity\\ProductVariationInterface');
$first_checker = $mock_builder
->getMock();
$first_checker
->expects($this
->any())
->method('applies')
->with($entity)
->willReturn(TRUE);
$first_checker
->expects($this
->any())
->method('check')
->with($entity, 1)
->willReturn(NULL);
$second_checker = $mock_builder
->getMock();
$second_checker
->expects($this
->any())
->method('applies')
->with($entity)
->willReturn(TRUE);
$second_checker
->expects($this
->any())
->method('check')
->with($entity, 1)
->willReturn(TRUE);
$third_checker = $mock_builder
->getMock();
$third_checker
->expects($this
->any())
->method('applies')
->with($entity)
->willReturn(FALSE);
$third_checker
->expects($this
->any())
->method('check')
->with($entity, 1)
->willReturn(FALSE);
$fourth_checker = $mock_builder
->getMock();
$fourth_checker
->expects($this
->any())
->method('applies')
->with($entity)
->willReturn(TRUE);
$fourth_checker
->expects($this
->any())
->method('check')
->with($entity, 1)
->willReturn(FALSE);
$user = $this
->createMock('\\Drupal\\Core\\Session\\AccountInterface');
$store = $this
->createMock('Drupal\\commerce_store\\Entity\\StoreInterface');
$context = new Context($user, $store);
$this->availabilityManager
->addChecker($first_checker);
$result = $this->availabilityManager
->check($entity, 1, $context);
$this
->assertNotEmpty($result, 'The checked entity is available when a checker returns NULL.');
$this->availabilityManager
->addChecker($second_checker);
$result = $this->availabilityManager
->check($entity, 1, $context);
$this
->assertNotEmpty($result, 'The checked entity is available when no checkers return FALSE.');
$this->availabilityManager
->addChecker($third_checker);
$result = $this->availabilityManager
->check($entity, 1, $context);
$this
->assertNotEmpty($result, 'The checked entity is available when a checker that would return FALSE does not apply.');
$this->availabilityManager
->addChecker($fourth_checker);
$result = $this->availabilityManager
->check($entity, 1, $context);
$this
->assertEmpty($result, 'The checked entity is not available when a checker that returns FALSE applies');
$expectedCheckers = [
$first_checker,
$second_checker,
$third_checker,
$fourth_checker,
];
$checkers = $this->availabilityManager
->getCheckers();
$this
->assertEquals($expectedCheckers, $checkers, 'The manager has the expected checkers');
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AvailabilityManagerTest:: |
protected | property | The availability manager. | |
AvailabilityManagerTest:: |
protected | function |
Overrides UnitTestCase:: |
|
AvailabilityManagerTest:: |
public | function | ::covers addChecker ::covers getCheckers ::covers check. | |
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. | |
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. |