Skip to content

Instantly share code, notes, and snippets.

@westonwatson
Last active August 14, 2020 16:39
Show Gist options
  • Save westonwatson/bd9e82091c1e878b0a1ec15da22f74cc to your computer and use it in GitHub Desktop.
Save westonwatson/bd9e82091c1e878b0a1ec15da22f74cc to your computer and use it in GitHub Desktop.
Example Mockery Attempt
<?php
/* ACTUAL FEATURE CLASS */
namespace App\Tools\Clients;
use App\Tools\Clients\Http\HttpClient;
use Illuminate\Support\Facades\App;
/**
* Class ScreenshotServiceClient.
*/
class ScreenshotServiceClient
{
/**
* @var mixed
*/
public $last_response;
/**
* @var HttpClient
*/
private $client;
/**
* @var \Illuminate\Config\Repository|mixed
*/
private $api_root;
/**
* ScreenshotServiceClient constructor.
* @param null|HttpClient $http_client
*/
public function __construct($http_client = null)
{
$this->api_root = config('app.screenshot.endpoint');
$this->client = $http_client ?? $this->get_client();
}
/**
* @param string $url
* @param int $width
* @param int $height
* @param int $wait
*
* @throws \Exception
*
* @return null|string
*/
public function get_screenshot($url, $width = 768, $height = 1024, $wait = 1)
{
$request = '?';
$request .= $this->build_request($url, $width, $height, $wait);
try {
$this->last_response = $this->client->get($request);
$resppnse = json_decode((string) $this->last_response);
} catch (\Exception $e) {
throw new \Exception("Unable to get screenshot for {$url} - ".$e->getMessage());
}
if ('OK' == $resppnse->status) {
return $resppnse->image;
}
throw new \Exception('Screenshot Service '.$resppnse->message);
}
/**
* @param string $url
* @param int $width
* @param int $height
* @param int $wait
*
* @return string
*/
private function build_request($url, $width, $height, $wait)
{
//$url = urlencode($url);
return http_build_query(['url' => $url, 'wait' => $wait, 'width' => $width, 'height' => $height]);
}
/**
* @return HttpClient
*/
private function get_client()
{
return App::make(HttpClient::class, ['root_url' => $this->api_root]);
}
}
/* TEST BELOW */
<?php
namespace Tests\Unit\Tools\Clients;
use App\Tools\Clients\Http\HttpClient;
use App\Tools\Clients\Http\HttpResponse;
use App\Tools\Clients\ScreenshotServiceClient;
use Illuminate\Support\Facades\App;
use Tests\TestCase;
class ScreenshotServiceClientTest extends TestCase
{
//($url, $width = 768, $height = 1024, $wait = 1)
const BASE64_IMAGE = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVQI12P4//8/AAX+Av7czFnnAAAAAElFTkSuQmCC';
const GOOD_URL = 'http://google.com';
const WIDTH = 768;
const HEIGHT = 1024;
const WAIT = 1;
const SCREENSHOT_SUCCESS = '{"status":"OK","image":"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVQI12P4//8/AAX+Av7czFnnAAAAAElFTkSuQmCC"}';
CONST SCREENSHOT_FAILURE = '{"status":"BROWSE_FAIL","message":"Error: net::ERR_NAME_NOT_RESOLVED at http://google.com","warning":"Using placeholder image instead!","image":"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVQI12P4//8/AAX+Av7czFnnAAAAAElFTkSuQmCC"}';
protected function setUp(): void
{
parent::setUp();
}
public function test_get_screenshot_success()
{
$this->mock(HttpClient::class, function ($mock) {
$response = App::make(HttpResponse::class, [
'status' => 200,
'headers' => [],
'body' => self::SCREENSHOT_SUCCESS,
]);
$mock->shouldReceive('get')
->withAnyArgs()
->andReturn($response);
});
$screenshot_client = App::make(ScreenshotServiceClient::class);
$screenshot_response = $screenshot_client->get_screenshot(self::GOOD_URL, self::WIDTH, self::HEIGHT, self::WAIT);
$this->assertEquals(self::BASE64_IMAGE, $screenshot_response);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment