Passed
Pull Request — master (#103)
by Razvan
15:39 queued 12:47
created

PassTest::testBoardingPass()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 69

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 69
rs 8.6763
c 0
b 0
f 0
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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