Passed
Push — master ( a94b97...2fde7e )
by Razvan
03:25
created

PassTest::testAddingNfcTag()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 11
rs 10
c 1
b 0
f 1
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
            ->setAuthenticationToken('123')
256
            ->setGroupingIdentifier('group1')
257
            ->setType('generic')
258
            ->setSuppressStripShine(false)
259
            ->setAppLaunchURL('http://app.launch.url')
260
            ->addAssociatedStoreIdentifier(123);
261
262
        $properties = [
263
            'webServiceURL',
264
            'foregroundColor',
265
            'backgroundColor',
266
            'labelColor',
267
            'authenticationToken',
268
            'groupingIdentifier',
269
            'suppressStripShine',
270
            'associatedStoreIdentifiers',
271
            'appLaunchURL',
272
        ];
273
        $array = $this->pass->toArray();
274
        foreach ($properties as $property) {
275
            $this->assertTrue(isset($array[$property]));
276
        }
277
    }
278
279
    public function testPassWithEmptyStructureSerializesAsEmptyObject()
280
    {
281
        $this->storeCard->setStructure(new Structure());
282
        $array = $this->storeCard->toArray();
283
        self::assertTrue(is_object($array['storeCard']));
284
    }
285
286
    public function testSerialNumberConvertedToString()
287
    {
288
        $this->storeCard->setSerialNumber(0);
289
        $array = $this->storeCard->toArray();
290
        self::assertTrue(is_string($array['serialNumber']));
291
    }
292
293
    public function testAddingBarcode()
294
    {
295
        $barcode1 = new Barcode(Barcode::TYPE_QR, 'barcode 1');
296
        $barcode2 = new Barcode(Barcode::TYPE_CODE_128, 'barcode 2');
297
        $barcode3 = new Barcode(Barcode::TYPE_AZTEC, 'barcode 3');
298
299
        $this->storeCard->addBarcode($barcode1);
300
        self::assertEquals($barcode1, $this->storeCard->getBarcode());
301
302
        $this->storeCard->addBarcode($barcode2);
303
        self::assertEquals($barcode1, $this->storeCard->getBarcode());
304
305
        $this->storeCard->setBarcode($barcode3);
306
        self::assertEquals($barcode3, $this->storeCard->getBarcode());
307
        $barcodes = $this->storeCard->getBarcodes();
308
        self::assertEquals($barcode3, $barcodes[0]);
309
        self::assertEquals($barcode1, $barcodes[1]);
310
        self::assertEquals($barcode2, $barcodes[2]);
311
    }
312
313
    public function testAddingNfcTag()
314
    {
315
        // Add nfc
316
        $nfc = new Nfc('message', 'encryptionPublicKey');
317
        $this->pass->setNfc($nfc);
318
        $this->assertInstanceOf(Pass\NfcInterface::class, $this->pass->getNfc());
319
320
        $json = PassFactory::serialize($this->pass);
321
        $array = json_decode($json, true);
322
323
        $this->assertArrayHasKey('nfc', $array);
324
    }
325
326
    public function testMaxDistance()
327
    {
328
        $this->storeCard->setMaxDistance(100);
329
        self::assertEquals(100, $this->storeCard->getMaxDistance());
330
    }
331
332
    /**
333
     * {@inheritdoc}
334
     */
335
    protected function setUp(): void
336
    {
337
        $this->coupon = new Coupon(uniqid(), 'Lorem ipsum');
338
        $this->eventTicket = new EventTicket(uniqid(), 'Lorem ipsum');
339
        $this->generic = new Generic(uniqid(), 'Lorem ipsum');
340
        $this->storeCard = new StoreCard(uniqid(), 'Lorem ipsum');
341
        $this->pass = new Pass(uniqid(), 'Lorem ipsum');
342
    }
343
}
344