-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomValidatorTest.php
176 lines (138 loc) · 5.42 KB
/
CustomValidatorTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
/**
* Validator Test with custom implementation.
*
* @package TheWebSolver\Codegarage\Test
*/
declare( strict_types = 1 );
namespace TheWebSolver\Codegarage\Test;
use PHPUnit\Framework\TestCase;
use TheWebSolver\Codegarage\PaymentCard\CardType;
use TheWebSolver\Codegarage\PaymentCard\Traits\CardResolver;
use TheWebSolver\Codegarage\PaymentCard\Traits\BatchResolver;
use TheWebSolver\Codegarage\PaymentCard\CardInterface as Card;
use TheWebSolver\Codegarage\PaymentCard\CardFactory as Factory;
class CustomValidatorTest extends TestCase {
public static string $payload;
public static function setUpBeforeClass(): void {
$slash = DIRECTORY_SEPARATOR;
self::$payload = dirname( __DIR__ ) . $slash . 'Resource' . $slash . 'paymentCards.json';
}
public function testWithCustomLuhn(): void {
$luhnAlwaysPass = new class() extends CardType {
public static function matchesLuhnAlgorithm( string $value, bool $shouldRun = true ): bool {
return true;
}
};
$americanExpressCard = ( new $luhnAlwaysPass() )
->setLength( array( 15 ) )
->setIdRange( array( 34, 37 ) );
$this->assertTrue( $americanExpressCard->isNumberValid( 378282246310005 ) );
$luhnAlwaysFails = new class() extends CardType {
public static function matchesLuhnAlgorithm( string $value, bool $shouldRun = true ): bool {
return false;
}
};
$americanExpressCard = ( new $luhnAlwaysFails() )
->setLength( array( 15 ) )
->setIdRange( array( 34, 37 ) );
$this->assertFalse( $americanExpressCard->isNumberValid( 378282246310005 ) );
}
public function testWithAllowedCards(): void {
$allowedCards = array( 'americanExpress', 'dinersClub', 'visa' );
$class = new class( $allowedCards ) {
use CardResolver {
getCards as public;
}
/** @param ?string[] $allowedCards */
public function __construct( ?array $allowedCards = null, Factory $factory = new Factory() ) {
if ( empty( $allowedCards ) ) {
return;
}
$this->withoutDefaults()->setCards(
...array_map( $factory->withPayload( CustomValidatorTest::$payload )->createCard( ... ), $allowedCards )
);
}
public function validate( string|int $cardNumber ): bool {
return $this->resolveCardFromNumber( $cardNumber ) instanceof Card;
}
};
$this->assertCount( expectedCount: 3, haystack: $class->getCards() );
$this->assertTrue( $class->validate( cardNumber: 378282246310005 ) ); // American Express.
$this->assertFalse( $class->validate( cardNumber: 5105105105105100 ) ); // Mastercard
}
public function testInBatch(): void {
$validator = new class() {
use BatchResolver {
getCoveredCards as public;
resetCoveredCards as public;
}
/** @var array{first:string,second:string} */
private array $batches;
public function __construct() {
$slash = DIRECTORY_SEPARATOR;
$this->batches = array(
'first' => __DIR__ . $slash . 'Resource' . $slash . 'Cards.json',
'second' => dirname( __DIR__ ) . $slash . 'Resource' . $slash . 'paymentCards.json',
);
}
public function validate( string|int $cardNumber ): bool {
$firstBatch = Factory::createFromJsonFile( path: $this->batches['first'], lazyload: true );
if ( $this->resolveCardFromNumberIn( $firstBatch, $cardNumber ) ) {
return true;
}
$secondBatch = Factory::createFromJsonFile( path: $this->batches['second'], lazyload: true );
return $this->resolveCardFromNumberIn( $secondBatch, $cardNumber ) ? true : false;
}
};
$this->assertTrue( $validator->validate( 378282246310005 ) ); // American Express.
$this->assertCount( expectedCount: 4, haystack: $validator->getCoveredCards() );
$validator->resetCoveredCards();
$this->assertTrue( $validator->validate( 5105105105105100 ) ); // Mastercard.
$this->assertCount( expectedCount: 6, haystack: $validator->getCoveredCards() );
$validator->resetCoveredCards();
$this->assertFalse( $validator->validate( 'invalid card number' ) );
$this->assertCount( expectedCount: 13, haystack: $validator->getCoveredCards() );
}
/**
* @param string|mixed[]|null $allowedCards
* @dataProvider provideAllowedOrBatchData
*/
public function testEitherAllowedOrBatch( string|array|null $allowedCards, int $number, int $count = 0 ): void {
$class = new class( $allowedCards ) {
use CardResolver, BatchResolver {
BatchResolver::getCoveredCards as public;
}
private bool $useBatch = false;
/** @param string|mixed[]|null $allowedCards */
public function __construct(
string|array|null $allowedCards,
private readonly Factory $factory = new Factory()
) {
if ( empty( $allowedCards ) ) {
return;
}
$this->useBatch = true;
$factory->withPayload( $allowedCards );
}
public function validate( string|int $cardNumber ): bool {
$card = ! $this->useBatch
? $this->resolveCardFromNumber( $cardNumber )
: $this->resolveCardFromNumberIn( batch: $this->factory->lazyLoadCards(), number: $cardNumber );
return $card instanceof Card;
}
};
$this->assertTrue( $class->validate( $number ) );
$this->assertCount( expectedCount: $count, haystack: $class->getCoveredCards() );
}
/** @return mixed[] */
public function provideAllowedOrBatchData(): array {
$slash = DIRECTORY_SEPARATOR;
$payload = dirname( __DIR__ ) . $slash . 'Resource' . $slash . 'paymentCards.json';
return array(
array( null, 378282246310005 ),
array( $payload, 378282246310005, 1 ),
array( $payload, 5105105105105100, 3 ),
);
}
}