1
+ <?php
2
+
3
+
4
+ namespace Ivan770 \HttpClient ;
5
+
6
+ use Illuminate \Contracts \Pipeline \Pipeline as PipelineContract ;
7
+ use Illuminate \Pipeline \Pipeline ;
8
+ use Illuminate \Container \Container ;
9
+ use Illuminate \Support \Collection ;
10
+ use Ivan770 \HttpClient \Contracts \Response ;
11
+ use Ivan770 \HttpClient \Exceptions \DataIsNotCollection ;
12
+ use Ivan770 \HttpClient \Exceptions \PipelineNotAvailable ;
13
+
14
+
15
+ class MockResponse implements Response
16
+ {
17
+ /**
18
+ * @var Collection|string
19
+ */
20
+ protected $ data ;
21
+
22
+ /**
23
+ * @var int
24
+ */
25
+ protected $ status ;
26
+
27
+ /**
28
+ * @var array
29
+ */
30
+ protected $ headers ;
31
+
32
+ /**
33
+ * @var Container
34
+ */
35
+ protected $ container ;
36
+
37
+ /**
38
+ * @var Pipeline
39
+ */
40
+ protected $ pipeline ;
41
+
42
+ /**
43
+ * MockResponse constructor.
44
+ *
45
+ * @param Collection|string $data
46
+ * @param int $status
47
+ * @param array $headers
48
+ */
49
+ public function __construct ($ data , $ status = 200 , $ headers = [])
50
+ {
51
+ $ this ->data = $ data ;
52
+ $ this ->status = $ status ;
53
+ $ this ->headers = $ headers ;
54
+ }
55
+
56
+ /**
57
+ * Check if Pipeline is available
58
+ *
59
+ * @return bool
60
+ * @throws PipelineNotAvailable
61
+ */
62
+ protected function pipelineAvailable ()
63
+ {
64
+ if (class_exists (Pipeline::class)) {
65
+ return true ;
66
+ }
67
+ throw new PipelineNotAvailable ("Pipeline class cannot be found " );
68
+ }
69
+
70
+ /**
71
+ * Get container instance
72
+ *
73
+ * @return Container|null
74
+ */
75
+ protected function getContainer ()
76
+ {
77
+ if (is_null ($ this ->container ) && class_exists (Container::class)) {
78
+ $ this ->container = Container::getInstance ();
79
+ }
80
+ return $ this ->container ;
81
+ }
82
+
83
+ /**
84
+ * Get Pipeline instance
85
+ *
86
+ * @return Pipeline
87
+ * @throws PipelineNotAvailable
88
+ */
89
+ protected function getPipeline ()
90
+ {
91
+ if ($ this ->getContainer ()->bound (PipelineContract::class)) {
92
+ $ this ->pipeline = $ this ->getContainer ()->make (PipelineContract::class);
93
+ }
94
+ if (is_null ($ this ->pipeline ) && $ this ->pipelineAvailable ()) {
95
+ $ this ->pipeline = new Pipeline ($ this ->getContainer ());
96
+ }
97
+ return $ this ->pipeline ;
98
+ }
99
+
100
+ /**
101
+ * Get passed collection
102
+ *
103
+ * @return Collection
104
+ * @throws DataIsNotCollection
105
+ */
106
+ public function toCollection ()
107
+ {
108
+ if ($ this ->data instanceof Collection) {
109
+ return $ this ->data ;
110
+ }
111
+
112
+ throw new DataIsNotCollection ("Passed data has to be collection instance " );
113
+ }
114
+
115
+ /**
116
+ * Get passed data
117
+ *
118
+ * @param bool $throw
119
+ * @return Collection|string
120
+ */
121
+ public function getContent ($ throw = true )
122
+ {
123
+ return $ this ->data ;
124
+ }
125
+
126
+ /**
127
+ * @return int
128
+ */
129
+ public function getStatus ()
130
+ {
131
+ return $ this ->status ;
132
+ }
133
+
134
+ /**
135
+ * @return array
136
+ */
137
+ public function getHeaders ()
138
+ {
139
+ return $ this ->headers ;
140
+ }
141
+
142
+ /**
143
+ * Pass response content to function
144
+ *
145
+ * @param \Closure $function Function to call
146
+ * @return mixed
147
+ */
148
+ public function then ($ function )
149
+ {
150
+ return $ function ($ this ->getContent ());
151
+ }
152
+
153
+ /**
154
+ * Pass response content to pipeline
155
+ *
156
+ * @return Pipeline
157
+ * @throws PipelineNotAvailable
158
+ */
159
+ public function pipeline ()
160
+ {
161
+ return $ this ->getPipeline ()->send ($ this ->getContent ());
162
+ }
163
+
164
+ /**
165
+ * Make new MockResponse instance
166
+ *
167
+ * @param Collection|string $data
168
+ * @param int $status
169
+ * @param array $headers
170
+ * @return MockResponse
171
+ */
172
+ public static function make ($ data , $ status = 200 , $ headers = [])
173
+ {
174
+ return (new static ($ data , $ status , $ headers ));
175
+ }
176
+ }
0 commit comments