You are here

class UnsetLineItemTest in Payment 8.2

@coversDefaultClass \Drupal\payment\Plugin\Action\UnsetLineItem

@group Payment

Hierarchy

Expanded class hierarchy of UnsetLineItemTest

File

tests/src/Unit/Plugin/Action/UnsetLineItemTest.php, line 18

Namespace

Drupal\Tests\payment\Unit\Plugin\Action
View source
class UnsetLineItemTest extends UnitTestCase {

  /**
   * The string translator.
   *
   * @var \Drupal\Core\StringTranslation\TranslationInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $stringTranslation;

  /**
   * The class under test.
   *
   * @var \Drupal\payment\Plugin\Action\UnsetLineItem
   */
  protected $sut;

  /**
   * {@inheritdoc}
   */
  public function setUp() : void {
    $this->stringTranslation = $this
      ->getStringTranslationStub();
    $configuration = [];
    $plugin_definition = [];
    $plugin_id = $this
      ->randomMachineName();
    $this->sut = new UnsetLineItem($configuration, $plugin_id, $plugin_definition, $this->stringTranslation);
  }

  /**
   * @covers ::create
   * @covers ::__construct
   */
  function testCreate() {
    $container = $this
      ->createMock(ContainerInterface::class);
    $map = array(
      array(
        'string_translation',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->stringTranslation,
      ),
    );
    $container
      ->expects($this
      ->any())
      ->method('get')
      ->willReturnMap($map);
    $configuration = [];
    $plugin_definition = [];
    $plugin_id = $this
      ->randomMachineName();
    $sut = UnsetLineItem::create($container, $configuration, $plugin_id, $plugin_definition);
    $this
      ->assertInstanceOf(UnsetLineItem::class, $sut);
  }

  /**
   * @covers ::defaultConfiguration
   */
  public function testDefaultConfiguration() {
    $configuration = $this->sut
      ->defaultConfiguration();
    $this
      ->assertIsArray($configuration);
    $this
      ->assertArrayHasKey('line_item_name', $configuration);
  }

  /**
   * @covers ::buildConfigurationForm
   */
  public function testBuildConfigurationForm() {
    $form = [];
    $form_state = $this
      ->createMock(FormStateInterface::class);
    $form = $this->sut
      ->buildConfigurationForm($form, $form_state);
    $this
      ->assertIsArray($form);
    $this
      ->assertArrayHasKey('line_item_name', $form);
  }

  /**
   * @covers ::submitConfigurationForm
   * @depends testBuildConfigurationForm
   */
  public function testSubmitConfigurationForm() {
    $name = $this
      ->randomMachineName();
    $form = [];
    $form_state = $this
      ->createMock(FormStateInterface::class);
    $form_state
      ->expects($this
      ->atLeastOnce())
      ->method('getValues')
      ->willReturn(array(
      'line_item_name' => $name,
    ));
    $this->sut
      ->submitConfigurationForm($form, $form_state);
    $configuration = $this->sut
      ->getConfiguration();
    $this
      ->assertSame($name, $configuration['line_item_name']);
  }

  /**
   * @covers ::execute
   */
  public function testExecute() {
    $name = $this
      ->randomMachineName();
    $payment = $this
      ->createMock(PaymentInterface::class);
    $payment
      ->expects($this
      ->once())
      ->method('unsetLineItem')
      ->with($name);
    $this->sut
      ->setConfiguration(array(
      'line_item_name' => $name,
    ));
    $this->sut
      ->execute($payment);
  }

  /**
   * @covers ::access
   */
  public function testAccessWithPaymentAsObject() {
    $account = $this
      ->createMock(AccountInterface::class);
    $access_result = new AccessResultAllowed();
    $payment = $this
      ->createMock(PaymentInterface::class);
    $payment
      ->expects($this
      ->atLeastOnce())
      ->method('access')
      ->with('update', $account, TRUE)
      ->willReturn($access_result);
    $this
      ->assertSame($access_result, $this->sut
      ->access($payment, $account, TRUE));
  }

  /**
   * @covers ::access
   */
  public function testAccessWithPaymentAsBoolean() {
    $account = $this
      ->createMock(AccountInterface::class);
    $payment = $this
      ->createMock(PaymentInterface::class);
    $payment
      ->expects($this
      ->atLeastOnce())
      ->method('access')
      ->with('update', $account)
      ->willReturn(TRUE);
    $this
      ->assertTrue($this->sut
      ->access($payment, $account));
  }

  /**
   * @covers ::access
   */
  public function testAccessWithoutPaymentAsObject() {
    $account = $this
      ->createMock(AccountInterface::class);
    $access_result = $this->sut
      ->access(NULL, $account, TRUE);
    $this
      ->assertFalse($access_result
      ->isAllowed());
  }

  /**
   * @covers ::access
   */
  public function testAccessWithoutPaymentAsBoolean() {
    $account = $this
      ->createMock(AccountInterface::class);
    $access_result = $this->sut
      ->access(NULL, $account);
    $this
      ->assertFalse($access_result);
  }

}

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.
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.
UnsetLineItemTest::$stringTranslation protected property The string translator.
UnsetLineItemTest::$sut protected property The class under test.
UnsetLineItemTest::setUp public function Overrides UnitTestCase::setUp
UnsetLineItemTest::testAccessWithoutPaymentAsBoolean public function @covers ::access
UnsetLineItemTest::testAccessWithoutPaymentAsObject public function @covers ::access
UnsetLineItemTest::testAccessWithPaymentAsBoolean public function @covers ::access
UnsetLineItemTest::testAccessWithPaymentAsObject public function @covers ::access
UnsetLineItemTest::testBuildConfigurationForm public function @covers ::buildConfigurationForm
UnsetLineItemTest::testCreate function @covers ::create @covers ::__construct
UnsetLineItemTest::testDefaultConfiguration public function @covers ::defaultConfiguration
UnsetLineItemTest::testExecute public function @covers ::execute
UnsetLineItemTest::testSubmitConfigurationForm public function @covers ::submitConfigurationForm @depends testBuildConfigurationForm