You are here

class CreditCardTypeTest in Commerce Core 8.2

@coversDefaultClass \Drupal\commerce_payment\CreditCardType @group commerce

Hierarchy

Expanded class hierarchy of CreditCardTypeTest

File

modules/payment/tests/src/Unit/CreditCardTypeTest.php, line 12

Namespace

Drupal\Tests\commerce_payment\Unit
View source
class CreditCardTypeTest extends UnitTestCase {

  /**
   * The credit card type definition array.
   *
   * @var array
   */
  protected $definition;

  /**
   * The credit card type.
   *
   * @var \Drupal\commerce_payment\CreditCardType
   */
  protected $type;

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this->definition = [
      'id' => 'dummy',
      'label' => 'DummyCard',
      'number_prefixes' => [
        '51-55',
        '500',
        '222100-272099',
      ],
      'number_lengths' => [
        5,
        10,
      ],
      'security_code_length' => 6,
      'uses_luhn' => FALSE,
    ];
    $this->type = new CreditCardType($this->definition);
  }

  /**
   * @covers ::getId
   */
  public function testGetId() {
    $this
      ->assertEquals($this->definition['id'], $this->type
      ->getId());
  }

  /**
   * @covers ::getLabel
   */
  public function testGetLabel() {
    $this
      ->assertEquals($this->definition['label'], $this->type
      ->getLabel());
  }

  /**
   * @covers ::getNumberPrefixes
   */
  public function testGetNumberPrefixes() {
    $this
      ->assertEquals($this->definition['number_prefixes'], $this->type
      ->getNumberPrefixes());
  }

  /**
   * @covers ::getNumberLengths
   */
  public function testGetNumberLengths() {
    $this
      ->assertEquals($this->definition['number_lengths'], $this->type
      ->getNumberLengths(), 'Credit card type number length matches.');
  }

  /**
   * @covers ::getSecurityCodeLength
   */
  public function testGetSecurityCodeLength() {
    $this
      ->assertEquals($this->definition['security_code_length'], $this->type
      ->getSecurityCodeLength(), 'Credit card type security code length matches.');
  }

  /**
   * @covers ::usesLuhn
   */
  public function testUsesLuhn() {
    $this
      ->assertEquals($this->definition['uses_luhn'], $this->type
      ->usesLuhn());
  }

  /**
   * Tests the creation of a CreditCardType with an invalid definition.
   *
   * @dataProvider definitionProvider
   */
  public function testInvalidDefinition($definition, $message) {
    $this
      ->expectException(\InvalidArgumentException::class);
    $this
      ->expectExceptionMessage($message);
    $card = new CreditCardType($definition);
  }

  /**
   * Data provider for ::testInvalidDefinition.
   *
   * @return array
   *   A list of testInvalidDefinition function arguments.
   */
  public function definitionProvider() {
    return [
      [
        [],
        'Missing required property id.',
      ],
      [
        [
          'id' => 'llama',
        ],
        'Missing required property label.',
      ],
      [
        [
          'id' => 'llama',
          'label' => 'Llama',
        ],
        'Missing required property number_prefixes.',
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CreditCardTypeTest::$definition protected property The credit card type definition array.
CreditCardTypeTest::$type protected property The credit card type.
CreditCardTypeTest::definitionProvider public function Data provider for ::testInvalidDefinition.
CreditCardTypeTest::setUp protected function Overrides UnitTestCase::setUp
CreditCardTypeTest::testGetId public function @covers ::getId
CreditCardTypeTest::testGetLabel public function @covers ::getLabel
CreditCardTypeTest::testGetNumberLengths public function @covers ::getNumberLengths
CreditCardTypeTest::testGetNumberPrefixes public function @covers ::getNumberPrefixes
CreditCardTypeTest::testGetSecurityCodeLength public function @covers ::getSecurityCodeLength
CreditCardTypeTest::testInvalidDefinition public function Tests the creation of a CreditCardType with an invalid definition.
CreditCardTypeTest::testUsesLuhn public function @covers ::usesLuhn
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.