-
Notifications
You must be signed in to change notification settings - Fork 852
/
Copy pathStripeObjectTest.php
599 lines (503 loc) · 19 KB
/
StripeObjectTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
<?php
namespace Stripe;
/**
* @internal
* @covers \Stripe\StripeObject
*/
final class StripeObjectTest extends \Stripe\TestCase
{
use TestHelper;
/** @var \ReflectionMethod */
private $deepCopyReflector;
/** @var \ReflectionProperty */
private $optsReflector;
/** @var \ReflectionProperty */
private $retrieveOptionsReflector;
/**
* @before
*/
public function setUpReflectors()
{
// Sets up reflectors needed by some tests to access protected or
// private attributes.
// This is used to invoke the `deepCopy` protected function
$this->deepCopyReflector = new \ReflectionMethod(\Stripe\StripeObject::class, 'deepCopy');
$this->deepCopyReflector->setAccessible(true);
// This is used to access the `_opts` protected variable
$this->optsReflector = new \ReflectionProperty(\Stripe\StripeObject::class, '_opts');
$this->optsReflector->setAccessible(true);
// This is used to access the `_retrieveOptions` protected variable
$this->retrieveOptionsReflector = new \ReflectionProperty(\Stripe\StripeObject::class, '_retrieveOptions');
$this->retrieveOptionsReflector->setAccessible(true);
}
public function testArrayAccessorsSemantics()
{
/** @var mixed $s */
$s = new StripeObject();
$s['foo'] = 'a';
static::assertSame($s['foo'], 'a');
static::assertTrue(isset($s['foo']));
unset($s['foo']);
static::assertFalse(isset($s['foo']));
}
public function testNormalAccessorsSemantics()
{
/** @var mixed $s */
$s = new StripeObject();
$s->foo = 'a';
static::assertSame($s->foo, 'a');
static::assertTrue(isset($s->foo));
$s->foo = null;
static::assertFalse(isset($s->foo));
}
public function testArrayAccessorsMatchNormalAccessors()
{
/** @var mixed $s */
$s = new StripeObject();
$s->foo = 'a';
static::assertSame($s['foo'], 'a');
$s['bar'] = 'b';
static::assertSame($s->bar, 'b');
}
public function testCount()
{
/** @var mixed $s */
$s = new StripeObject();
static::assertCount(0, $s);
$s['key1'] = 'value1';
static::assertCount(1, $s);
$s['key2'] = 'value2';
static::assertCount(2, $s);
unset($s['key1']);
static::assertCount(1, $s);
}
public function testKeys()
{
/** @var mixed $s */
$s = new StripeObject();
$s->foo = 'bar';
static::assertSame($s->keys(), ['foo']);
}
public function testValues()
{
/** @var mixed $s */
$s = new StripeObject();
$s->foo = 'bar';
static::assertSame($s->values(), ['bar']);
}
public function testToArray()
{
$array = [
'foo' => 'a',
'list' => [1, 2, 3],
'null' => null,
'metadata' => [
'key' => 'value',
1 => 'one',
],
];
$s = StripeObject::constructFrom($array);
$converted = $s->toArray();
static::compatAssertIsArray($converted);
static::assertSame($array, $converted);
}
public function testToArrayRecursive()
{
// deep nested associative array (when contained in an indexed array)
// or StripeObject
$nestedArray = ['id' => 7, 'foo' => 'bar'];
$nested = StripeObject::constructFrom($nestedArray);
$obj = StripeObject::constructFrom([
'id' => 1,
// simple associative array that contains a StripeObject to help us
// test deep recursion
'nested' => ['object' => 'list', 'data' => [$nested]],
'list' => [$nested],
]);
$expected = [
'id' => 1,
'nested' => ['object' => 'list', 'data' => [$nestedArray]],
'list' => [$nestedArray],
];
static::assertSame($expected, $obj->toArray());
}
public function testNonexistentProperty()
{
$capture = \tmpfile();
$origErrorLog = \ini_set('error_log', \stream_get_meta_data($capture)['uri']);
try {
/** @var mixed $s */
$s = new StripeObject();
static::assertNull($s->nonexistent);
static::compatAssertMatchesRegularExpression(
'/Stripe Notice: Undefined property of Stripe\\\\StripeObject instance: nonexistent/',
\stream_get_contents($capture)
);
} finally {
\ini_set('error_log', $origErrorLog);
\fclose($capture);
}
}
public function testPropertyDoesNotExists()
{
/** @var mixed $s */
$s = new StripeObject();
static::assertNull($s['nonexistent']);
}
public function testJsonEncode()
{
/** @var mixed $s */
$s = new StripeObject();
$s->foo = 'a';
static::assertSame('{"foo":"a"}', \json_encode($s));
}
public function testToString()
{
/** @var mixed $s */
$s = new StripeObject();
$s->foo = 'a';
$string = (string) $s;
$expected = <<<'EOS'
Stripe\StripeObject JSON: {
"foo": "a"
}
EOS;
static::assertSame($expected, $string);
}
public function testReplaceNewNestedUpdatable()
{
/** @var mixed $s */
$s = new StripeObject();
$s->metadata = ['bar'];
static::assertSame($s->metadata, ['bar']);
$s->metadata = ['baz', 'qux'];
static::assertSame($s->metadata, ['baz', 'qux']);
}
public function testSetPermanentAttribute()
{
$this->expectException(\InvalidArgumentException::class);
/** @var mixed $s */
$s = new StripeObject();
$s->id = 'abc_123';
}
public function testSetEmptyStringValue()
{
$this->expectException(\InvalidArgumentException::class);
/** @var mixed $s */
$s = new StripeObject();
$s->foo = '';
}
public function testSerializeParametersOnEmptyObject()
{
$obj = StripeObject::constructFrom([]);
static::assertSame([], $obj->serializeParameters());
}
public function testSerializeParametersOnNewObjectWithSubObject()
{
/** @var mixed $obj */
$obj = new StripeObject();
$obj->metadata = ['foo' => 'bar'];
static::assertSame(['metadata' => ['foo' => 'bar']], $obj->serializeParameters());
}
public function testSerializeParametersOnBasicObject()
{
$obj = StripeObject::constructFrom(['foo' => null]);
$obj->updateAttributes(['foo' => 'bar']);
static::assertSame(['foo' => 'bar'], $obj->serializeParameters());
}
public function testSerializeParametersOnMoreComplexObject()
{
/** @var mixed $obj */
$obj = StripeObject::constructFrom([
'foo' => StripeObject::constructFrom([
'bar' => null,
'baz' => null,
]),
]);
$obj->foo->bar = 'newbar';
static::assertSame(['foo' => ['bar' => 'newbar']], $obj->serializeParameters());
}
public function testSerializeParametersOnArray()
{
/** @var mixed $obj */
$obj = StripeObject::constructFrom([
'foo' => null,
]);
$obj->foo = ['new-value'];
static::assertSame(['foo' => ['new-value']], $obj->serializeParameters());
}
public function testSerializeParametersOnArrayThatShortens()
{
/** @var mixed $obj */
$obj = StripeObject::constructFrom([
'foo' => ['0-index', '1-index', '2-index'],
]);
$obj->foo = ['new-value'];
static::assertSame(['foo' => ['new-value']], $obj->serializeParameters());
}
public function testSerializeParametersOnArrayThatLengthens()
{
/** @var mixed $obj */
$obj = StripeObject::constructFrom([
'foo' => ['0-index', '1-index', '2-index'],
]);
$obj->foo = \array_fill(0, 4, 'new-value');
static::assertSame(['foo' => \array_fill(0, 4, 'new-value')], $obj->serializeParameters());
}
public function testSerializeParametersOnArrayOfHashes()
{
/** @var mixed $obj */
$obj = StripeObject::constructFrom(['foo' => null]);
$obj->foo = [
StripeObject::constructFrom(['bar' => null]),
];
/** @var mixed $first */
$first = $obj->foo[0];
$first->bar = 'baz';
static::assertSame(['foo' => [['bar' => 'baz']]], $obj->serializeParameters());
}
public function testSerializeParametersDoesNotIncludeUnchangedValues()
{
/** @var mixed $obj */
$obj = StripeObject::constructFrom([
'foo' => null,
]);
static::assertSame([], $obj->serializeParameters());
}
public function testSerializeParametersOnUnchangedArray()
{
/** @var mixed $obj */
$obj = StripeObject::constructFrom([
'foo' => ['0-index', '1-index', '2-index'],
]);
$obj->foo = ['0-index', '1-index', '2-index'];
static::assertSame([], $obj->serializeParameters());
}
public function testSerializeParametersWithStripeObject()
{
/** @var mixed $obj */
$obj = StripeObject::constructFrom([]);
$obj->metadata = StripeObject::constructFrom(['foo' => 'bar']);
$serialized = $obj->serializeParameters();
static::assertSame(['foo' => 'bar'], $serialized['metadata']);
}
public function testSerializeParametersOnReplacedStripeObject()
{
/** @var mixed $obj */
$obj = StripeObject::constructFrom([
'source' => StripeObject::constructFrom(['bar' => 'foo']),
]);
$obj->source = StripeObject::constructFrom(['baz' => 'foo']);
$serialized = $obj->serializeParameters();
static::assertSame(['baz' => 'foo'], $serialized['source']);
}
public function testSerializeParametersOnReplacedStripeObjectWhichIsMetadata()
{
/** @var mixed $obj */
$obj = StripeObject::constructFrom([
'metadata' => StripeObject::constructFrom(['bar' => 'foo']),
]);
$obj->metadata = StripeObject::constructFrom(['baz' => 'foo']);
$serialized = $obj->serializeParameters();
static::assertSame(['bar' => '', 'baz' => 'foo'], $serialized['metadata']);
}
public function testSerializeParametersOnArrayOfStripeObjects()
{
/** @var mixed $obj */
$obj = StripeObject::constructFrom([]);
$obj->metadata = [
StripeObject::constructFrom(['foo' => 'bar']),
];
$serialized = $obj->serializeParameters();
static::assertSame([['foo' => 'bar']], $serialized['metadata']);
}
public function testSerializeParametersOnSetApiResource()
{
$customer = Customer::constructFrom(['id' => 'cus_123']);
/** @var mixed $obj */
$obj = StripeObject::constructFrom([]);
// the key here is that the property is set explicitly (and therefore
// marked as unsaved), which is why it gets included below
$obj->customer = $customer;
$serialized = $obj->serializeParameters();
static::assertSame(['customer' => $customer], $serialized);
}
public function testSerializeParametersOnNotSetApiResource()
{
$customer = Customer::constructFrom(['id' => 'cus_123']);
$obj = StripeObject::constructFrom(['customer' => $customer]);
$serialized = $obj->serializeParameters();
static::assertSame([], $serialized);
}
public function testSerializeParametersOnApiResourceFlaggedWithSaveWithParent()
{
$customer = Customer::constructFrom(['id' => 'cus_123']);
$customer->saveWithParent = true;
$obj = StripeObject::constructFrom(['customer' => $customer]);
$serialized = $obj->serializeParameters();
static::assertSame(['customer' => []], $serialized);
}
public function testSerializeParametersRaisesExceotionOnOtherEmbeddedApiResources()
{
// This customer doesn't have an ID and therefore the library doesn't know
// what to do with it and throws an InvalidArgumentException because it's
// probably not what the user expected to happen.
$customer = Customer::constructFrom([]);
/** @var mixed $obj */
$obj = StripeObject::constructFrom([]);
$obj->customer = $customer;
try {
$serialized = $obj->serializeParameters();
static::fail('Did not raise error');
} catch (\InvalidArgumentException $e) {
static::assertSame(
'Cannot save property `customer` containing an API resource of type Stripe\\Customer. ' .
"It doesn't appear to be persisted and is not marked as `saveWithParent`.",
$e->getMessage()
);
} catch (\Exception $e) {
static::fail('Unexpected exception: ' . \get_class($e));
}
}
public function testSerializeParametersForce()
{
$obj = StripeObject::constructFrom([
'id' => 'id',
'metadata' => StripeObject::constructFrom([
'bar' => 'foo',
]),
]);
$serialized = $obj->serializeParameters(true);
static::assertSame(['id' => 'id', 'metadata' => ['bar' => 'foo']], $serialized);
}
public function testDirty()
{
$obj = StripeObject::constructFrom([
'id' => 'id',
'metadata' => StripeObject::constructFrom([
'bar' => 'foo',
]),
]);
// note that `$force` and `dirty()` are for different things, but are
// functionally equivalent
$obj->dirty();
$serialized = $obj->serializeParameters();
static::assertSame(['id' => 'id', 'metadata' => ['bar' => 'foo']], $serialized);
}
public function testDeepCopy()
{
$opts = [
'api_base' => Stripe::$apiBase,
'api_key' => 'apikey',
];
$values = [
'id' => 1,
'name' => 'Stripe',
'arr' => [
StripeObject::constructFrom(['id' => 'index0'], $opts),
'index1',
2,
],
'map' => [
'0' => StripeObject::constructFrom(['id' => 'index0'], $opts),
'1' => 'index1',
'2' => 2,
],
];
$copyValues = $this->deepCopyReflector->invoke(null, $values);
// we can't compare the hashes directly because they have embedded
// objects which are different from each other
static::assertSame($values['id'], $copyValues['id']);
static::assertSame($values['name'], $copyValues['name']);
static::assertSame(\count($values['arr']), \count($copyValues['arr']));
// internal values of the copied StripeObject should be the same,
// but the object itself should be new (hence the assertNotSame)
static::assertSame($values['arr'][0]['id'], $copyValues['arr'][0]['id']);
static::assertNotSame($values['arr'][0], $copyValues['arr'][0]);
// likewise, the Util\RequestOptions instance in _opts should have
// copied values but be a new instance
static::assertSame(
$this->optsReflector->getValue($values['arr'][0])->apiKey,
$this->optsReflector->getValue($copyValues['arr'][0])->apiKey
);
static::assertNotSame(
$this->optsReflector->getValue($values['arr'][0]),
$this->optsReflector->getValue($copyValues['arr'][0])
);
// scalars however, can be compared
static::assertSame($values['arr'][1], $copyValues['arr'][1]);
static::assertSame($values['arr'][2], $copyValues['arr'][2]);
// and a similar story with the hash
static::assertSame($values['map']['0']['id'], $copyValues['map']['0']['id']);
static::assertNotSame($values['map']['0'], $copyValues['map']['0']);
static::assertNotSame(
$this->optsReflector->getValue($values['arr'][0]),
$this->optsReflector->getValue($copyValues['arr'][0])
);
static::assertSame(
$this->optsReflector->getValue($values['map']['0'])->apiKey,
$this->optsReflector->getValue($copyValues['map']['0'])->apiKey
);
static::assertNotSame(
$this->optsReflector->getValue($values['map']['0']),
$this->optsReflector->getValue($copyValues['map']['0'])
);
static::assertSame($values['map']['1'], $copyValues['map']['1']);
static::assertSame($values['map']['2'], $copyValues['map']['2']);
}
public function testDeepCopyMaintainClass()
{
$charge = Charge::constructFrom(['id' => 1], null);
$copyCharge = $this->deepCopyReflector->invoke(null, $charge);
static::assertSame(\get_class($charge), \get_class($copyCharge));
}
public function testIsDeleted()
{
$obj = StripeObject::constructFrom([]);
static::assertFalse($obj->isDeleted());
$obj = StripeObject::constructFrom(['deleted' => false]);
static::assertFalse($obj->isDeleted());
$obj = StripeObject::constructFrom(['deleted' => true]);
static::assertTrue($obj->isDeleted());
}
public function testConstructorIdPassing()
{
$obj = new StripeObject(['id' => 'id_foo', 'other' => 'bar']);
static::assertSame('id_foo', $obj->id);
static::assertSame(['other' => 'bar'], $this->retrieveOptionsReflector->getValue($obj));
$obj = new StripeObject('id_foo');
static::assertSame('id_foo', $obj->id);
static::assertSame([], $this->retrieveOptionsReflector->getValue($obj));
$obj = new StripeObject(['id' => 'id_foo']);
static::assertSame('id_foo', $obj->id);
static::assertSame([], $this->retrieveOptionsReflector->getValue($obj));
$obj = new StripeObject(['id' => ['foo' => 'bar']]);
static::assertSame(['foo' => 'bar'], $obj->id);
static::assertSame([], $this->retrieveOptionsReflector->getValue($obj));
}
public function testConstructFromIdPassing()
{
$obj = StripeObject::constructFrom(['inner' => ['id' => ['foo' => 'bar']]]);
static::assertSame(['foo' => 'bar'], $obj['inner']->id->toArray());
static::assertSame([], $this->retrieveOptionsReflector->getValue($obj));
}
public function testDeserializeEmptyMetadata()
{
/** @var mixed $obj */
$obj = StripeObject::constructFrom([
'metadata' => [],
]);
static::assertInstanceOf(\Stripe\StripeObject::class, $obj->metadata);
}
public function testDeserializeMetadataWithKeyNamedMetadata()
{
/** @var mixed $obj */
$obj = StripeObject::constructFrom([
'metadata' => ['metadata' => 'value'],
]);
static::assertInstanceOf(\Stripe\StripeObject::class, $obj->metadata);
/** @var mixed $inner */
$inner = $obj->metadata;
static::assertSame('value', $inner->metadata);
}
}