Completed
Push — master ( c103b4...a1e452 )
by
unknown
11s
created

PassValidatorTest::testPassFormatVersion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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