You are here

class LineItemCollectionTest in Payment 8.2

@coversDefaultClass \Drupal\payment\LineItemCollection

@group Payment

Hierarchy

Expanded class hierarchy of LineItemCollectionTest

File

tests/src/Unit/LineItemCollectionTest.php, line 14

Namespace

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

  /**
   * The line items' ISO 4217 currency code.
   *
   * @var string|null $currency_code
   *   The currency code or NULL if the collection itself has no specific
   *   currency.
   */
  protected $currencyCode;

  /**
   * The line items.
   *
   * @var \Drupal\payment\Plugin\Payment\LineItem\PaymentLineItemInterface[]|\PHPUnit\Framework\MockObject\MockObject[]
   *   Keys are line item names.
   */
  protected $lineItems = [];

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

  /**
   * {@inheritdoc}
   */
  public function setUp() : void {
    $this->currencyCode = $this
      ->randomMachineName();
    $line_item_name_a = $this
      ->randomMachineName();
    $line_item_a = $this
      ->createMock(PaymentLineItemInterface::class);
    $line_item_a
      ->expects($this
      ->atLeastOnce())
      ->method('getName')
      ->willReturn($line_item_name_a);
    $line_item_name_b = $this
      ->randomMachineName();
    $line_item_b = $this
      ->createMock(PaymentLineItemInterface::class);
    $line_item_b
      ->expects($this
      ->atLeastOnce())
      ->method('getName')
      ->willReturn($line_item_name_b);
    $this->lineItems = [
      $line_item_name_a => $line_item_a,
      $line_item_name_b => $line_item_b,
    ];
    $this->sut = new LineItemCollection($this->currencyCode, $this->lineItems);
  }

  /**
   * @covers ::__construct
   * @covers ::getLineItem
   * @covers ::getLineItems
   */
  public function testConstruct() {
    $this->sut = new LineItemCollection($this->currencyCode, $this->lineItems);

    // Test that all line items can be retrieved individually after they have
    // been injected through the constructor.
    foreach ($this->lineItems as $name => $line_item) {
      $this
        ->assertSame($line_item, $this->sut
        ->getLineItem($name));
    }

    // Test that all line items can be retrieved after they have been injected
    // through the constructor.
    $this
      ->assertSame($this->lineItems, $this->sut
      ->getLineItems());

    // Test that the currency code can be retrieved after it has been injected
    // through the constructor.
    $this
      ->assertSame($this->currencyCode, $this->sut
      ->getCurrencyCode());
  }

  /**
   * @covers ::getCurrencyCode
   * @covers ::setCurrencyCode
   */
  public function testGetCurrencyCode() {
    $currency_code = $this
      ->randomMachineName();
    $this
      ->assertSame($this->sut, $this->sut
      ->setCurrencyCode($currency_code));
    $this
      ->assertSame($currency_code, $this->sut
      ->getCurrencyCode());
  }

  /**
   * @covers ::setLineItem
   * @covers ::getLineItem
   * @covers ::getLineItems
   */
  public function testSetLineItem() {
    $line_item_name = $this
      ->randomMachineName();
    $line_item = $this
      ->createMock(PaymentLineItemInterface::class);
    $line_item
      ->expects($this
      ->atLeastOnce())
      ->method('getName')
      ->willReturn($line_item_name);
    $this
      ->assertSame($this->sut, $this->sut
      ->setLineItem($line_item));
    $this
      ->assertSame($line_item, $this->sut
      ->getLineItem($line_item_name));
    $expected = $this->lineItems + [
      $line_item_name => $line_item,
    ];
    $this
      ->assertSame($expected, $this->sut
      ->getLineItems());
  }

  /**
   * @covers ::setLineItems
   * @covers ::getLineItem
   * @covers ::getLineItems
   */
  public function testSetLineItems() {
    $line_item_name_a = $this
      ->randomMachineName();
    $line_item_a = $this
      ->createMock(PaymentLineItemInterface::class);
    $line_item_a
      ->expects($this
      ->atLeastOnce())
      ->method('getName')
      ->willReturn($line_item_name_a);
    $line_item_name_b = $this
      ->randomMachineName();
    $line_item_b = $this
      ->createMock(PaymentLineItemInterface::class);
    $line_item_b
      ->expects($this
      ->atLeastOnce())
      ->method('getName')
      ->willReturn($line_item_name_b);
    $line_items = [
      $line_item_name_a => $line_item_a,
      $line_item_name_b => $line_item_b,
    ];
    $this
      ->assertSame($this->sut, $this->sut
      ->setLineItems($line_items));
    $this
      ->assertSame($line_item_a, $this->sut
      ->getLineItem($line_item_name_a));
    $this
      ->assertSame($line_item_b, $this->sut
      ->getLineItem($line_item_name_b));
    $expected = [
      $line_item_name_a => $line_item_a,
      $line_item_name_b => $line_item_b,
    ];
    $this
      ->assertSame($expected, $this->sut
      ->getLineItems());
  }

  /**
   * @covers ::unsetLineItem
   * @covers ::getLineItem
   * @covers ::getLineItems
   */
  public function testUnsetLineItems() {
    list($line_item_name_a, $line_item_name_b) = array_keys($this->lineItems);
    $this
      ->assertSame($this->sut, $this->sut
      ->unsetLineItem($line_item_name_a));
    $this
      ->assertNull($this->sut
      ->getLineItem($line_item_name_a));
    $this
      ->assertSame($this->lineItems[$line_item_name_b], $this->sut
      ->getLineItem($line_item_name_b));
    $expected = [
      $line_item_name_b => $this->lineItems[$line_item_name_b],
    ];
    $this
      ->assertSame($expected, $this->sut
      ->getLineItems());
  }

  /**
   * @covers ::getLineItemsByType
   */
  public function testGetLineItemsByType() {
    $line_item_type = $this
      ->randomMachineName();

    /** @var \Drupal\payment\Plugin\Payment\LineItem\PaymentLineItemInterface|\PHPUnit\Framework\MockObject\MockObject $line_item_a */
    $line_item_a = reset($this->lineItems);
    $line_item_name_a = key($this->lineItems);
    $line_item_a
      ->expects($this
      ->atLeastOnce())
      ->method('getPluginId')
      ->willReturn($line_item_type);
    $line_item_b = end($this->lineItems);
    $line_item_b
      ->expects($this
      ->atLeastOnce())
      ->method('getPluginId')
      ->willReturn($this
      ->randomMachineName());
    $expected = [
      $line_item_name_a => $line_item_a,
    ];
    $this
      ->assertSame($expected, $this->sut
      ->getLineItemsByType($line_item_type));
  }

  /**
   * @covers ::getAmount
   */
  public function testGetAmount() {
    $line_item_amount_a = mt_rand();
    $line_item_amount_b = mt_rand();

    /** @var \Drupal\payment\Plugin\Payment\LineItem\PaymentLineItemInterface|\PHPUnit\Framework\MockObject\MockObject $line_item_a */

    /** @var \Drupal\payment\Plugin\Payment\LineItem\PaymentLineItemInterface|\PHPUnit\Framework\MockObject\MockObject $line_item_b */
    list($line_item_a, $line_item_b) = array_values($this->lineItems);
    $line_item_a
      ->expects($this
      ->atLeastOnce())
      ->method('getTotalAmount')
      ->willReturn($line_item_amount_a);
    $line_item_b
      ->expects($this
      ->atLeastOnce())
      ->method('getTotalAmount')
      ->willReturn($line_item_amount_b);
    $this
      ->assertSame(bcadd($line_item_amount_a, $line_item_amount_b, 6), $this->sut
      ->getAmount());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LineItemCollectionTest::$currencyCode protected property The line items' ISO 4217 currency code.
LineItemCollectionTest::$lineItems protected property The line items.
LineItemCollectionTest::$sut protected property The subject under test.
LineItemCollectionTest::setUp public function Overrides UnitTestCase::setUp
LineItemCollectionTest::testConstruct public function @covers ::__construct @covers ::getLineItem @covers ::getLineItems
LineItemCollectionTest::testGetAmount public function @covers ::getAmount
LineItemCollectionTest::testGetCurrencyCode public function @covers ::getCurrencyCode @covers ::setCurrencyCode
LineItemCollectionTest::testGetLineItemsByType public function @covers ::getLineItemsByType
LineItemCollectionTest::testSetLineItem public function @covers ::setLineItem @covers ::getLineItem @covers ::getLineItems
LineItemCollectionTest::testSetLineItems public function @covers ::setLineItems @covers ::getLineItem @covers ::getLineItems
LineItemCollectionTest::testUnsetLineItems public function @covers ::unsetLineItem @covers ::getLineItem @covers ::getLineItems
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.