Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

HttpClientTest

robertl7 Sep 26th, 2018 80 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. <?php
  2.  
  3. namespace Tests\unit\Utils;
  4.  
  5. use App\Utils\HttpClient;
  6. use GuzzleHttp\Client;
  7. use Psr\Http\Message\ResponseInterface;
  8. use Psr\Http\Message\StreamInterface;
  9. use PHPUnit\Framework\TestCase;
  10.  
  11. /**
  12.  * Class UserHelperTest
  13.  * @package Tests\unit\Utils
  14.  */
  15. class HttpClientTest extends TestCase
  16. {
  17.  
  18.     /** @var ResponseInterface $responseHttp */
  19.     private $responseHttp;
  20.  
  21.     /** @var StreamInterface $stream */
  22.     private $stream;
  23.  
  24.     /** @var Client $client */
  25.     private $client;
  26.  
  27.     public function setUp()
  28.     {
  29.         $this->responseHttp = $this->prophesize(ResponseInterface::class);
  30.         $this->stream = $this->prophesize(StreamInterface::class);
  31.         $this->client = $this->prophesize(Client::class);
  32.     }
  33.  
  34.     /**
  35.      * @test
  36.      */
  37.     public function makePostRequestWithFullParamShouldReturnArray()
  38.     {
  39.         $responseJsonData = '{"status":true,"data":[{"var1":2,"var2":"test"}]}';
  40.         $responseDecodedData = json_decode($responseJsonData);
  41.         $uri = 'sime-uri-1';
  42.         $data = ['partner_id' => '999'];
  43.  
  44.         $params = [
  45.             "headers" => [
  46.                 "sign" => "8a9f14225d826f66a34ff4dbc8ea2e6c72dad1cc192835f00b5999418061e6c09e6361ba7111f3fe5d28dd3ce4e1d60db5041b66639b11ecd8c91c330c715435"
  47.             ],
  48.             "connect_timeout" => 30,
  49.             "debug" => false,
  50.             "form_params" => ["partner_id" => "999"]
  51.         ];
  52.  
  53.         $this->client->request('POST', $uri, $params)->willReturn($this->responseHttp->reveal());
  54.         $this->responseHttp->getBody()->willReturn($this->stream->reveal());
  55.         $this->stream->getContents()->willReturn($responseJsonData);
  56.  
  57.         $httpClient = new HttpClient($this->client->reveal());
  58.  
  59.         $result = $httpClient->makePostRequest($uri, $data);
  60.  
  61.         self::assertInstanceOf(\stdClass::class, $result);
  62.         self::assertEquals($responseDecodedData->status, $result->status);
  63.         self::assertEquals(current($responseDecodedData->data)->var1, current($result->data)->var1);
  64.         self::assertEquals(current($responseDecodedData->data)->var2, current($result->data)->var2);
  65.     }
  66. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top