Skip to content

Commit 172cb11

Browse files
committed
Add testing to Request, change "then" method behaviour
1 parent 2dfa440 commit 172cb11

File tree

4 files changed

+214
-1
lines changed

4 files changed

+214
-1
lines changed
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Ivan770\HttpClient\Exceptions;
4+
5+
use Exception;
6+
7+
class DataIsNotCollection extends Exception
8+
{
9+
public function __construct($message, $code = 0, Exception $previous = null) {
10+
parent::__construct($message, $code, $previous);
11+
}
12+
13+
public function __toString() {
14+
return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
15+
}
16+
}

src/MockResponse.php

+176
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
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+
}

src/Request.php

+21
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ protected function defaultAttach(HttpClient $client)
6666
//
6767
}
6868

69+
/**
70+
* Request tests
71+
*
72+
* @return array
73+
*/
74+
protected function tests()
75+
{
76+
return [];
77+
}
78+
6979
/**
7080
* Attach builder properties. HttpClient instance is passed into Closure
7181
*
@@ -79,6 +89,17 @@ public function attach($callback)
7989
return $this;
8090
}
8191

92+
/**
93+
* Get test response
94+
*
95+
* @param $test
96+
* @return mixed
97+
*/
98+
public function mock($test)
99+
{
100+
return $this->tests()[$test];
101+
}
102+
82103
/**
83104
* Run request
84105
*

src/Response.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function getContent($throw = true)
8787
*/
8888
public function then($function)
8989
{
90-
return $function->call($this, $this->getContent());
90+
return $function($this->getContent());
9191
}
9292

9393
/**

0 commit comments

Comments
 (0)