Last active
March 29, 2022 19:02
-
-
Save eduPHP/d15007f2b8f65e98f66c4d566fa809fa to your computer and use it in GitHub Desktop.
Trait for testing with guzzle requests, use with https://gist.github.com/eduPHP/089ad67aa5980722c6cddd6353d0cf43
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace Tests; | |
| use GuzzleHttp\Client; | |
| use GuzzleHttp\Handler\MockHandler; | |
| use GuzzleHttp\HandlerStack; | |
| use GuzzleHttp\Psr7\Response as GuzzleResponse; | |
| use Illuminate\Support\Arr; | |
| use Tests\stubs\HttpClientStub; | |
| trait MocksGuzzle | |
| { | |
| protected function fakeApiResponse(string $name, array $replace = [], $raw = false) | |
| { | |
| if (file_exists($file = base_path("/tests/apiResponses/{$name}.json"))) { | |
| $content = file_get_contents($file); | |
| } elseif (file_exists($file = base_path("/tests/apiResponses/{$name}.xml"))) { | |
| $content = file_get_contents($file); | |
| } elseif (file_exists($file = base_path("/tests/apiResponses/{$name}"))) { | |
| $content = file_get_contents($file); | |
| } else { | |
| $content = $name; | |
| } | |
| foreach ($replace as $key => $value) { | |
| $content = preg_replace("[\{{$key}\}]", $value, $content); | |
| } | |
| return $raw ? $content : json_decode($content, true); | |
| } | |
| public function fakeGuzzleResponse($response, $status = 200): MockHandler | |
| { | |
| $response = Arr::wrap($response); | |
| $status = Arr::wrap($status); | |
| $guzzleResponse = []; | |
| foreach ($response as $i => $item) { | |
| if ($item instanceof GuzzleResponse) { | |
| $guzzleResponse[] = $item; | |
| } else { | |
| $guzzleResponse[] = new GuzzleResponse($status[$i] ?? 200, [], is_array($item) ? | |
| json_encode($item) : | |
| $item | |
| ); | |
| } | |
| } | |
| $mock = new MockHandler($guzzleResponse); | |
| $this->instance(Client::class, new HttpClientStub(['handler' => HandlerStack::create($mock)])); | |
| return $mock; | |
| } | |
| public function assertGuzzleHasFormParams(array|string $expectedParam, $n = 0) | |
| { | |
| foreach (Arr::wrap($expectedParam) as $key => $param) { | |
| if (is_numeric($key) && !isset(HttpClientStub::$options[$n]['form_params'][$param])) { | |
| $this->fail("Failed asserting that Guzzle has sent '{$param}' param."); | |
| } else if (!is_numeric($key) && !isset(HttpClientStub::$options[$n]['form_params'][$key])) { | |
| $this->fail("Failed asserting that Guzzle has sent '{$key}' param."); | |
| } else if (!is_numeric($key) && isset(HttpClientStub::$options[$n]['form_params'][$key]) && HttpClientStub::$options[$n]['form_params'][$key] !== $param) { | |
| $this->fail("Failed asserting that Guzzle param '{$key}' is equals to '{$param}'."); | |
| } | |
| $this->addToAssertionCount(1); | |
| } | |
| } | |
| public function assertGuzzleUrlContains(array|string $expectedParam, $n = 0) | |
| { | |
| foreach (Arr::wrap($expectedParam) as $param) { | |
| $this->assertStringContainsString( | |
| $param, | |
| HttpClientStub::$uri[$n] | |
| ); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment