You are here

class PaymentExecutionPaymentMethodManagerTest in Payment 8.2

@coversDefaultClass \Drupal\payment\Plugin\Payment\Method\PaymentExecutionPaymentMethodManager

@group Payment

Hierarchy

Expanded class hierarchy of PaymentExecutionPaymentMethodManagerTest

File

tests/src/Unit/Plugin/Payment/Method/PaymentExecutionPaymentMethodManagerTest.php, line 20

Namespace

Drupal\Tests\payment\Unit\Plugin\Payment\Method
View source
class PaymentExecutionPaymentMethodManagerTest extends UnitTestCase {

  /**
   * The account.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $account;

  /**
   * The payment to filter methods by.
   *
   * @var \Drupal\payment\Entity\PaymentInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $payment;

  /**
   * The original payment method manager.
   *
   * @var \Drupal\payment\Plugin\Payment\Method\PaymentMethodManagerInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $paymentMethodManager;

  /**
   * The class under test.
   *
   * @var \Drupal\payment\Plugin\Payment\Method\PaymentExecutionPaymentMethodManager
   */
  protected $sut;

  /**
   * {@inheritdoc}
   */
  public function setUp() : void {
    $this->account = $this
      ->createMock(AccountInterface::class);
    $this->payment = $this
      ->createMock(PaymentInterface::class);
    $this->paymentMethodManager = $this
      ->createMock(PaymentMethodManagerInterface::class);
    $this->sut = new PaymentExecutionPaymentMethodManager($this->payment, $this->account, $this->paymentMethodManager);
  }

  /**
   * @covers ::processDecoratedDefinitions
   */
  public function testProcessDecoratedDefinitions() {
    $payment_method_id_a = $this
      ->randomMachineName();
    $payment_method_a = $this
      ->createMock(PaymentMethodInterface::class);
    $payment_method_a
      ->expects($this
      ->atLeastOnce())
      ->method('executePaymentAccess')
      ->with($this->account)
      ->willReturn(AccessResult::allowed());
    $payment_method_id_b = $this
      ->randomMachineName();
    $payment_method_b = $this
      ->createMock(PaymentMethodInterface::class);
    $payment_method_b
      ->expects($this
      ->atLeastOnce())
      ->method('executePaymentAccess')
      ->with($this->account)
      ->willReturn(AccessResult::forbidden());
    $payment_method_definitions = [
      $payment_method_id_a => [
        'id' => $payment_method_id_a,
      ],
      $payment_method_id_b => [
        'id' => $payment_method_id_b,
      ],
    ];
    $this->paymentMethodManager
      ->expects($this
      ->atLeastOnce())
      ->method('getDefinitions')
      ->willReturn($payment_method_definitions);
    $map = [
      [
        $payment_method_id_a,
        [],
        $payment_method_a,
      ],
      [
        $payment_method_id_b,
        [],
        $payment_method_b,
      ],
    ];
    $this->paymentMethodManager
      ->expects($this
      ->atLeast(count($map)))
      ->method('createInstance')
      ->willReturnMap($map);
    $filtered_plugin_definitions = $this->sut
      ->getDefinitions();
    $expected_filtered_plugin_definitions = [
      $payment_method_id_a => [
        'id' => $payment_method_id_a,
      ],
    ];
    $this
      ->assertSame($expected_filtered_plugin_definitions, $filtered_plugin_definitions);
  }

  /**
   * @covers ::getOperationsProvider
   */
  public function testGetOperationsProvider() {
    $payment_method_id = $this
      ->randomMachineName();
    $payment_method = $this
      ->createMock(PaymentMethodInterface::class);
    $payment_method
      ->expects($this
      ->atLeastOnce())
      ->method('executePaymentAccess')
      ->with($this->account)
      ->willReturn(AccessResult::allowed());
    $payment_method_definitions = [
      $payment_method_id => [
        'id' => $payment_method_id,
      ],
    ];
    $this->paymentMethodManager
      ->expects($this
      ->atLeastOnce())
      ->method('getDefinitions')
      ->willReturn($payment_method_definitions);
    $this->paymentMethodManager
      ->expects($this
      ->atLeastOnce())
      ->method('createInstance')
      ->with($payment_method_id)
      ->willReturn($payment_method);
    $operations_provider = $this
      ->createMock(PluginOperationsProviderInterface::class);
    $this->paymentMethodManager
      ->expects($this
      ->atLeastOnce())
      ->method('getOperationsProvider')
      ->with($payment_method_id)
      ->willReturn($operations_provider);
    $this
      ->assertSame($operations_provider, $this->sut
      ->getOperationsProvider($payment_method_id));
  }

  /**
   * @covers ::getOperationsProvider
   */
  public function testGetOperationsProviderWithNonExistentPlugin() {
    $this
      ->expectException(PluginNotFoundException::class);
    $this->paymentMethodManager
      ->expects($this
      ->atLeastOnce())
      ->method('getDefinitions')
      ->willReturn([]);
    $this->sut
      ->getOperationsProvider($this
      ->randomMachineName());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PaymentExecutionPaymentMethodManagerTest::$account protected property The account.
PaymentExecutionPaymentMethodManagerTest::$payment protected property The payment to filter methods by.
PaymentExecutionPaymentMethodManagerTest::$paymentMethodManager protected property The original payment method manager.
PaymentExecutionPaymentMethodManagerTest::$sut protected property The class under test.
PaymentExecutionPaymentMethodManagerTest::setUp public function Overrides UnitTestCase::setUp
PaymentExecutionPaymentMethodManagerTest::testGetOperationsProvider public function @covers ::getOperationsProvider
PaymentExecutionPaymentMethodManagerTest::testGetOperationsProviderWithNonExistentPlugin public function @covers ::getOperationsProvider
PaymentExecutionPaymentMethodManagerTest::testProcessDecoratedDefinitions public function @covers ::processDecoratedDefinitions
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.