Created
December 5, 2019 06:19
-
-
Save nicekiwi/bd455f3c530a0de3255f53d85ae00a9a to your computer and use it in GitHub Desktop.
Laravel Mockery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Tests\Feature; | |
use App\Services\Addy\Models\AddressDetails; | |
use GuzzleHttp\Client; | |
use Tests\TestCase; | |
class AddyAddressLookupTest extends TestCase | |
{ | |
/** | |
* A basic feature test example. | |
* | |
* @return void | |
*/ | |
public function testAddressSearch() | |
{ | |
$response = $this->actingAs($this->resolveRandomUser()) | |
->json( | |
'GET', | |
'/address-lookup/search', | |
[ | |
'address' => '366A Tuam St', | |
] | |
); | |
$response->assertStatus(200) | |
->assertJsonStructure([ | |
'data' => [ | |
[ | |
'id', | |
'address', | |
] | |
], | |
]); | |
} | |
/** | |
* Expect error message from lookup controller | |
*/ | |
public function testAddressSearchFail() | |
{ | |
$response = $this->actingAs($this->resolveRandomUser()) | |
->json( | |
'GET', | |
'/address-lookup/search', | |
[ | |
'address' => 'a', | |
] | |
); | |
$response->assertStatus(422) | |
->assertExactJson([ | |
'message' => 'The given data was invalid.', | |
'errors' => [ | |
'address' => [ | |
'Please enter at least 2 characters.', | |
], | |
], | |
]); | |
} | |
/** | |
* | |
*/ | |
public function testFetchAddressDetailsFromCache() | |
{ | |
$id = 2417575; | |
$json = '{"id":2417575,"dpid":1540203,"linzid":2027703,"parcelid":6917300,"meshblock":438102,"number":"80","rdnumber":"","alpha":"","unittype":"","unitnumber":"","floor":"","street":"Queen Street","suburb":"Auckland Central","city":"Auckland","mailtown":"Auckland","territory":"Auckland","region":"Auckland","postcode":"1010","building":"","full":"80 Queen Street, Auckland Central, Auckland 1010","displayline":"80 Queen Street","address1":"80 Queen Street","address2":"Auckland Central","address3":"Auckland 1010","address4":"","type":"Urban","boxbagnumber":"","boxbaglobby":"","x":"174.766408","y":"-36.846255","modified":"2018-01-22","paf":true,"deleted":false}'; | |
$decodedJson = json_decode(response($json)->content(), true); | |
// Set the cache | |
\Cache::shouldReceive('get')->andReturn($decodedJson); | |
// Make sure the actual request is not called | |
$this->mock(Client::class, function ($mock) { | |
$mock->shouldReceive('get')->never(); | |
}); | |
$response = $this->actingAs($this->resolveRandomUser()) | |
->json( | |
'GET', | |
'/address-lookup/details/' . $id, | |
); | |
$response->assertStatus(200) | |
->assertJson(compact('id')) | |
->assertJsonStructure((new AddressDetails())->getFillable()); | |
} | |
public function testFetchAddressDetailsFromAddy() | |
{ | |
$id = 2417575; | |
$json = '{"id":2417575,"dpid":1540203,"linzid":2027703,"parcelid":6917300,"meshblock":438102,"number":"80","rdnumber":"","alpha":"","unittype":"","unitnumber":"","floor":"","street":"Queen Street","suburb":"Auckland Central","city":"Auckland","mailtown":"Auckland","territory":"Auckland","region":"Auckland","postcode":"1010","building":"","full":"80 Queen Street, Auckland Central, Auckland 1010","displayline":"80 Queen Street","address1":"80 Queen Street","address2":"Auckland Central","address3":"Auckland 1010","address4":"","type":"Urban","boxbagnumber":"","boxbaglobby":"","x":"174.766408","y":"-36.846255","modified":"2018-01-22","paf":true,"deleted":false}'; | |
// Disable the cache | |
\Cache::shouldReceive('get')->andReturnFalse(); | |
// Stub the Client Response | |
$this->mock(Client::class, function ($mock) use ($json) { | |
$mock->shouldReceive('get')->once()->andReturn(response($json)); | |
}); | |
$response = $this->actingAs($this->resolveRandomUser()) | |
->json( | |
'GET', | |
'/address-lookup/details/' . $id, | |
); | |
$response->assertStatus(200) | |
->assertJson(compact('id')) | |
->assertJsonStructure((new AddressDetails())->getFillable()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment