1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: lenovo |
5
|
|
|
* Date: 6/17/2018 |
6
|
|
|
* Time: 9:41 AM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace TimSDK\Tests\Foundation; |
10
|
|
|
|
11
|
|
|
use TimSDK\Foundation\ResponseBag; |
12
|
|
|
use TimSDK\Support\Collection; |
13
|
|
|
use TimSDK\Tests\TestCase; |
14
|
|
|
|
15
|
|
|
class ResponseBagTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
public function testResponseBagIsConstructed() |
18
|
|
|
{ |
19
|
|
|
$bag = new ResponseBag('foo', 'bar'); |
20
|
|
|
$this->assertSame(['foo'], $bag->getContents()->all()); |
21
|
|
|
$this->assertSame(['bar'], $bag->getHeaders()->all()); |
22
|
|
|
|
23
|
|
|
$bag = new ResponseBag('{"foo": "bar"}', '{"key": "value"}'); |
24
|
|
|
$this->assertSame(['foo' => 'bar'], $bag->getContents()->all()); |
25
|
|
|
$this->assertSame(['key' => 'value'], $bag->getHeaders()->all()); |
26
|
|
|
|
27
|
|
|
$bag = new ResponseBag( |
28
|
|
|
new TestJsonSerializeObject(['foo' => 'bar']), |
29
|
|
|
new TestJsonSerializeObject(['key' => 'value']) |
30
|
|
|
); |
31
|
|
|
$this->assertSame(['foo' => 'bar'], $bag->getContents()->all()); |
32
|
|
|
$this->assertSame(['key' => 'value'], $bag->getHeaders()->all()); |
33
|
|
|
|
34
|
|
|
$bag = new ResponseBag(false, true); |
35
|
|
|
$this->assertSame([false], $bag->getContents()->all()); |
36
|
|
|
$this->assertSame([true], $bag->getHeaders()->all()); |
37
|
|
|
|
38
|
|
|
$bag = new ResponseBag(null, null); |
39
|
|
|
$this->assertSame([], $bag->getContents()->all()); |
40
|
|
|
$this->assertSame([], $bag->getHeaders()->all()); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testResponseStatusCode() |
44
|
|
|
{ |
45
|
|
|
$response = new ResponseBag([], []); |
46
|
|
|
$this->assertSame(200, $response->getStatusCode()); |
47
|
|
|
|
48
|
|
|
$response = new ResponseBag([], [], 400); |
49
|
|
|
$this->assertSame(400, $response->getStatusCode()); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function testGetCollectionItems() |
53
|
|
|
{ |
54
|
|
|
$m = \Mockery::mock(ResponseBag::class, [ |
55
|
|
|
['foo' => 'bar'], |
56
|
|
|
['foo' => 'bar'], |
57
|
|
|
])->shouldAllowMockingProtectedMethods()->shouldDeferMissing(); |
58
|
|
|
|
59
|
|
|
$this->assertEquals(new Collection(['foo' => 'bar']), $m->getCollectionItems(['foo' => 'bar'])); |
60
|
|
|
$this->assertEquals(new Collection(['foo' => 'bar']), $m->getCollectionItems(new Collection(['foo' => 'bar']))); |
61
|
|
|
$this->assertEquals(new Collection(['foo' => 'bar']), $m->getCollectionItems('{"foo":"bar"}')); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
class TestJsonSerializeObject implements \JsonSerializable |
66
|
|
|
{ |
67
|
|
|
protected $items = []; |
68
|
|
|
|
69
|
|
|
public function __construct($items) |
70
|
|
|
{ |
71
|
|
|
$this->items = $items; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function jsonSerialize() |
75
|
|
|
{ |
76
|
|
|
return $this->items; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|