|
1
|
|
|
<?php declare(strict_types = 1); |
|
2
|
|
|
|
|
3
|
|
|
namespace SlevomatCsobGateway; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
use PHPUnit\Framework\TestCase; |
|
7
|
|
|
use function array_fill; |
|
8
|
|
|
use function implode; |
|
9
|
|
|
|
|
10
|
|
|
class ValidatorTest extends TestCase |
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
public function testCheckCartItemName(): void |
|
14
|
|
|
{ |
|
15
|
|
|
Validator::checkCartItemName('foo name'); |
|
16
|
|
|
|
|
17
|
|
|
try { |
|
18
|
|
|
Validator::checkCartItemName('very long long long cart item name'); |
|
19
|
|
|
self::fail(); |
|
20
|
|
|
|
|
21
|
|
|
} catch (InvalidArgumentException $e) { |
|
22
|
|
|
self::assertSame('Cart item name can have maximum of 20 characters.', $e->getMessage()); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
try { |
|
26
|
|
|
Validator::checkCartItemName(' whitespace'); |
|
27
|
|
|
self::fail(); |
|
28
|
|
|
|
|
29
|
|
|
} catch (InvalidArgumentException $e) { |
|
30
|
|
|
self::assertSame('Argument starts or ends with whitespace.', $e->getMessage()); |
|
31
|
|
|
} |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function testCheckCartItemDescription(): void |
|
35
|
|
|
{ |
|
36
|
|
|
Validator::checkCartItemDescription('foo description'); |
|
37
|
|
|
|
|
38
|
|
|
try { |
|
39
|
|
|
Validator::checkCartItemDescription('very long long long cart item description'); |
|
40
|
|
|
self::fail(); |
|
41
|
|
|
|
|
42
|
|
|
} catch (InvalidArgumentException $e) { |
|
43
|
|
|
self::assertSame('Cart item description can have maximum of 40 characters.', $e->getMessage()); |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function testCheckCartItemQuantity(): void |
|
48
|
|
|
{ |
|
49
|
|
|
Validator::checkCartItemQuantity(2); |
|
50
|
|
|
|
|
51
|
|
|
try { |
|
52
|
|
|
Validator::checkCartItemQuantity(0); |
|
53
|
|
|
self::fail(); |
|
54
|
|
|
|
|
55
|
|
|
} catch (InvalidArgumentException $e) { |
|
56
|
|
|
self::assertSame('Quantity must be greater than 0. 0 given.', $e->getMessage()); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function testCheckOrderId(): void |
|
61
|
|
|
{ |
|
62
|
|
|
Validator::checkOrderId('123'); |
|
63
|
|
|
|
|
64
|
|
|
try { |
|
65
|
|
|
Validator::checkOrderId('123456789123456789'); |
|
66
|
|
|
self::fail(); |
|
67
|
|
|
|
|
68
|
|
|
} catch (InvalidArgumentException $e) { |
|
69
|
|
|
self::assertSame('OrderId can have maximum of 10 characters.', $e->getMessage()); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
try { |
|
73
|
|
|
Validator::checkOrderId('abc'); |
|
74
|
|
|
self::fail(); |
|
75
|
|
|
|
|
76
|
|
|
} catch (InvalidArgumentException $e) { |
|
77
|
|
|
self::assertSame('OrderId must be numeric value. abc given.', $e->getMessage()); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function testCheckReturnUrl(): void |
|
82
|
|
|
{ |
|
83
|
|
|
Validator::checkReturnUrl('https://example.com'); |
|
84
|
|
|
|
|
85
|
|
|
try { |
|
86
|
|
|
Validator::checkReturnUrl('https://example.com/' . implode('-', array_fill(0, 100, 'long'))); |
|
87
|
|
|
self::fail(); |
|
88
|
|
|
|
|
89
|
|
|
} catch (InvalidArgumentException $e) { |
|
90
|
|
|
self::assertSame('ReturnUrl can have maximum of 300 characters.', $e->getMessage()); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function testDescription(): void |
|
95
|
|
|
{ |
|
96
|
|
|
Validator::checkDescription('foo description'); |
|
97
|
|
|
|
|
98
|
|
|
try { |
|
99
|
|
|
Validator::checkDescription(implode(' ', array_fill(0, 60, 'very')) . ' long description'); |
|
100
|
|
|
self::fail(); |
|
101
|
|
|
|
|
102
|
|
|
} catch (InvalidArgumentException $e) { |
|
103
|
|
|
self::assertSame('Description can have maximum of 255 characters.', $e->getMessage()); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function testMerchantData(): void |
|
108
|
|
|
{ |
|
109
|
|
|
Validator::checkMerchantData('foo merchant data'); |
|
110
|
|
|
|
|
111
|
|
|
try { |
|
112
|
|
|
Validator::checkMerchantData(implode(' ', array_fill(0, 60, 'very')) . ' long merchantData'); |
|
113
|
|
|
self::fail(); |
|
114
|
|
|
|
|
115
|
|
|
} catch (InvalidArgumentException $e) { |
|
116
|
|
|
self::assertSame('MerchantData can have maximum of 255 characters in encoded state.', $e->getMessage()); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function testCustomerId(): void |
|
121
|
|
|
{ |
|
122
|
|
|
Validator::checkCustomerId('foo customerId'); |
|
123
|
|
|
|
|
124
|
|
|
try { |
|
125
|
|
|
Validator::checkCustomerId('very very very very very very very very long long long customerId'); |
|
126
|
|
|
self::fail(); |
|
127
|
|
|
|
|
128
|
|
|
} catch (InvalidArgumentException $e) { |
|
129
|
|
|
self::assertSame('CustomerId can have maximum of 50 characters.', $e->getMessage()); |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
public function testPayId(): void |
|
134
|
|
|
{ |
|
135
|
|
|
Validator::checkPayId('foo payId'); |
|
136
|
|
|
|
|
137
|
|
|
try { |
|
138
|
|
|
Validator::checkPayId('very long long payId'); |
|
139
|
|
|
self::fail(); |
|
140
|
|
|
|
|
141
|
|
|
} catch (InvalidArgumentException $e) { |
|
142
|
|
|
self::assertSame('PayId can have maximum of 15 characters.', $e->getMessage()); |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
public function testTtlSec(): void |
|
147
|
|
|
{ |
|
148
|
|
|
Validator::checkTtlSec(500); |
|
149
|
|
|
|
|
150
|
|
|
try { |
|
151
|
|
|
Validator::checkTtlSec(200); |
|
152
|
|
|
self::fail(); |
|
153
|
|
|
|
|
154
|
|
|
} catch (InvalidArgumentException $e) { |
|
155
|
|
|
self::assertSame('TTL sec is out of range (300 - 1800). Current value is 200.', $e->getMessage()); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
try { |
|
159
|
|
|
Validator::checkTtlSec(3000); |
|
160
|
|
|
self::fail(); |
|
161
|
|
|
|
|
162
|
|
|
} catch (InvalidArgumentException $e) { |
|
163
|
|
|
self::assertSame('TTL sec is out of range (300 - 1800). Current value is 3000.', $e->getMessage()); |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
} |
|
168
|
|
|
|