You are here

class EventDispatcherCollectionTest in Payment 8.2

@coversDefaultClass \Drupal\payment\EventDispatcherCollection

@group Payment

Hierarchy

Expanded class hierarchy of EventDispatcherCollectionTest

File

tests/src/Unit/EventDispatcherCollectionTest.php, line 29

Namespace

Drupal\Tests\payment\Unit
View source
class EventDispatcherCollectionTest extends UnitTestCase {

  /**
   * The subject under test.
   *
   * @var \Drupal\payment\EventDispatcherCollection
   */
  protected $sut;

  /**
   * {@inheritdoc}
   */
  public function setUp() : void {
    $this->sut = new EventDispatcherCollection();
  }

  /**
   * @covers ::alterQueueLoadedPaymentIds
   * @covers ::addEventDispatcher
   */
  public function testAlterQueueLoadedPaymentIds() {
    $queue_id = $this
      ->randomMachineName();
    $category_id = $this
      ->randomMachineName();
    $owner_id = mt_rand();
    $payment_ids = [
      mt_rand(),
      mt_rand(),
      mt_rand(),
    ];
    $payment_ids_a = [
      mt_rand(),
      mt_rand(),
      mt_rand(),
    ];
    $payment_ids_b = [
      mt_rand(),
      mt_rand(),
      mt_rand(),
    ];
    $event_dispatcher_a = $this
      ->createMock(EventDispatcherInterface::class);
    $event_dispatcher_a
      ->expects($this
      ->atLeastOnce())
      ->method('alterQueueLoadedPaymentIds')
      ->with($queue_id, $category_id, $owner_id, $payment_ids)
      ->willReturn($payment_ids_a);
    $event_dispatcher_b = $this
      ->createMock(EventDispatcherInterface::class);
    $event_dispatcher_b
      ->expects($this
      ->atLeastOnce())
      ->method('alterQueueLoadedPaymentIds')
      ->with($queue_id, $category_id, $owner_id, $payment_ids_a)
      ->willReturn($payment_ids_b);
    $this->sut
      ->addEventDispatcher($event_dispatcher_a);
    $this->sut
      ->addEventDispatcher($event_dispatcher_b);
    $this
      ->assertSame($payment_ids_b, $this->sut
      ->alterQueueLoadedPaymentIds($queue_id, $category_id, $owner_id, $payment_ids));
  }

  /**
   * @covers ::setPaymentStatus
   */
  public function testSetPaymentStatus() {
    $payment = $this
      ->createMock(PaymentInterface::class);
    $previous_payment_status = $this
      ->createMock(PaymentStatusInterface::class);
    $event_dispatcher_a = $this
      ->createMock(EventDispatcherInterface::class);
    $event_dispatcher_a
      ->expects($this
      ->atLeastOnce())
      ->method('setPaymentStatus')
      ->with($payment, $previous_payment_status);
    $event_dispatcher_b = $this
      ->createMock(EventDispatcherInterface::class);
    $event_dispatcher_b
      ->expects($this
      ->atLeastOnce())
      ->method('setPaymentStatus')
      ->with($payment, $previous_payment_status);
    $this->sut
      ->addEventDispatcher($event_dispatcher_a);
    $this->sut
      ->addEventDispatcher($event_dispatcher_b);
    $this->sut
      ->setPaymentStatus($payment, $previous_payment_status);
  }

  /**
   * @covers ::preExecutePayment
   *
   */
  public function testPreExecutePayment() {
    $payment = $this
      ->createMock(PaymentInterface::class);
    $event_dispatcher_a = $this
      ->createMock(EventDispatcherInterface::class);
    $event_dispatcher_a
      ->expects($this
      ->atLeastOnce())
      ->method('preExecutePayment')
      ->with($payment);
    $event_dispatcher_b = $this
      ->createMock(EventDispatcherInterface::class);
    $event_dispatcher_b
      ->expects($this
      ->atLeastOnce())
      ->method('preExecutePayment')
      ->with($payment);
    $this->sut
      ->addEventDispatcher($event_dispatcher_a);
    $this->sut
      ->addEventDispatcher($event_dispatcher_b);
    $this->sut
      ->preExecutePayment($payment);
  }

  /**
   * @covers ::executePaymentAccess
   *
   * @dataProvider providerExecutePaymentAccess
   *
   * @param bool|null $expected_access
   *   TRUE for allowed, NULL for neutral, FALSE for forbidden.
   * @param \Drupal\Core\Access\AccessResult $event_dispatcher_access_a
   * @param \Drupal\Core\Access\AccessResult $event_dispatcher_access_b
   */
  public function testExecutePaymentAccess($expected_access, AccessResult $event_dispatcher_access_a, AccessResult $event_dispatcher_access_b) {
    $payment = $this
      ->createMock(PaymentInterface::class);
    $payment_method = $this
      ->createMock(PaymentMethodInterface::class);
    $account = $this
      ->createMock(AccountInterface::class);
    $event_dispatcher_a = $this
      ->createMock(EventDispatcherInterface::class);
    $event_dispatcher_a
      ->expects($this
      ->atLeastOnce())
      ->method('executePaymentAccess')
      ->with($payment, $payment_method, $account)
      ->willReturn($event_dispatcher_access_a);
    $event_dispatcher_b = $this
      ->createMock(EventDispatcherInterface::class);
    $event_dispatcher_b
      ->expects($this
      ->atLeastOnce())
      ->method('executePaymentAccess')
      ->with($payment, $payment_method, $account)
      ->willReturn($event_dispatcher_access_b);
    $this->sut
      ->addEventDispatcher($event_dispatcher_a);
    $this->sut
      ->addEventDispatcher($event_dispatcher_b);
    $access = $this->sut
      ->executePaymentAccess($payment, $payment_method, $account);
    $this
      ->assertInstanceOf(AccessResultInterface::class, $access);
    if ($expected_access === TRUE) {
      $this
        ->assertTrue($access
        ->isAllowed());
    }
    elseif ($expected_access === FALSE) {
      $this
        ->assertTrue($access
        ->isForbidden());
    }
    elseif ($expected_access === NULL) {
      $this
        ->assertTrue($access
        ->isNeutral());
    }
  }

  /**
   * Provides data to self::testExecutePaymentAccess
   */
  public function providerExecutePaymentAccess() {
    return [
      [
        TRUE,
        AccessResult::allowed(),
        AccessResult::allowed(),
      ],
      [
        TRUE,
        AccessResult::allowed(),
        AccessResult::neutral(),
      ],
      [
        TRUE,
        AccessResult::neutral(),
        AccessResult::allowed(),
      ],
      [
        NULL,
        AccessResult::neutral(),
        AccessResult::neutral(),
      ],
      [
        FALSE,
        AccessResult::allowed(),
        AccessResult::forbidden(),
      ],
      [
        FALSE,
        AccessResult::neutral(),
        AccessResult::forbidden(),
      ],
      [
        FALSE,
        AccessResult::forbidden(),
        AccessResult::allowed(),
      ],
      [
        FALSE,
        AccessResult::forbidden(),
        AccessResult::neutral(),
      ],
      [
        FALSE,
        AccessResult::forbidden(),
        AccessResult::forbidden(),
      ],
    ];
  }

  /**
   * @covers ::preCapturePayment
   */
  public function testPreCapturePayment() {
    $payment = $this
      ->createMock(PaymentInterface::class);
    $event_dispatcher_a = $this
      ->createMock(EventDispatcherInterface::class);
    $event_dispatcher_a
      ->expects($this
      ->atLeastOnce())
      ->method('preCapturePayment')
      ->with($payment);
    $event_dispatcher_b = $this
      ->createMock(EventDispatcherInterface::class);
    $event_dispatcher_b
      ->expects($this
      ->atLeastOnce())
      ->method('preCapturePayment')
      ->with($payment);
    $this->sut
      ->addEventDispatcher($event_dispatcher_a);
    $this->sut
      ->addEventDispatcher($event_dispatcher_b);
    $this->sut
      ->preCapturePayment($payment);
  }

  /**
   * @covers ::preRefundPayment
   */
  public function testPreRefundPayment() {
    $payment = $this
      ->createMock(PaymentInterface::class);
    $event_dispatcher_a = $this
      ->createMock(EventDispatcherInterface::class);
    $event_dispatcher_a
      ->expects($this
      ->atLeastOnce())
      ->method('preRefundPayment')
      ->with($payment);
    $event_dispatcher_b = $this
      ->createMock(EventDispatcherInterface::class);
    $event_dispatcher_b
      ->expects($this
      ->atLeastOnce())
      ->method('preRefundPayment')
      ->with($payment);
    $this->sut
      ->addEventDispatcher($event_dispatcher_a);
    $this->sut
      ->addEventDispatcher($event_dispatcher_b);
    $this->sut
      ->preRefundPayment($payment);
  }

  /**
   * @covers ::preResumeContext
   */
  public function testPreResumeContext() {
    $payment = $this
      ->createMock(PaymentInterface::class);
    $event_dispatcher_a = $this
      ->createMock(EventDispatcherInterface::class);
    $event_dispatcher_a
      ->expects($this
      ->atLeastOnce())
      ->method('preResumeContext')
      ->with($payment);
    $event_dispatcher_b = $this
      ->createMock(EventDispatcherInterface::class);
    $event_dispatcher_b
      ->expects($this
      ->atLeastOnce())
      ->method('preResumeContext')
      ->with($payment);
    $this->sut
      ->addEventDispatcher($event_dispatcher_a);
    $this->sut
      ->addEventDispatcher($event_dispatcher_b);
    $this->sut
      ->preResumeContext($payment);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EventDispatcherCollectionTest::$sut protected property The subject under test.
EventDispatcherCollectionTest::providerExecutePaymentAccess public function Provides data to self::testExecutePaymentAccess
EventDispatcherCollectionTest::setUp public function Overrides UnitTestCase::setUp
EventDispatcherCollectionTest::testAlterQueueLoadedPaymentIds public function @covers ::alterQueueLoadedPaymentIds @covers ::addEventDispatcher
EventDispatcherCollectionTest::testExecutePaymentAccess public function @covers ::executePaymentAccess
EventDispatcherCollectionTest::testPreCapturePayment public function @covers ::preCapturePayment
EventDispatcherCollectionTest::testPreExecutePayment public function @covers ::preExecutePayment
EventDispatcherCollectionTest::testPreRefundPayment public function @covers ::preRefundPayment
EventDispatcherCollectionTest::testPreResumeContext public function @covers ::preResumeContext
EventDispatcherCollectionTest::testSetPaymentStatus public function @covers ::setPaymentStatus
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.