Skip to content
This repository has been archived by the owner on Dec 16, 2024. It is now read-only.

Commit

Permalink
add http client stub for async tests
Browse files Browse the repository at this point in the history
  • Loading branch information
giansalex committed Aug 19, 2019
1 parent 15f2fec commit 4f38c36
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
39 changes: 39 additions & 0 deletions tests/Peru/Sunat/Async/HttpClientStub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Tests\Peru\Sunat\Async;

use Peru\Http\Async\ClientInterface;
use React\Promise\PromiseInterface;
use Tests\Peru\Sunat\ClientStubDecorator;

class HttpClientStub implements ClientInterface
{
/**
* @var ClientInterface
*/
private $client;

/**
* HttpClientStub constructor.
* @param ClientInterface $client
*/
public function __construct(ClientInterface $client)
{
$this->client = $client;
}

/**
* Make GET Request.
*
* @param string $url
* @param array $headers
*
* @return PromiseInterface
*/
public function getAsync(string $url, array $headers = []): PromiseInterface
{
return $this->client->getAsync(ClientStubDecorator::getNewUrl($url), $headers);
}
}
7 changes: 2 additions & 5 deletions tests/Peru/Sunat/Async/RucTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@

use function Clue\React\Block\await;
use Peru\Http\Async\HttpClient;
use Peru\Sunat\Async\Ruc;
use Peru\Sunat\Company;
use Peru\Sunat\HtmlParser;
use Peru\Sunat\RucParser;
use Peru\Sunat\{Async\Ruc, Company, HtmlParser, RucParser};
use PHPUnit\Framework\TestCase;
use React\EventLoop\Factory;

Expand All @@ -21,7 +18,7 @@ class RucTest extends TestCase
public function testGetRuc()
{
$loop = Factory::create();
$cs = new Ruc(new HttpClient($loop), new RucParser(new HtmlParser()));
$cs = new Ruc(new HttpClientStub(new HttpClient($loop)), new RucParser(new HtmlParser()));
$promise = $cs->get('10401510465');
/**@var $company Company */
$company = await($promise, $loop);
Expand Down
6 changes: 3 additions & 3 deletions tests/Peru/Sunat/ClientStubDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __construct(ClientInterface $client)
*/
public function get(string $url, array $headers = [])
{
return $this->client->get($this->getNewUrl($url), $headers);
return $this->client->get(self::getNewUrl($url), $headers);
}

/**
Expand All @@ -52,10 +52,10 @@ public function get(string $url, array $headers = [])
*/
public function post(string $url, $data, array $headers = [])
{
return $this->client->post($this->getNewUrl($url), $data, $headers);
return $this->client->post(self::getNewUrl($url), $data, $headers);
}

private function getNewUrl($url)
public static function getNewUrl($url)
{
$urlBase = getenv('MOCK_URL');
$u = parse_url($url);
Expand Down

0 comments on commit 4f38c36

Please sign in to comment.