chore: replace php-vcr by guzzle when mocking external api calls (#2296)

This commit is contained in:
Régis Freyd
2019-01-12 18:04:03 -05:00
committed by GitHub
parent 16d9e36f0f
commit 25b3505274
21 changed files with 180 additions and 402 deletions
+7 -4
View File
@@ -4,6 +4,7 @@ namespace App\Services\Account\Place;
use App\Models\Account\Place;
use App\Services\BaseService;
use GuzzleHttp\Client as GuzzleClient;
use App\Services\Instance\Geolocalization\GetGPSCoordinate;
class CreatePlace extends BaseService
@@ -31,9 +32,10 @@ class CreatePlace extends BaseService
* Create a place.
*
* @param array $data
* @param GuzzleClient the Guzzle client, only needed when unit testing
* @return Place
*/
public function execute(array $data) : Place
public function execute(array $data, GuzzleClient $client = null) : Place
{
$this->validate($data);
@@ -49,7 +51,7 @@ class CreatePlace extends BaseService
]);
if (is_null($place->latitude)) {
$this->getGeocodingInfo($place);
$this->getGeocodingInfo($place, $client);
}
return $place;
@@ -59,13 +61,14 @@ class CreatePlace extends BaseService
* Get geocoding information about the place (lat/longitude).
*
* @param Place $place
* @param GuzzleClient the Guzzle client, only needed when unit testing
* @return void|null
*/
private function getGeocodingInfo(Place $place)
private function getGeocodingInfo(Place $place, GuzzleClient $client = null)
{
(new GetGPSCoordinate)->execute([
'account_id' => $place->account_id,
'place_id' => $place->id,
]);
], $client);
}
}
+7 -4
View File
@@ -4,6 +4,7 @@ namespace App\Services\Account\Place;
use App\Models\Account\Place;
use App\Services\BaseService;
use GuzzleHttp\Client as GuzzleClient;
use App\Services\Instance\Geolocalization\GetGPSCoordinate;
class UpdatePlace extends BaseService
@@ -32,9 +33,10 @@ class UpdatePlace extends BaseService
* Update a place.
*
* @param array $data
* @param GuzzleClient the Guzzle client, only needed when unit testing
* @return Place
*/
public function execute(array $data) : Place
public function execute(array $data, GuzzleClient $client = null) : Place
{
$this->validate($data);
@@ -52,7 +54,7 @@ class UpdatePlace extends BaseService
]);
if (is_null($place->latitude)) {
$this->getGeocodingInfo($place);
$this->getGeocodingInfo($place, $client);
}
return $place;
@@ -62,13 +64,14 @@ class UpdatePlace extends BaseService
* Get geocoding information about the place (lat/longitude).
*
* @param Place $place
* @param GuzzleClient the Guzzle client, only needed when unit testing
* @return void|null
*/
private function getGeocodingInfo(Place $place)
private function getGeocodingInfo(Place $place, GuzzleClient $client = null)
{
(new GetGPSCoordinate)->execute([
'account_id' => $place->account_id,
'place_id' => $place->id,
]);
], $client);
}
}
@@ -10,6 +10,8 @@ use GuzzleHttp\Exception\ClientException;
class GetGPSCoordinate extends BaseService
{
protected $client;
/**
* Get the validation rules that apply to the service.
*
@@ -28,12 +30,19 @@ class GetGPSCoordinate extends BaseService
* This method uses LocationIQ to process the geocoding.
*
* @param array $data
* @param GuzzleClient the Guzzle client, only needed when unit testing
* @return Place|null
*/
public function execute(array $data)
public function execute(array $data, GuzzleClient $client = null)
{
$this->validate($data);
if (! is_null($client)) {
$this->client = $client;
} else {
$this->client = new GuzzleClient();
}
$place = Place::where('account_id', $data['account_id'])
->findOrFail($data['place_id']);
@@ -79,10 +88,8 @@ class GetGPSCoordinate extends BaseService
return;
}
$client = new GuzzleClient();
try {
$response = $client->request('GET', $query);
$response = $this->client->request('GET', $query);
} catch (ClientException $e) {
Log::error('Error making the call: '.$e);
@@ -13,6 +13,8 @@ use App\Services\Instance\Geolocalization\GetGPSCoordinate;
class GetWeatherInformation extends BaseService
{
protected $client;
/**
* Get the validation rules that apply to the service.
*
@@ -29,13 +31,14 @@ class GetWeatherInformation extends BaseService
* Get the weather information.
*
* @param array $data
* @param GuzzleClient the Guzzle client, only needed when unit testing
* @return Weather|null
* @throws Illuminate\Validation\ValidationException if the array that is given in parameter is not valid
* @throws App\Exceptions\MissingEnvVariableException if the weather services are not enabled
* @throws Illuminate\Database\Eloquent\ModelNotFoundException if the Place object is not found
* @throws GuzzleHttp\Exception\ClientException if the request to Darksky crashed
*/
public function execute(array $data)
public function execute(array $data, GuzzleClient $client = null)
{
$this->validateWeatherEnvVariables();
@@ -51,6 +54,12 @@ class GetWeatherInformation extends BaseService
}
}
if (! is_null($client)) {
$this->client = $client;
} else {
$this->client = new GuzzleClient();
}
return $this->query($place);
}
@@ -81,10 +90,8 @@ class GetWeatherInformation extends BaseService
{
$query = $this->buildQuery($place);
$client = new GuzzleClient();
try {
$response = $client->request('GET', $query);
$response = $this->client->request('GET', $query);
$response = json_decode($response->getBody());
} catch (ClientException $e) {
Log::error('Error making the call: '.$e);
-1
View File
@@ -54,7 +54,6 @@
"laravel/dusk": "^4.0",
"matthiasnoback/live-code-coverage": "^1.0",
"mockery/mockery": "^1.0",
"php-vcr/php-vcr": "^1.4",
"phpunit/phpcov": "^5.0",
"phpunit/phpunit": "^7.0",
"roave/security-advisories": "dev-master",
Generated
+8 -172
View File
@@ -1,10 +1,10 @@
{
"_readme": [
"This file locks the dependencies of your project to a known state",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "a8211d953d18fad45debb466840f13dd",
"content-hash": "fab6b967d2a90f8f4122b02ceafdac6d",
"packages": [
{
"name": "aws/aws-sdk-php",
@@ -3589,16 +3589,16 @@
},
{
"name": "nikic/php-parser",
"version": "v4.1.1",
"version": "v4.2.0",
"source": {
"type": "git",
"url": "https://github.com/nikic/PHP-Parser.git",
"reference": "8aae5b59b83bb4d0dbf07b0a835f2680a658f610"
"reference": "594bcae1fc0bccd3993d2f0d61a018e26ac2865a"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/8aae5b59b83bb4d0dbf07b0a835f2680a658f610",
"reference": "8aae5b59b83bb4d0dbf07b0a835f2680a658f610",
"url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/594bcae1fc0bccd3993d2f0d61a018e26ac2865a",
"reference": "594bcae1fc0bccd3993d2f0d61a018e26ac2865a",
"shasum": ""
},
"require": {
@@ -3614,7 +3614,7 @@
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "4.1-dev"
"dev-master": "4.2-dev"
}
},
"autoload": {
@@ -3636,7 +3636,7 @@
"parser",
"php"
],
"time": "2018-12-26T11:32:39+00:00"
"time": "2019-01-12T16:31:37+00:00"
},
{
"name": "ok/ipstack-client",
@@ -7739,61 +7739,6 @@
],
"time": "2018-11-09T08:37:55+00:00"
},
{
"name": "beberlei/assert",
"version": "v2.9.6",
"source": {
"type": "git",
"url": "https://github.com/beberlei/assert.git",
"reference": "ec9e4cf0b63890edce844ee3922e2b95a526e936"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/beberlei/assert/zipball/ec9e4cf0b63890edce844ee3922e2b95a526e936",
"reference": "ec9e4cf0b63890edce844ee3922e2b95a526e936",
"shasum": ""
},
"require": {
"ext-mbstring": "*",
"php": ">=5.3"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.1.1",
"phpunit/phpunit": "^4.8.35|^5.7"
},
"type": "library",
"autoload": {
"psr-4": {
"Assert\\": "lib/Assert"
},
"files": [
"lib/Assert/functions.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-2-Clause"
],
"authors": [
{
"name": "Benjamin Eberlei",
"email": "kontakt@beberlei.de",
"role": "Lead Developer"
},
{
"name": "Richard Quadling",
"email": "rquadling@gmail.com",
"role": "Collaborator"
}
],
"description": "Thin assertion library for input validation in business models.",
"keywords": [
"assert",
"assertion",
"validation"
],
"time": "2018-06-11T17:15:25+00:00"
},
{
"name": "composer/xdebug-handler",
"version": "1.3.1",
@@ -8726,56 +8671,6 @@
],
"time": "2018-02-15T16:58:55+00:00"
},
{
"name": "php-vcr/php-vcr",
"version": "1.4.4",
"source": {
"type": "git",
"url": "https://github.com/php-vcr/php-vcr.git",
"reference": "13872a0b291c1a398461ac178850837648998271"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/php-vcr/php-vcr/zipball/13872a0b291c1a398461ac178850837648998271",
"reference": "13872a0b291c1a398461ac178850837648998271",
"shasum": ""
},
"require": {
"beberlei/assert": "^2.0",
"ext-curl": "*",
"symfony/event-dispatcher": "^2.4|^3.0|^4.0",
"symfony/yaml": "~2.1|^3.0|^4.0"
},
"require-dev": {
"lapistano/proxy-object": "dev-master#d7184a479f502d5a0f96d0bae73566dbb498da8f",
"mikey179/vfsstream": "^1.2",
"phpunit/phpunit": "^4.8|^5.0",
"sebastian/version": "^1.0.3|^2.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.4.x-dev"
}
},
"autoload": {
"classmap": [
"src/"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Adrian Philipp",
"email": "mail@adrian-philipp.com"
}
],
"description": "Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.",
"time": "2018-05-30T15:10:47+00:00"
},
{
"name": "phpdocumentor/reflection-common",
"version": "1.0.1",
@@ -10240,65 +10135,6 @@
"homepage": "https://symfony.com",
"time": "2019-01-03T09:07:35+00:00"
},
{
"name": "symfony/yaml",
"version": "v4.2.2",
"source": {
"type": "git",
"url": "https://github.com/symfony/yaml.git",
"reference": "d0aa6c0ea484087927b49fd513383a7d36190ca6"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/yaml/zipball/d0aa6c0ea484087927b49fd513383a7d36190ca6",
"reference": "d0aa6c0ea484087927b49fd513383a7d36190ca6",
"shasum": ""
},
"require": {
"php": "^7.1.3",
"symfony/polyfill-ctype": "~1.8"
},
"conflict": {
"symfony/console": "<3.4"
},
"require-dev": {
"symfony/console": "~3.4|~4.0"
},
"suggest": {
"symfony/console": "For validating YAML files using the lint command"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "4.2-dev"
}
},
"autoload": {
"psr-4": {
"Symfony\\Component\\Yaml\\": ""
},
"exclude-from-classmap": [
"/Tests/"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Fabien Potencier",
"email": "fabien@symfony.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Symfony Yaml Component",
"homepage": "https://symfony.com",
"time": "2019-01-03T09:07:35+00:00"
},
{
"name": "theseer/fdomdocument",
"version": "1.6.6",
+3 -28
View File
@@ -161,36 +161,11 @@ sudo apt -y -f install google-chrome-stable fonts-liberation libappindicator1
You should never make real HTTP calls in your unit tests - like querying an external API that is not linked to Monica.
You can mock http calls with PHP VCR.
You can mock http calls by mocking calls made by Guzzl.
In your test files, the first time you access an external API, you need to add this to your test:
You can find an example of how mocking is done in the `GetWeatherInformationTest.php` file.
```
\VCR\VCR::turnOn();
\VCR\VCR::insertCassette('name_of_your_test.yml');
```
and add this at the end of the file:
```
\VCR\VCR::eject();
\VCR\VCR::turnOff();
```
Run the test. PHP VCR will record the call and its response into a yml file. The .yml file is stored in `tests/fixtures`.
Remember to remove any API key from the yml file that what used to make the call in the first place before commiting.
If you are satisfied with the call and the response, replace the code above by:
```
\VCR\VCR::turnOn();
\VCR\VCR::configure()->setMode('none');
\VCR\VCR::configure()->enableRequestMatchers(array('method', 'url'));
\VCR\VCR::insertCassette('name_of_your_test.yml');
```
This will make sure that when the test run again, it will query the yml file to get the response and not make the real call.
You need to provide a sample response of the external call that you are mocking in the Fixtures folder.
<a id="markdown-coding-guidelines" name="coding-guidelines"></a>
## Coding guidelines
@@ -0,0 +1,19 @@
[{
"place_id":"200788090",
"licence":"\u00a9 LocationIQ.com CC BY 4.0, Data \u00a9 OpenStreetMap contributors, ODbL 1.0",
"osm_type":"relation",
"osm_id":"3503615",
"boundingbox":[
"34.05269",
"34.112456",
"-118.4270732",
"-118.3715358"
],
"lat":"34.0736204",
"lon":"-118.4003563",
"display_name":"Beverly Hills, Los Angeles County, California, USA",
"class":"place",
"type":"city",
"importance":0.72188891692726,
"icon":"https:\/\/locationiq.org\/static\/images\/mapicons\/poi_place_city.p.20.png"
}]
@@ -0,0 +1,19 @@
[{
"place_id":"200788090",
"licence":"\u00a9 LocationIQ.com CC BY 4.0, Data \u00a9 OpenStreetMap contributors, ODbL 1.0",
"osm_type":"relation",
"osm_id":"3503615",
"boundingbox":[
"34.05269",
"34.112456",
"-118.4270732",
"-118.3715358"
],
"lat":"34.0736204",
"lon":"-118.4003563",
"display_name":"Beverly Hills, Los Angeles County, California, USA",
"class":"place",
"type":"city",
"importance":0.72188891692726,
"icon":"https:\/\/locationiq.org\/static\/images\/mapicons\/poi_place_city.p.20.png"
}]
@@ -0,0 +1 @@
{"error":"Unable to geocode"}
@@ -0,0 +1,19 @@
[{
"place_id":"200788090",
"licence":"\u00a9 LocationIQ.com CC BY 4.0, Data \u00a9 OpenStreetMap contributors, ODbL 1.0",
"osm_type":"relation",
"osm_id":"3503615",
"boundingbox":[
"34.05269",
"34.112456",
"-118.4270732",
"-118.3715358"
],
"lat":"34.0736204",
"lon":"-118.4003563",
"display_name":"Beverly Hills, Los Angeles County, California, USA",
"class":"place",
"type":"city",
"importance":0.72188891692726,
"icon":"https:\/\/locationiq.org\/static\/images\/mapicons\/poi_place_city.p.20.png"
}]
@@ -0,0 +1,27 @@
{
"latitude":34.112456,
"longitude":-118.4270732,
"timezone":"America/Los_Angeles",
"currently":{
"time":1545426311,
"summary":"Partly Cloudy",
"icon":"partly-cloudy-day",
"nearestStormDistance":10,
"nearestStormBearing":296,
"precipIntensity":0,
"precipProbability":0,
"temperature":67.94,
"apparentTemperature":67.94,
"dewPoint":41.87,
"humidity":0.39,
"pressure":1014.57,
"windSpeed":3.35,
"windGust":6.12,
"windBearing":232,
"cloudCover":0.28,
"uvIndex":2,
"visibility":10,
"ozone":277.93
},
"offset":-8
}
@@ -3,8 +3,12 @@
namespace Tests\Unit\Services\Account\Place;
use Tests\TestCase;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use App\Models\Account\Place;
use GuzzleHttp\Psr7\Response;
use App\Models\Account\Account;
use GuzzleHttp\Handler\MockHandler;
use App\Services\Account\Place\CreatePlace;
use Illuminate\Validation\ValidationException;
use Illuminate\Foundation\Testing\DatabaseTransactions;
@@ -28,8 +32,7 @@ class CreatePlaceTest extends TestCase
'longitude' => '10',
];
$placeService = new CreatePlace;
$place = $placeService->execute($request);
$place = (new CreatePlace)->execute($request);
$this->assertDatabaseHas('places', [
'id' => $place->id,
@@ -49,10 +52,10 @@ class CreatePlaceTest extends TestCase
config(['monica.enable_geolocation' => true]);
config(['monica.location_iq_api_key' => 'test']);
\VCR\VCR::turnOn();
\VCR\VCR::configure()->setMode('none');
\VCR\VCR::configure()->enableRequestMatchers(['url']);
\VCR\VCR::insertCassette('create_place_service_gets_gps_coordinates.yml');
$body = file_get_contents(base_path('tests/Fixtures/Services/Account/Place/CreatePlaceSampleResponse.json'));
$mock = new MockHandler([new Response(200, [], $body)]);
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$account = factory(Account::class)->create([]);
@@ -67,8 +70,7 @@ class CreatePlaceTest extends TestCase
'longitude' => '',
];
$placeService = new CreatePlace;
$place = $placeService->execute($request);
$place = (new CreatePlace)->execute($request, $client);
$this->assertDatabaseHas('places', [
'id' => $place->id,
@@ -77,9 +79,6 @@ class CreatePlaceTest extends TestCase
'latitude' => 34.0736204,
'longitude' => -118.4003563,
]);
\VCR\VCR::eject();
\VCR\VCR::turnOff();
}
public function test_it_fails_if_wrong_parameters_are_given()
@@ -3,8 +3,12 @@
namespace Tests\Unit\Services\Account\Place;
use Tests\TestCase;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use App\Models\Account\Place;
use GuzzleHttp\Psr7\Response;
use App\Models\Account\Account;
use GuzzleHttp\Handler\MockHandler;
use App\Services\Account\Place\UpdatePlace;
use Illuminate\Validation\ValidationException;
use Illuminate\Foundation\Testing\DatabaseTransactions;
@@ -19,7 +23,7 @@ class UpdatePlaceTest extends TestCase
$place = factory(Place::class)->create([]);
$request = [
'account_id' => $place->account->id,
'account_id' => $place->account_id,
'place_id' => $place->id,
'street' => '199 Lafayette Street',
'city' => 'New York City',
@@ -30,8 +34,7 @@ class UpdatePlaceTest extends TestCase
'longitude' => '10',
];
$placeService = new UpdatePlace;
$place = $placeService->execute($request);
$place = (new UpdatePlace)->execute($request);
$this->assertDatabaseHas('places', [
'id' => $place->id,
@@ -51,15 +54,15 @@ class UpdatePlaceTest extends TestCase
config(['monica.enable_geolocation' => true]);
config(['monica.location_iq_api_key' => 'test']);
\VCR\VCR::turnOn();
\VCR\VCR::configure()->setMode('none');
\VCR\VCR::configure()->enableRequestMatchers(['url']);
\VCR\VCR::insertCassette('create_place_service_gets_gps_coordinates.yml');
$body = file_get_contents(base_path('tests/Fixtures/Services/Account/Place/UpdatePlaceSampleResponse.json'));
$mock = new MockHandler([new Response(200, [], $body)]);
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$place = factory(Place::class)->create([]);
$request = [
'account_id' => $place->account->id,
'account_id' => $place->account_id,
'place_id' => $place->id,
'street' => '12',
'city' => 'beverly hills',
@@ -70,8 +73,7 @@ class UpdatePlaceTest extends TestCase
'longitude' => '',
];
$placeService = new UpdatePlace;
$place = $placeService->execute($request);
$place = (new UpdatePlace)->execute($request, $client);
$this->assertDatabaseHas('places', [
'id' => $place->id,
@@ -80,9 +82,6 @@ class UpdatePlaceTest extends TestCase
'latitude' => 34.0736204,
'longitude' => -118.4003563,
]);
\VCR\VCR::eject();
\VCR\VCR::turnOff();
}
public function test_it_fails_if_wrong_parameters_are_given()
@@ -3,7 +3,11 @@
namespace Tests\Unit\Services\Instance\Geolocalization;
use Tests\TestCase;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use App\Models\Account\Place;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Handler\MockHandler;
use Illuminate\Validation\ValidationException;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use App\Services\Instance\Geolocalization\GetGPSCoordinate;
@@ -23,8 +27,7 @@ class GetGPSCoordinateTest extends TestCase
'place_id' => $place->id,
];
$geocodingService = new GetGPSCoordinate;
$place = $geocodingService->execute($request);
$place = (new GetGPSCoordinate)->execute($request);
$this->assertNull($place);
}
@@ -34,10 +37,10 @@ class GetGPSCoordinateTest extends TestCase
config(['monica.enable_geolocation' => true]);
config(['monica.location_iq_api_key' => 'test']);
\VCR\VCR::turnOn();
\VCR\VCR::configure()->setMode('none');
\VCR\VCR::configure()->enableRequestMatchers(['url']);
\VCR\VCR::insertCassette('geolocalization_service_gets_gps_coordinates.yml');
$body = file_get_contents(base_path('tests/Fixtures/Services/Instance/Geolocalization/GetGPSCoordinateSampleResponse.json'));
$mock = new MockHandler([new Response(200, [], $body)]);
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$place = factory(Place::class)->create();
@@ -46,8 +49,7 @@ class GetGPSCoordinateTest extends TestCase
'place_id' => $place->id,
];
$geocodingService = new GetGPSCoordinate;
$place = $geocodingService->execute($request);
$place = (new GetGPSCoordinate)->execute($request, $client);
$this->assertDatabaseHas('places', [
'id' => $place->id,
@@ -57,21 +59,18 @@ class GetGPSCoordinateTest extends TestCase
Place::class,
$place
);
\VCR\VCR::eject();
\VCR\VCR::turnOff();
}
public function test_it_returns_null_if_address_is_garbage()
{
\VCR\VCR::turnOn();
\VCR\VCR::configure()->setMode('none');
\VCR\VCR::configure()->enableRequestMatchers(['method', 'url']);
\VCR\VCR::insertCassette('geolocalization_service_returns_null_if_address_is_garbage.yml');
config(['monica.enable_geolocation' => true]);
config(['monica.location_iq_api_key' => 'test']);
$body = file_get_contents(base_path('tests/Fixtures/Services/Instance/Geolocalization/GetGPSCoordinateGarbageResponse.json'));
$mock = new MockHandler([new Response(200, [], $body)]);
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$place = factory(Place::class)->create([
'country' => 'ewqr',
'street' => '',
@@ -84,13 +83,9 @@ class GetGPSCoordinateTest extends TestCase
'place_id' => $place->id,
];
$geocodingService = new GetGPSCoordinate;
$place = $geocodingService->execute($request);
$place = (new GetGPSCoordinate)->execute($request);
$this->assertNull($place);
\VCR\VCR::eject();
\VCR\VCR::turnOff();
}
public function test_it_fails_if_wrong_parameters_are_given()
@@ -102,6 +97,6 @@ class GetGPSCoordinateTest extends TestCase
$this->expectException(ValidationException::class);
$geocodingService = new GetGPSCoordinate;
$place = $geocodingService->execute($request);
$place = (new GetGPSCoordinate)->execute($request);
}
}
@@ -3,8 +3,12 @@
namespace Tests\Unit\Services\Instance\Weather;
use Tests\TestCase;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use App\Models\Account\Place;
use GuzzleHttp\Psr7\Response;
use App\Models\Account\Weather;
use GuzzleHttp\Handler\MockHandler;
use Illuminate\Validation\ValidationException;
use App\Exceptions\MissingEnvVariableException;
use Illuminate\Foundation\Testing\DatabaseTransactions;
@@ -24,16 +28,16 @@ class GetWeatherInformationTest extends TestCase
config(['monica.enable_weather' => true]);
config(['monica.darksky_api_key' => 'test']);
\VCR\VCR::turnOn();
\VCR\VCR::configure()->setMode('none');
\VCR\VCR::configure()->enableRequestMatchers(['url']);
\VCR\VCR::insertCassette('get_weather_information_gets_weather.yml');
$body = file_get_contents(base_path('tests/Fixtures/Services/Instance/Weather/GetWeatherInformationSampleResponse.json'));
$mock = new MockHandler([new Response(200, [], $body)]);
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$request = [
'place_id' => $place->id,
];
$weather = (new GetWeatherInformation)->execute($request);
$weather = (new GetWeatherInformation)->execute($request, $client);
$this->assertDatabaseHas('weather', [
'id' => $weather->id,
@@ -50,9 +54,6 @@ class GetWeatherInformationTest extends TestCase
Weather::class,
$weather
);
\VCR\VCR::eject();
\VCR\VCR::turnOff();
}
public function test_it_cant_get_weather_info_if_weather_not_enabled()
@@ -1,26 +0,0 @@
-
request:
method: GET
url: 'https://us1.locationiq.com/v1/search.php?key=test&q=12+beverly+hills+90210+United+States&format=json'
headers:
Host: us1.locationiq.com
Accept-Encoding: null
User-Agent: 'GuzzleHttp/6.3.3 curl/7.54.0 PHP/7.1.17'
Accept: null
response:
status:
http_version: '1.1'
code: '200'
message: OK
headers:
Date: 'Mon, 17 Dec 2018 12:46:08 GMT'
Content-Type: 'application/json; charset=UTF-8'
Transfer-Encoding: chunked
Connection: keep-alive
Set-Cookie: '__cfduid=d534aca2dd5607d039bb4589b30bd50581545050768; expires=Tue, 17-Dec-19 12:46:08 GMT; path=/; domain=.locationiq.com; HttpOnly'
X-Powered-By: LocationIQ
Access-Control-Allow-Origin: '*'
Expect-CT: 'max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"'
Server: cloudflare
CF-RAY: 48a97da52bcc3f89-YUL
body: '[{"place_id":"200788090","licence":"\u00a9 LocationIQ.com CC BY 4.0, Data \u00a9 OpenStreetMap contributors, ODbL 1.0","osm_type":"relation","osm_id":"3503615","boundingbox":["34.05269","34.112456","-118.4270732","-118.3715358"],"lat":"34.0736204","lon":"-118.4003563","display_name":"Beverly Hills, Los Angeles County, California, USA","class":"place","type":"city","importance":0.72188891692726,"icon":"https:\/\/locationiq.org\/static\/images\/mapicons\/poi_place_city.p.20.png"}]'
@@ -1,26 +0,0 @@
-
request:
method: GET
url: 'https://us1.locationiq.com/v1/search.php?key=test&q=12+beverly+hills+90210+United+States&format=json'
headers:
Host: us1.locationiq.com
Accept-Encoding: null
User-Agent: 'GuzzleHttp/6.3.3 curl/7.54.0 PHP/7.1.17'
Accept: null
response:
status:
http_version: '1.1'
code: '200'
message: OK
headers:
Date: 'Mon, 17 Dec 2018 12:46:08 GMT'
Content-Type: 'application/json; charset=UTF-8'
Transfer-Encoding: chunked
Connection: keep-alive
Set-Cookie: '__cfduid=d534aca2dd5607d039bb4589b30bd50581545050768; expires=Tue, 17-Dec-19 12:46:08 GMT; path=/; domain=.locationiq.com; HttpOnly'
X-Powered-By: LocationIQ
Access-Control-Allow-Origin: '*'
Expect-CT: 'max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"'
Server: cloudflare
CF-RAY: 48a97da52bcc3f89-YUL
body: '[{"place_id":"200788090","licence":"\u00a9 LocationIQ.com CC BY 4.0, Data \u00a9 OpenStreetMap contributors, ODbL 1.0","osm_type":"relation","osm_id":"3503615","boundingbox":["34.05269","34.112456","-118.4270732","-118.3715358"],"lat":"34.0736204","lon":"-118.4003563","display_name":"Beverly Hills, Los Angeles County, California, USA","class":"place","type":"city","importance":0.72188891692726,"icon":"https:\/\/locationiq.org\/static\/images\/mapicons\/poi_place_city.p.20.png"}]'
@@ -1,26 +0,0 @@
-
request:
method: GET
url: 'https://us1.locationiq.com/v1/search.php?key=test&q=+sieklopekznqqq+&format=json'
headers:
Host: us1.locationiq.com
Accept-Encoding: null
User-Agent: 'GuzzleHttp/6.3.3 curl/7.54.0 PHP/7.1.17'
Accept: null
response:
status:
http_version: '1.1'
code: '404'
message: 'Not Found'
headers:
Date: 'Mon, 17 Dec 2018 12:47:18 GMT'
Content-Type: 'application/json; charset=UTF-8'
Transfer-Encoding: chunked
Connection: keep-alive
Set-Cookie: '__cfduid=d180d96ea0a158c95a6edfd5c469ef92c1545050838; expires=Tue, 17-Dec-19 12:47:18 GMT; path=/; domain=.locationiq.com; HttpOnly'
X-Powered-By: LocationIQ
Access-Control-Allow-Origin: '*'
Expect-CT: 'max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"'
Server: cloudflare
CF-RAY: 48a97f5cdcd23f83-YUL
body: '{"error":"Unable to geocode"}'
-27
View File
@@ -1,27 +0,0 @@
-
request:
method: GET
url: 'https://api.darksky.net/forecast/test/34.112456,-118.4270732?exclude=alerts,minutely,hourly,daily,flags'
headers:
Host: api.darksky.net
Accept-Encoding: null
User-Agent: 'GuzzleHttp/6.3.3 curl/7.54.0 PHP/7.1.17'
Accept: null
response:
status:
http_version: '1.1'
code: '200'
message: OK
headers:
Date: 'Fri, 21 Dec 2018 21:05:11 GMT'
Content-Type: 'application/json; charset=utf-8'
Content-Length: '483'
Connection: keep-alive
X-Authentication-Time: 2ms
Cache-Control: max-age=3600
Expires: 'Fri, 21 Dec 2018 22:05:11 +0000'
X-Forecast-API-Calls: '3'
X-Content-Type-Options: nosniff
X-Response-Time: 8.247ms
Vary: Accept-Encoding
body: '{"latitude":34.112456,"longitude":-118.4270732,"timezone":"America/Los_Angeles","currently":{"time":1545426311,"summary":"Partly Cloudy","icon":"partly-cloudy-day","nearestStormDistance":10,"nearestStormBearing":296,"precipIntensity":0,"precipProbability":0,"temperature":67.94,"apparentTemperature":67.94,"dewPoint":41.87,"humidity":0.39,"pressure":1014.57,"windSpeed":3.35,"windGust":6.12,"windBearing":232,"cloudCover":0.28,"uvIndex":2,"visibility":10,"ozone":277.93},"offset":-8}'
@@ -1,26 +0,0 @@
-
request:
method: GET
url: 'https://us1.locationiq.com/v1/search.php?key=test&q=12+beverly+hills+90210+United+States&format=json'
headers:
Host: us1.locationiq.com
Accept-Encoding: null
User-Agent: 'GuzzleHttp/6.3.3 curl/7.54.0 PHP/7.1.17'
Accept: null
response:
status:
http_version: '1.1'
code: '200'
message: OK
headers:
Date: 'Mon, 17 Dec 2018 12:46:08 GMT'
Content-Type: 'application/json; charset=UTF-8'
Transfer-Encoding: chunked
Connection: keep-alive
Set-Cookie: '__cfduid=d534aca2dd5607d039bb4589b30bd50581545050768; expires=Tue, 17-Dec-19 12:46:08 GMT; path=/; domain=.locationiq.com; HttpOnly'
X-Powered-By: LocationIQ
Access-Control-Allow-Origin: '*'
Expect-CT: 'max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"'
Server: cloudflare
CF-RAY: 48a97da52bcc3f89-YUL
body: '[{"place_id":"200788090","licence":"\u00a9 LocationIQ.com CC BY 4.0, Data \u00a9 OpenStreetMap contributors, ODbL 1.0","osm_type":"relation","osm_id":"3503615","boundingbox":["34.05269","34.112456","-118.4270732","-118.3715358"],"lat":"34.0736204","lon":"-118.4003563","display_name":"Beverly Hills, Los Angeles County, California, USA","class":"place","type":"city","importance":0.72188891692726,"icon":"https:\/\/locationiq.org\/static\/images\/mapicons\/poi_place_city.p.20.png"}]'