Completed
Push — master ( 07cf6a...9e5240 )
by
unknown
02:11
created

PassTest::testEventTicket()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 50
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

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