PassValidatorTest::testPassBarcodeFormat()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 13
rs 9.9666
1
<?php
2
3
namespace Passbook\Tests;
4
5
use Passbook\Pass;
6
use Passbook\Pass\Barcode;
7
use Passbook\Pass\Beacon;
8
use Passbook\Pass\Image;
9
use Passbook\Pass\Location;
10
use Passbook\PassValidator;
11
use Passbook\Type\Generic;
12
use PHPUnit\Framework\TestCase;
13
14
class PassValidatorTest extends TestCase
15
{
16
    protected const SERIAL_NUMBER = '123';
17
    protected const DESCRIPTION = 'description';
18
19
    /**
20
     * @var Pass
21
     */
22
    private $pass;
23
24
    public function testPassWithoutDescription()
25
    {
26
        $this->assertFails(new Generic(self::SERIAL_NUMBER, ''), PassValidator::DESCRIPTION_REQUIRED);
27
        $this->assertFails(new Generic(self::SERIAL_NUMBER, null), PassValidator::DESCRIPTION_REQUIRED);
28
        $this->assertPasses(new Generic(self::SERIAL_NUMBER, '0'), PassValidator::DESCRIPTION_REQUIRED);
29
    }
30
31
    public function testPassWithoutSerialNumber()
32
    {
33
        $this->assertFails(new Generic('', self::DESCRIPTION), PassValidator::SERIAL_NUMBER_REQUIRED);
34
        $this->assertFails(new Generic(null, self::DESCRIPTION), PassValidator::SERIAL_NUMBER_REQUIRED);
35
        $this->assertPasses(new Generic('0', self::DESCRIPTION), PassValidator::SERIAL_NUMBER_REQUIRED);
36
    }
37
38
    public function testPassFormatVersion()
39
    {
40
        $this->assertPasses($this->pass, PassValidator::FORMAT_VERSION_REQUIRED);
41
        $this->pass->setFormatVersion('');
42
        $this->assertFails($this->pass, PassValidator::FORMAT_VERSION_REQUIRED);
43
        $this->pass->setFormatVersion(null);
44
        $this->assertFails($this->pass, PassValidator::FORMAT_VERSION_REQUIRED);
45
        $this->pass->setFormatVersion('0');
46
        $this->assertFails($this->pass, PassValidator::FORMAT_VERSION_REQUIRED);
47
    }
48
49
    public function testPassWithoutOrganizationName()
50
    {
51
        $this->pass->setOrganizationName('');
52
        $this->assertFails($this->pass, PassValidator::ORGANIZATION_NAME_REQUIRED);
53
        $this->pass->setOrganizationName(null);
54
        $this->assertFails($this->pass, PassValidator::ORGANIZATION_NAME_REQUIRED);
55
        $this->pass->setOrganizationName('0');
56
        $this->assertPasses($this->pass, PassValidator::ORGANIZATION_NAME_REQUIRED);
57
    }
58
59
    public function testPassWithoutPassTypeIdentifier()
60
    {
61
        $this->pass->setPassTypeIdentifier('');
62
        $this->assertFails($this->pass, PassValidator::PASS_TYPE_IDENTIFIER_REQUIRED);
63
        $this->pass->setPassTypeIdentifier(null);
64
        $this->assertFails($this->pass, PassValidator::PASS_TYPE_IDENTIFIER_REQUIRED);
65
        $this->pass->setPassTypeIdentifier('0');
66
        $this->assertPasses($this->pass, PassValidator::PASS_TYPE_IDENTIFIER_REQUIRED);
67
    }
68
69
    public function testPassWithoutTeamIdentifier()
70
    {
71
        $this->pass->setTeamIdentifier('');
72
        $this->assertFails($this->pass, PassValidator::TEAM_IDENTIFIER_REQUIRED);
73
        $this->pass->setTeamIdentifier(null);
74
        $this->assertFails($this->pass, PassValidator::TEAM_IDENTIFIER_REQUIRED);
75
        $this->pass->setTeamIdentifier('0');
76
        $this->assertPasses($this->pass, PassValidator::TEAM_IDENTIFIER_REQUIRED);
77
    }
78
79
    public function testPassBarcodeFormat()
80
    {
81
        // First test pass without barcode passes
82
        $this->assertPasses($this->pass, PassValidator::BARCODE_FORMAT_INVALID);
83
        $this->pass->setBarcode(new Barcode(Barcode::TYPE_CODE_128, 'message'));
84
        $this->assertPasses($this->pass, PassValidator::BARCODE_FORMAT_INVALID);
85
86
        $this->pass->setBarcode(new Barcode('', 'message'));
87
        $this->assertFails($this->pass, PassValidator::BARCODE_FORMAT_INVALID);
88
        $this->pass->setBarcode(new Barcode(null, 'message'));
89
        $this->assertFails($this->pass, PassValidator::BARCODE_FORMAT_INVALID);
90
        $this->pass->setBarcode(new Barcode('invalid format', 'message'));
91
        $this->assertFails($this->pass, PassValidator::BARCODE_FORMAT_INVALID);
92
    }
93
94
    public function testPassBarcodeMessage()
95
    {
96
        // Test first before barcode is added
97
        $this->assertPasses($this->pass, PassValidator::BARCODE_MESSAGE_INVALID);
98
        $this->pass->setBarcode(new Barcode(Barcode::TYPE_QR, 'message'));
99
        $this->assertPasses($this->pass, PassValidator::BARCODE_MESSAGE_INVALID);
100
        $this->pass->setBarcode(new Barcode(Barcode::TYPE_QR, ''));
101
        $this->assertPasses($this->pass, PassValidator::BARCODE_MESSAGE_INVALID);
102
103
        $this->pass->setBarcode(new Barcode('', null));
104
        $this->assertFails($this->pass, PassValidator::BARCODE_FORMAT_INVALID);
105
        $this->pass->setBarcode(new Barcode(null, 0));
106
        $this->assertFails($this->pass, PassValidator::BARCODE_FORMAT_INVALID);
107
        $this->pass->setBarcode(new Barcode(null, 123));
108
        $this->assertFails($this->pass, PassValidator::BARCODE_FORMAT_INVALID);
109
    }
110
111
    public function testPassLocation()
112
    {
113
        $this->assertPasses($this->pass, PassValidator::LOCATION_LONGITUDE_REQUIRED);
114
        $this->assertPasses($this->pass, PassValidator::LOCATION_LATITUDE_REQUIRED);
115
        $this->assertPasses($this->pass, PassValidator::LOCATION_ALTITUDE_INVALID);
116
117
        $location = new Location(0, 0);
118
        $this->pass->addLocation($location);
119
        $this->assertPasses($this->pass, PassValidator::LOCATION_LONGITUDE_REQUIRED);
120
        $this->assertPasses($this->pass, PassValidator::LOCATION_LATITUDE_REQUIRED);
121
        $this->assertPasses($this->pass, PassValidator::LOCATION_ALTITUDE_INVALID);
122
123
        $location->setLatitude(null);
124
        $this->assertFails($this->pass, PassValidator::LOCATION_LATITUDE_REQUIRED);
125
        $location->setLatitude('');
126
        $this->assertFails($this->pass, PassValidator::LOCATION_LATITUDE_REQUIRED);
127
        $location->setLatitude('foo');
128
        $this->assertFails($this->pass, PassValidator::LOCATION_LATITUDE_INVALID);
129
130
        $location->setLatitude(0);
131
        $location->setLongitude(null);
132
        $this->assertFails($this->pass, PassValidator::LOCATION_LONGITUDE_REQUIRED);
133
        $location->setLongitude('');
134
        $this->assertFails($this->pass, PassValidator::LOCATION_LONGITUDE_REQUIRED);
135
        $location->setLongitude('foo');
136
        $this->assertFails($this->pass, PassValidator::LOCATION_LONGITUDE_INVALID);
137
138
        $location->setLongitude(0);
139
        $location->setAltitude(0);
140
        $this->assertPasses($this->pass, PassValidator::LOCATION_ALTITUDE_INVALID);
141
142
        $location->setAltitude('');
143
        $this->assertFails($this->pass, PassValidator::LOCATION_ALTITUDE_INVALID);
144
        $location->setAltitude('foo');
145
        $this->assertFails($this->pass, PassValidator::LOCATION_ALTITUDE_INVALID);
146
    }
147
148
    public function testPassBeacon()
149
    {
150
        $this->assertPasses($this->pass, PassValidator::BEACON_PROXIMITY_UUID_REQUIRED);
151
        $this->assertPasses($this->pass, PassValidator::BEACON_MAJOR_INVALID);
152
        $this->assertPasses($this->pass, PassValidator::BEACON_MINOR_INVALID);
153
154
        $beacon = new Beacon('2b4fcf51-4eaa-446d-b24e-4d1b437f3840');
155
        $this->pass->addBeacon($beacon);
156
        $this->assertPasses($this->pass, PassValidator::BEACON_PROXIMITY_UUID_REQUIRED);
157
        $this->assertPasses($this->pass, PassValidator::BEACON_MAJOR_INVALID);
158
        $this->assertPasses($this->pass, PassValidator::BEACON_MINOR_INVALID);
159
160
        $beacon->setMajor('');
161
        $this->assertFails($this->pass, PassValidator::BEACON_MAJOR_INVALID);
162
        $beacon->setMajor('foo');
163
        $this->assertFails($this->pass, PassValidator::BEACON_MAJOR_INVALID);
164
        $beacon->setMajor(-1);
165
        $this->assertFails($this->pass, PassValidator::BEACON_MAJOR_INVALID);
166
        $beacon->setMajor(65536);
167
        $this->assertFails($this->pass, PassValidator::BEACON_MAJOR_INVALID);
168
169
        $beacon->setMajor(0);
170
        $beacon->setMinor('');
171
        $this->assertFails($this->pass, PassValidator::BEACON_MINOR_INVALID);
172
        $beacon->setMinor('foo');
173
        $this->assertFails($this->pass, PassValidator::BEACON_MINOR_INVALID);
174
        $beacon->setMinor(-1);
175
        $this->assertFails($this->pass, PassValidator::BEACON_MINOR_INVALID);
176
        $beacon->setMinor(65536);
177
        $this->assertFails($this->pass, PassValidator::BEACON_MINOR_INVALID);
178
    }
179
180
    public function testWebServiceAuthenticationToken()
181
    {
182
        $this->assertPasses($this->pass, PassValidator::WEB_SERVICE_URL_INVALID);
183
        $this->assertPasses($this->pass, PassValidator::WEB_SERVICE_AUTHENTICATION_TOKEN_INVALID);
184
185
        $this->pass->setWebServiceURL('');
186
        $this->assertFails($this->pass, PassValidator::WEB_SERVICE_URL_INVALID);
187
188
        $this->pass->setWebServiceURL('https://example.com');
189
        $this->assertFails($this->pass, PassValidator::WEB_SERVICE_AUTHENTICATION_TOKEN_REQUIRED);
190
191
        $this->pass->setAuthenticationToken('1234567890abcde');
192
        $this->assertFails($this->pass, PassValidator::WEB_SERVICE_AUTHENTICATION_TOKEN_INVALID);
193
    }
194
195
    public function testPassWithoutIcon()
196
    {
197
        self::assertArrayNotHasKey('icon', $this->pass->getImages(), 'pass must not have an icon for test to be valid');
198
        $this->assertFails($this->pass, PassValidator::ICON_REQUIRED);
199
200
        $icon = new Image(__DIR__ . '/../../img/icon.png', 'icon');
201
        $this->pass->addImage($icon);
202
        $this->assertPasses($this->pass, PassValidator::ICON_REQUIRED);
203
    }
204
205
    public function testPassAssociatedStoreIdentifiers()
206
    {
207
        $this->assertPasses($this->pass, PassValidator::ASSOCIATED_STORE_IDENTIFIER_INVALID);
208
209
        $this->pass->setAppLaunchURL('url');
210
        $this->assertFails($this->pass, PassValidator::ASSOCIATED_STORE_IDENTIFIER_REQUIRED);
211
212
        $this->pass->addAssociatedStoreIdentifier(123);
213
        $this->assertPasses($this->pass, PassValidator::ASSOCIATED_STORE_IDENTIFIER_INVALID);
214
215
        $this->pass->addAssociatedStoreIdentifier('not an integer');
216
        $this->assertFails($this->pass, PassValidator::ASSOCIATED_STORE_IDENTIFIER_INVALID);
217
    }
218
219
    public function testPassImageType()
220
    {
221
        $this->assertPasses($this->pass, PassValidator::IMAGE_TYPE_INVALID);
222
223
        $png = new Image(__DIR__ . '/../../img/icon.png', 'icon');
224
        $this->pass->addImage($png);
225
        $this->assertPasses($this->pass, PassValidator::IMAGE_TYPE_INVALID);
226
227
        $png = new Image(__DIR__ . '/../../img/[email protected]', 'icon');
228
        $this->pass->addImage($png);
229
        $this->assertPasses($this->pass, PassValidator::IMAGE_TYPE_INVALID);
230
231
        $jpg = new Image(__DIR__ . '/../../img/icon.jpg', 'icon');
232
        $this->pass->addImage($jpg);
233
        $this->assertFails($this->pass, PassValidator::IMAGE_TYPE_INVALID);
234
    }
235
236
    public function testGroupingIdentity()
237
    {
238
        $this->pass->setType('boardingPass');
239
        $this->pass->setGroupingIdentifier('group1');
240
        $this->assertPasses($this->pass, PassValidator::GROUPING_IDENTITY_INVALID);
241
242
        $this->pass->setType('eventTicket');
243
        $this->pass->setGroupingIdentifier('group1');
244
        $this->assertPasses($this->pass, PassValidator::GROUPING_IDENTITY_INVALID);
245
246
        $this->pass->setType('coupon');
247
        $this->pass->setGroupingIdentifier('group1');
248
        $this->assertFails($this->pass, PassValidator::GROUPING_IDENTITY_INVALID);
249
250
        $this->pass->setType('storeCard');
251
        $this->pass->setGroupingIdentifier('group1');
252
        $this->assertFails($this->pass, PassValidator::GROUPING_IDENTITY_INVALID);
253
    }
254
255
    private function assertFails($pass, $expectedError)
256
    {
257
        $validator = new PassValidator();
258
        self::assertFalse($validator->validate($pass));
259
        self::assertContains($expectedError, $validator->getErrors());
260
    }
261
262
    private function assertPasses($pass, $unexpectedError)
263
    {
264
        $validator = new PassValidator();
265
        $validator->validate($pass);
266
        self::assertNotContains($unexpectedError, $validator->getErrors());
267
    }
268
269
    protected function setUp(): void
270
    {
271
        $this->pass = new Generic(self::SERIAL_NUMBER, self::DESCRIPTION);
272
    }
273
}
274