testPassWithEmptyStructureSerializesAsEmptyObject()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
1
<?php
2
3
namespace Passbook\Tests;
4
5
use DateTime;
6
use Passbook\Pass;
7
use Passbook\Pass\Nfc;
8
use Passbook\PassFactory;
9
use Passbook\Pass\Field;
10
use Passbook\Pass\Barcode;
11
use Passbook\Pass\Beacon;
12
use Passbook\Pass\Location;
13
use Passbook\Pass\Structure;
14
use Passbook\Type\BoardingPass;
15
use Passbook\Type\Coupon;
16
use Passbook\Type\EventTicket;
17
use Passbook\Type\Generic;
18
use Passbook\Type\StoreCard;
19
use PHPUnit\Framework\TestCase;
20
21
class PassTest extends TestCase
22
{
23
    /**
24
     * @var BoardingPass
25
     */
26
    protected $boardingPass;
27
28
    /**
29
     * @var Coupon
30
     */
31
    protected $coupon;
32
33
    /**
34
     * @var EventTicket
35
     */
36
    protected $eventTicket;
37
38
    /**
39
     * @var Generic
40
     */
41
    protected $generic;
42
43
    /**
44
     * @var StoreCard
45
     */
46
    protected $storeCard;
47
48
    /**
49
     * @var Pass
50
     */
51
    protected $pass;
52
53
    public static function setUpBeforeClass(): void
54
    {
55
        // Avoid warnings when system does not have timezone set
56
        date_default_timezone_set(date_default_timezone_get());
57
    }
58
59
    /**
60
     * Boarding Pass
61
     */
62
    public function testBoardingPass()
63
    {
64
        $boardingPass = new BoardingPass(uniqid(), 'SFO to JFK', BoardingPass::TYPE_AIR);
65
66
        // Set colors
67
        $boardingPass->setBackgroundColor('rgb(22, 55, 110)');
68
        $boardingPass->setForegroundColor('rgb(50, 91, 185)');
69
70
        // Logo text
71
        $boardingPass->setLogoText('Skyport Airways');
72
73
        // Relevant date
74
        $boardingPass->setRelevantDate(new DateTime());
75
76
        // Add location
77
        $location = new Location(-122.3748889, 37.6189722);
78
        $boardingPass->addLocation($location);
79
80
        // Create pass structure
81
        $structure = new Structure();
82
83
        // Add header field
84
        $header = new Field('gate', '23');
85
        $header->setLabel('GATE');
86
        $structure->addHeaderField($header);
87
88
        // Add primary fields
89
        $primary = new Field('depart', 'SFO');
90
        $primary->setLabel('SAN FRANCISCO');
91
        $structure->addPrimaryField($primary);
92
93
        $primary = new Field('arrive', 'JFK');
94
        $primary->setLabel('NEW YORK');
95
        $structure->addPrimaryField($primary);
96
97
        // Add secondary field
98
        $secondary = new Field('passenger', 'John Appleseed');
99
        $secondary->setLabel('PASSENGER');
100
        $structure->addSecondaryField($secondary);
101
102
        // Add auxiliary fields
103
        $auxiliary = new Field('boardingTime', '2:25 PM');
104
        $auxiliary->setLabel('DEPART');
105
        $structure->addAuxiliaryField($auxiliary);
106
107
        $auxiliary = new Field('flightNewName', '815');
108
        $auxiliary->setLabel('FLIGHT');
109
        $structure->addAuxiliaryField($auxiliary);
110
111
        $auxiliary = new Field('class', 'Coach');
112
        $auxiliary->setLabel('DESIG.');
113
        $structure->addAuxiliaryField($auxiliary);
114
115
        $auxiliary = new Field('date', '7/22');
116
        $auxiliary->setLabel('DATE');
117
        $structure->addAuxiliaryField($auxiliary);
118
119
        // Set pass structure
120
        $boardingPass->setStructure($structure);
121
122
        // Add barcode
123
        $barcode = new Barcode(Barcode::TYPE_PDF_417, 'SFOJFK JOHN APPLESEED LH451 2012-07-22T14:25-08:00');
124
        $boardingPass->setBarcode($barcode);
125
126
        $json = PassFactory::serialize($boardingPass);
127
        $array = json_decode($json, true);
128
129
        $this->assertArrayHasKey('transitType', $array[$boardingPass->getType()]);
130
    }
131
132
    /**
133
     * Store Card
134
     */
135
    public function testStoreCard()
136
    {
137
        $json = PassFactory::serialize($this->storeCard);
138
        $this->assertJson($json);
139
    }
140
141
    /**
142
     * Event Ticket
143
     */
144
    public function testEventTicket()
145
    {
146
        $this->eventTicket->setBackgroundColor('rgb(60, 65, 76)');
147
        $this->assertSame('rgb(60, 65, 76)', $this->eventTicket->getBackgroundColor());
148
        $this->eventTicket->setLogoText('Apple Inc.');
149
        $this->assertSame('Apple Inc.', $this->eventTicket->getLogoText());
150
151
        // Add location
152
        $location = new Location(59.33792, 18.06873);
153
        $this->eventTicket->addLocation($location);
154
155
        // Create pass structure
156
        $structure = new Structure();
157
158
        // Add primary field
159
        $primary = new Field('event', 'The Beat Goes On');
160
        $primary->setLabel('Event');
161
        $structure->addPrimaryField($primary);
162
163
        // Add secondary field
164
        $secondary = new Field('location', 'Moscone West');
165
        $secondary->setLabel('Location');
166
        $structure->addSecondaryField($secondary);
167
168
        // Add auxiliary field
169
        $auxiliary = new Field('datetime', '2013-04-15 @10:25');
170
        $auxiliary->setLabel('Date & Time');
171
        $structure->addAuxiliaryField($auxiliary);
172
173
        // Relevant date
174
        $this->eventTicket->setRelevantDate(new DateTime());
175
176
        // Set pass structure
177
        $this->eventTicket->setStructure($structure);
178
179
        // Set grouping
180
        $this->eventTicket->setGroupingIdentifier('group1');
181
182
        // Add barcode
183
        $barcode = new Barcode('PKBarcodeFormatQR', 'barcodeMessage');
184
        $this->eventTicket->setBarcode($barcode);
185
186
        $json = PassFactory::serialize($this->eventTicket);
187
        $array = json_decode($json, true);
188
189
        $this->assertArrayHasKey('eventTicket', $array);
190
        $this->assertArrayHasKey('locations', $array);
191
        $this->assertArrayHasKey('barcode', $array);
192
        $this->assertArrayHasKey('logoText', $array);
193
        $this->assertArrayHasKey('backgroundColor', $array);
194
        $this->assertArrayHasKey('eventTicket', $array);
195
        $this->assertArrayHasKey('relevantDate', $array);
196
        $this->assertArrayHasKey('groupingIdentifier', $array);
197
    }
198
199
    /**
200
     * Generic
201
     */
202
    public function testGeneric()
203
    {
204
        $this->generic->setBackgroundColor('rgb(60, 65, 76)');
205
        $this->assertSame('rgb(60, 65, 76)', $this->generic->getBackgroundColor());
206
        $this->generic->setLogoText('Apple Inc.');
207
        $this->assertSame('Apple Inc.', $this->generic->getLogoText());
208
209
        $this->generic
210
            ->setFormatVersion(1)
211
            ->setDescription('description');
212
213
        // Create pass structure
214
        $structure = new Structure();
215
216
        // Add primary field
217
        $primary = new Field('event', 'The Beat Goes On');
218
        $primary->setLabel('Event');
219
        $structure->addPrimaryField($primary);
220
221
        // Add back field
222
        $back = new Field('back', 'Hello World!');
223
        $back->setLabel('Location');
224
        $structure->addSecondaryField($back);
225
226
        // Add auxiliary field
227
        $auxiliary = new Field('datetime', '2014 Aug 1');
228
        $auxiliary->setLabel('Date & Time');
229
        $structure->addAuxiliaryField($auxiliary);
230
231
        // Set pass structure
232
        $this->generic->setStructure($structure);
233
234
        // Add beacon
235
        $beacon = new Beacon('abcdef01-2345-6789-abcd-ef0123456789');
236
        $this->generic->addBeacon($beacon);
237
238
        $json = PassFactory::serialize($this->generic);
239
        $array = json_decode($json, true);
240
241
        $this->assertArrayHasKey('beacons', $array);
242
        $this->assertArrayHasKey('generic', $array);
243
    }
244
245
    /**
246
     * Pass
247
     */
248
    public function testPass()
249
    {
250
        $this->pass
251
            ->setWebServiceURL('http://example.com')
252
            ->setForegroundColor('rgb(0, 255, 0)')
253
            ->setBackgroundColor('rgb(0, 255, 0)')
254
            ->setLabelColor('rgb(0, 255, 0)')
255
            ->setStripColor('rgb(0, 255, 0)')
256
            ->setAuthenticationToken('123')
257
            ->setGroupingIdentifier('group1')
258
            ->setType('generic')
259
            ->setSuppressStripShine(false)
260
            ->setAppLaunchURL('http://app.launch.url')
261
            ->addAssociatedStoreIdentifier(123);
262
263
        $properties = [
264
            'webServiceURL',
265
            'foregroundColor',
266
            'backgroundColor',
267
            'labelColor',
268
            'stripColor',
269
            'authenticationToken',
270
            'groupingIdentifier',
271
            'suppressStripShine',
272
            'associatedStoreIdentifiers',
273
            'appLaunchURL',
274
        ];
275
        $array = $this->pass->toArray();
276
        foreach ($properties as $property) {
277
            $this->assertTrue(isset($array[$property]));
278
        }
279
    }
280
281
    public function testPassWithEmptyStructureSerializesAsEmptyObject()
282
    {
283
        $this->storeCard->setStructure(new Structure());
284
        $array = $this->storeCard->toArray();
285
        self::assertTrue(is_object($array['storeCard']));
286
    }
287
288
    public function testSerialNumberConvertedToString()
289
    {
290
        $this->storeCard->setSerialNumber(0);
291
        $array = $this->storeCard->toArray();
292
        self::assertTrue(is_string($array['serialNumber']));
293
    }
294
295
    public function testAddingBarcode()
296
    {
297
        $barcode1 = new Barcode(Barcode::TYPE_QR, 'barcode 1');
298
        $barcode2 = new Barcode(Barcode::TYPE_CODE_128, 'barcode 2');
299
        $barcode3 = new Barcode(Barcode::TYPE_AZTEC, 'barcode 3');
300
301
        $this->storeCard->addBarcode($barcode1);
302
        self::assertEquals($barcode1, $this->storeCard->getBarcode());
303
304
        $this->storeCard->addBarcode($barcode2);
305
        self::assertEquals($barcode1, $this->storeCard->getBarcode());
306
307
        $this->storeCard->setBarcode($barcode3);
308
        self::assertEquals($barcode3, $this->storeCard->getBarcode());
309
        $barcodes = $this->storeCard->getBarcodes();
310
        self::assertEquals($barcode3, $barcodes[0]);
311
        self::assertEquals($barcode1, $barcodes[1]);
312
        self::assertEquals($barcode2, $barcodes[2]);
313
    }
314
315
    public function testAddingNfcTag()
316
    {
317
        // Add nfc
318
        $nfc = new Nfc('message', 'encryptionPublicKey');
319
        $this->pass->setNfc($nfc);
320
        $this->assertInstanceOf(Pass\NfcInterface::class, $this->pass->getNfc());
321
322
        $json = PassFactory::serialize($this->pass);
323
        $array = json_decode($json, true);
324
325
        $this->assertArrayHasKey('nfc', $array);
326
    }
327
328
    public function testStripColor()
329
    {
330
        // Coupon
331
        $this->coupon->setStripColor('rgb(0, 0, 0)');
332
        $this->assertSame('rgb(0, 0, 0)', $this->coupon->getStripColor());
333
334
        $json = PassFactory::serialize($this->coupon);
335
        $array = json_decode($json, true);
336
337
        $this->assertArrayHasKey('stripColor', $array);
338
        $this->assertSame('rgb(0, 0, 0)', $array['stripColor']);
339
340
        // Event Ticket
341
        $this->eventTicket->setStripColor('rgb(0, 0, 0)');
342
        $this->assertSame('rgb(0, 0, 0)', $this->eventTicket->getStripColor());
343
344
        $json = PassFactory::serialize($this->eventTicket);
345
        $array = json_decode($json, true);
346
347
        $this->assertArrayHasKey('stripColor', $array);
348
        $this->assertSame('rgb(0, 0, 0)', $array['stripColor']);
349
350
        // Generic
351
        $this->generic->setStripColor('rgb(0, 0, 0)');
352
        $this->assertSame('rgb(0, 0, 0)', $this->generic->getStripColor());
353
354
        $json = PassFactory::serialize($this->generic);
355
        $array = json_decode($json, true);
356
357
        $this->assertArrayHasKey('stripColor', $array);
358
        $this->assertSame('rgb(0, 0, 0)', $array['stripColor']);
359
360
        // Storecard
361
        $this->storeCard->setStripColor('rgb(0, 0, 0)');
362
        $this->assertSame('rgb(0, 0, 0)', $this->storeCard->getStripColor());
363
364
        $json = PassFactory::serialize($this->storeCard);
365
        $array = json_decode($json, true);
366
367
        $this->assertArrayHasKey('stripColor', $array);
368
        $this->assertSame('rgb(0, 0, 0)', $array['stripColor']);
369
370
        // Pass
371
        $this->pass->setStripColor('rgb(0, 0, 0)');
372
        $this->assertSame('rgb(0, 0, 0)', $this->pass->getStripColor());
373
374
        $json = PassFactory::serialize($this->pass);
375
        $array = json_decode($json, true);
376
377
        $this->assertArrayHasKey('stripColor', $array);
378
        $this->assertSame('rgb(0, 0, 0)', $array['stripColor']);
379
    }
380
381
    public function testMaxDistance()
382
    {
383
        $this->storeCard->setMaxDistance(100);
384
        self::assertEquals(100, $this->storeCard->getMaxDistance());
385
    }
386
387
    /**
388
     * {@inheritdoc}
389
     */
390
    protected function setUp(): void
391
    {
392
        $this->coupon = new Coupon(uniqid(), 'Lorem ipsum');
393
        $this->eventTicket = new EventTicket(uniqid(), 'Lorem ipsum');
394
        $this->generic = new Generic(uniqid(), 'Lorem ipsum');
395
        $this->storeCard = new StoreCard(uniqid(), 'Lorem ipsum');
396
        $this->pass = new Pass(uniqid(), 'Lorem ipsum');
397
    }
398
}
399