You are here

class DatabaseQueueTest in Payment 8.2

@coversDefaultClass \Drupal\payment\DatabaseQueue

@group Payment

Hierarchy

Expanded class hierarchy of DatabaseQueueTest

File

tests/src/Unit/DatabaseQueueTest.php, line 16

Namespace

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

  /**
   * The database connection.
   *
   * @var \Drupal\Core\Database\Connection|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $database;

  /**
   * The event dispatcher.
   *
   * @var \Drupal\payment\EventDispatcherInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $eventDispatcher;

  /**
   * The database connection.
   *
   * @var \Drupal\payment\Plugin\Payment\Status\PaymentStatusManagerInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $paymentStatusManager;

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

  /**
   * The unique ID of the queue (instance).
   *
   * @var string
   */
  protected $queueId;

  /**
   * {@inheritdoc}
   */
  function setUp() : void {
    parent::setUp();
    $this->database = $this
      ->getMockBuilder(Connection::class)
      ->disableOriginalConstructor()
      ->getMock();
    $this->eventDispatcher = $this
      ->createMock(EventDispatcherInterface::class);
    $this->paymentStatusManager = $this
      ->createMock(PaymentStatusManagerInterface::class);
    $this->queueId = $this
      ->randomMachineName();
    $this->sut = new DatabaseQueue($this->queueId, $this->database, $this->eventDispatcher, $this->paymentStatusManager);
  }

  /**
   * @covers ::getClaimExpirationPeriod
   * @covers ::setClaimExpirationPeriod
   */
  public function testGetClaimExpirationPeriod() {
    $expiration_period = mt_rand();
    $this
      ->assertSame($this->sut, $this->sut
      ->setClaimExpirationPeriod($expiration_period));
    $this
      ->assertSame($expiration_period, $this->sut
      ->getClaimExpirationPeriod());
  }

  /**
   * @covers ::getAllowedPaymentStatusIds
   * @covers ::setAllowedPaymentStatusIds
   */
  public function testGetAllowedPaymentStatusIds() {
    $allowed_payment_status_ids = array(
      $this
        ->randomMachineName(),
      $this
        ->randomMachineName(),
    );
    $this
      ->assertSame($this->sut, $this->sut
      ->setAllowedPaymentStatusIds($allowed_payment_status_ids));
    $this
      ->assertSame($allowed_payment_status_ids, $this->sut
      ->getAllowedPaymentStatusIds());
  }

  /**
   * @covers ::claimPayment
   */
  public function testClaimPayment() {
    $payment_id = mt_rand();
    $acquisition_code = $this
      ->randomMachineName();

    /** @var \Drupal\payment\DatabaseQueue|\PHPUnit\Framework\MockObject\MockObject $queue */
    $queue = $this
      ->getMockBuilder(DatabaseQueue::class)
      ->setConstructorArgs(array(
      $this->queueId,
      $this->database,
      $this->eventDispatcher,
      $this->paymentStatusManager,
    ))
      ->setMethods(array(
      'tryClaimPaymentOnce',
    ))
      ->getMock();
    $queue
      ->expects($this
      ->at(0))
      ->method('tryClaimPaymentOnce')
      ->with($payment_id)
      ->willReturn(FALSE);
    $queue
      ->expects($this
      ->at(1))
      ->method('tryClaimPaymentOnce')
      ->with($payment_id)
      ->willReturn($acquisition_code);
    $this
      ->assertSame($acquisition_code, $queue
      ->claimPayment($payment_id));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DatabaseQueueTest::$database protected property The database connection.
DatabaseQueueTest::$eventDispatcher protected property The event dispatcher.
DatabaseQueueTest::$paymentStatusManager protected property The database connection.
DatabaseQueueTest::$queueId protected property The unique ID of the queue (instance).
DatabaseQueueTest::$sut protected property The class under test.
DatabaseQueueTest::setUp function Overrides UnitTestCase::setUp
DatabaseQueueTest::testClaimPayment public function @covers ::claimPayment
DatabaseQueueTest::testGetAllowedPaymentStatusIds public function @covers ::getAllowedPaymentStatusIds @covers ::setAllowedPaymentStatusIds
DatabaseQueueTest::testGetClaimExpirationPeriod public function @covers ::getClaimExpirationPeriod @covers ::setClaimExpirationPeriod
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.