Skip to content

Instantly share code, notes, and snippets.

@pazthor
Created May 30, 2023 04:46
Show Gist options
  • Save pazthor/378fecc9a26602ec74b6bc7b27f04d14 to your computer and use it in GitHub Desktop.
Save pazthor/378fecc9a26602ec74b6bc7b27f04d14 to your computer and use it in GitHub Desktop.
Extracto de clases del challenge de Easy Broker escrito en PHP
<?php
namespace App\Services;
use GuzzleHttp\Exception\GuzzleException;
class GetCustomer
{
private $token;
private $baseURL;
public function __construct()
{
$this->token= 'l7u502p8v46ba3ppgvj5y2aad50lb9';
$this->baseURL='https://api.stagingeb.com/v1/';
}
/**
* @throws GuzzleException
*/
public function requestProperties($pagination=1, $limit=50){
$client = new \GuzzleHttp\Client();
$response = $client->request('GET', "{$this->baseURL}properties?page={$pagination}&limit={$limit}", [
'headers' => [
'X-Authorization' => $this->token,
'accept' => 'application/json',
],
]);
return json_decode($response->getBody(), true) ;
}
/**
* @throws GuzzleException
*/
public function getTitles(){
$key = 'content';
$keyToSearh = 'title';
$responseGetCustomer = $this->requestProperties();
$existContent = array_key_exists($key,$responseGetCustomer);
if(! $existContent){
return "Error to get Data";
}
$arrayContent = $responseGetCustomer[$key];
return array_column($arrayContent,$keyToSearh);
}
}
<?php
namespace Tests\Feature;
use App\Services\GetCustomer;
use Mockery\MockInterface;
use Tests\TestCase;
class ExampleTest extends TestCase
{
public function test_should_fail_if_received_a_diferente_json()
{
$responseFake=["paginatio"=> [],"conten"=>[]];
$mock = $this->partialMock(GetCustomer::class, function (MockInterface $mock) use ($responseFake) {
$mock->shouldReceive('requestProperties')->once()->andReturn($responseFake);
});
$customer= app(GetCustomer::class);
$result = $customer->getTitles();
$expectedMessage='Error to get Data';
$this->assertEquals($expectedMessage, $result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment