class CreditCardTest in Commerce Core 8.2
@coversDefaultClass \Drupal\commerce_payment\CreditCard @group commerce
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\commerce_payment\Unit\CreditCardTest
Expanded class hierarchy of CreditCardTest
File
- modules/
payment/ tests/ src/ Unit/ CreditCardTest.php, line 14
Namespace
Drupal\Tests\commerce_payment\UnitView source
class CreditCardTest extends UnitTestCase {
/**
* @covers ::getTypes
*/
public function testGetTypes() {
$types = CreditCard::getTypes();
$this
->assertIsArray($types);
foreach ($types as $key => $type) {
$this
->assertInstanceOf(CreditCardType::class, $type);
$this
->assertEquals($key, $type
->getId());
}
}
/**
* @covers ::getType
*/
public function testGetInvalidType() {
$this
->expectException(\InvalidArgumentException::class);
$this
->expectExceptionMessage('Invalid credit card type "monkey"');
CreditCard::getType("monkey");
}
/**
* @covers ::getType
*/
public function testGetType() {
$visa = CreditCard::getType('visa');
$this
->assertInstanceOf(CreditCardType::class, $visa);
$this
->assertEquals('visa', $visa
->getId());
}
/**
* @covers ::detectType
* @covers ::matchPrefix
* @covers ::validateNumber
* @covers ::validateLuhn
* @dataProvider cardsProvider
*/
public function testValidateNumber($number, $type, $valid) {
$detected_type = CreditCard::detectType($number);
if ($detected_type) {
$this
->assertEquals($detected_type
->getId(), $type);
$result = CreditCard::validateNumber($number, $detected_type);
$this
->assertEquals($valid, $result);
}
else {
$this
->assertEquals(NULL, $type);
}
}
/**
* @covers ::validateExpirationDate
* @dataProvider expirationDateProvider
*/
public function testValidateExpirationDate($month, $year, $valid) {
$result = CreditCard::validateExpirationDate($month, $year);
$this
->assertEquals($valid, $result);
}
/**
* @covers ::calculateExpirationTimestamp
*/
public function testCalculateExpirationTimestamp() {
$timestamp = CreditCard::calculateExpirationTimestamp(12, 2012);
$date = date('Y-m-d H:i:s', $timestamp);
$expected_date = date('2012-12-31 23:59:59');
$this
->assertEquals($expected_date, $date);
}
/**
* @covers ::validateSecurityCode
* @dataProvider securityCodeProvider
*/
public function testsValidateSecurityCode($security_code, $type, $valid) {
$type = CreditCard::getType($type);
$result = CreditCard::validateSecurityCode($security_code, $type);
$this
->assertEquals($valid, $result);
}
/**
* Data provider for ::testValidateNumber.
*
* @return array
* A list of testValidateNumber function arguments.
*/
public function cardsProvider() {
return [
// Non-numeric value.
[
'invalid',
NULL,
FALSE,
],
// Invalid length.
[
41111111111111111,
'visa',
FALSE,
],
// Fails luhn check.
[
41111111111111112,
'visa',
FALSE,
],
// Valid numbers.
[
4111111111111111,
'visa',
TRUE,
],
[
6759649826438453,
'maestro',
TRUE,
],
[
3528000700000000,
'jcb',
TRUE,
],
[
5555555555554444,
'mastercard',
TRUE,
],
[
36700102000000,
'dinersclub',
TRUE,
],
[
6011000400000000,
'discover',
TRUE,
],
[
6208205838887174,
'unionpay',
TRUE,
],
[
374251018720018,
'amex',
TRUE,
],
];
}
/**
* Data provider for ::testValidateExpirationDate.
*
* @return array
* A list of testValidateExpirationDate function arguments.
*/
public function expirationDateProvider() {
return [
// Invalid month.
[
0,
2020,
FALSE,
],
[
13,
2020,
FALSE,
],
// Invalid year.
[
10,
2012,
FALSE,
],
// Valid month and year.
[
date('n'),
date('Y'),
TRUE,
],
];
}
/**
* Data provider for ::testValidateSecurityCode.
*
* @return array
* A list of testValidateSecurityCode function arguments.
*/
public function securityCodeProvider() {
return [
// Invalid lengths.
[
1,
'visa',
FALSE,
],
[
1111,
'visa',
FALSE,
],
// Non-numeric inputs.
[
'llama',
'visa',
FALSE,
],
[
'12.4',
'visa',
FALSE,
],
// Valid number.
[
111,
'visa',
TRUE,
],
];
}
/**
* @covers ::getAvsResponseCodeMeanings
*/
public function testGetAvsResponseCodeMeanings() {
$response_meanings = CreditCard::getAvsResponseCodeMeanings();
$types = CreditCard::getTypes();
$this
->assertIsArray($response_meanings);
foreach ($response_meanings as $credit_card_type => $mapping) {
$this
->assertInstanceOf(CreditCardType::class, $types[$credit_card_type]);
$this
->assertIsArray($mapping);
foreach ($mapping as $code => $meaning) {
$this
->assertInstanceOf(TranslatableMarkup::class, $meaning);
}
}
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CreditCardTest:: |
public | function | Data provider for ::testValidateNumber. | |
CreditCardTest:: |
public | function | Data provider for ::testValidateExpirationDate. | |
CreditCardTest:: |
public | function | Data provider for ::testValidateSecurityCode. | |
CreditCardTest:: |
public | function | @covers ::calculateExpirationTimestamp | |
CreditCardTest:: |
public | function | @covers ::getAvsResponseCodeMeanings | |
CreditCardTest:: |
public | function | @covers ::getType | |
CreditCardTest:: |
public | function | @covers ::getType | |
CreditCardTest:: |
public | function | @covers ::getTypes | |
CreditCardTest:: |
public | function | @covers ::validateSecurityCode @dataProvider securityCodeProvider | |
CreditCardTest:: |
public | function | @covers ::validateExpirationDate @dataProvider expirationDateProvider | |
CreditCardTest:: |
public | function | @covers ::detectType @covers ::matchPrefix @covers ::validateNumber @covers ::validateLuhn @dataProvider cardsProvider | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. | |
UnitTestCase:: |
protected | function | 340 |