Skip to content

Commit

Permalink
Improves tests.
Browse files Browse the repository at this point in the history
Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
  • Loading branch information
crynobone committed Apr 6, 2018
1 parent eb71fad commit d011c79
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 36 deletions.
14 changes: 10 additions & 4 deletions tests/One/InsurerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,27 @@

class InsurerTest extends TestCase
{
/**
* API Version.
*
* @var string
*/
private $apiVersion = 'v1';

/** @test */
public function it_can_get_list_of_insurers()
{
$headers = [
'Accept' => 'application/vnd.KATSANA.v1+json',
'Authorization' => 'Bearer '.static::ACCESS_TOKEN,
];

$faker = Faker::create()
->call('GET', $headers)
->expectEndpointIs('https://api.katsana.com/insurer/MY')
->shouldResponseWith(200, '[{"country":"MY","name":"AIA","partner":false},{"country":"MY","name":"AIG","partner":false},{"country":"MY","name":"Allianz Malaysia Berhad","partner":true},{"country":"MY","name":"AmGeneral","partner":false},{"country":"MY","name":"Axa Affin","partner":false},{"country":"MY","name":"Berjaya Sompo","partner":false},{"country":"MY","name":"Chubb","partner":false},{"country":"MY","name":"Etiqa Insurance Berhad","partner":true},{"country":"MY","name":"Etiqa Takaful Berhad","partner":true},{"country":"MY","name":"Hong Leong","partner":false},{"country":"MY","name":"Kurnia","partner":false},{"country":"MY","name":"Liberty Insurance","partner":false},{"country":"MY","name":"Lonpac","partner":false},{"country":"MY","name":"MPI Generali","partner":false},{"country":"MY","name":"MSIG","partner":false},{"country":"MY","name":"RHB Insurance","partner":false},{"country":"MY","name":"Takaful Ikhlas","partner":false},{"country":"MY","name":"Takaful Malaysia","partner":false},{"country":"MY","name":"Tokio Marine","partner":false},{"country":"MY","name":"Zurich","partner":false}]');

$response = (new Client($faker->http(), 'homestead', 'secret'))
->useVersion('v1')
$response = $this->makeClientWithAccessToken($faker)
->uses('Insurer')
->all();

Expand All @@ -43,8 +50,7 @@ public function it_can_get_list_of_insurers_without_access_token()
->expectEndpointIs('https://api.katsana.com/insurer/MY')
->shouldResponseWith(200, '[{"country":"MY","name":"AIA","partner":false},{"country":"MY","name":"AIG","partner":false},{"country":"MY","name":"Allianz Malaysia Berhad","partner":true},{"country":"MY","name":"AmGeneral","partner":false},{"country":"MY","name":"Axa Affin","partner":false},{"country":"MY","name":"Berjaya Sompo","partner":false},{"country":"MY","name":"Chubb","partner":false},{"country":"MY","name":"Etiqa Insurance Berhad","partner":true},{"country":"MY","name":"Etiqa Takaful Berhad","partner":true},{"country":"MY","name":"Hong Leong","partner":false},{"country":"MY","name":"Kurnia","partner":false},{"country":"MY","name":"Liberty Insurance","partner":false},{"country":"MY","name":"Lonpac","partner":false},{"country":"MY","name":"MPI Generali","partner":false},{"country":"MY","name":"MSIG","partner":false},{"country":"MY","name":"RHB Insurance","partner":false},{"country":"MY","name":"Takaful Ikhlas","partner":false},{"country":"MY","name":"Takaful Malaysia","partner":false},{"country":"MY","name":"Tokio Marine","partner":false},{"country":"MY","name":"Zurich","partner":false}]');

$response = (new Client($faker->http(), 'homestead', 'secret'))
->useVersion('v1')
$response = $this->makeClient($faker)
->uses('Insurer')
->all();

Expand Down
38 changes: 17 additions & 21 deletions tests/One/ProfileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,27 @@

class ProfileTest extends TestCase
{
/**
* API Version.
*
* @var string
*/
private $apiVersion = 'v1';

/** @test */
public function it_can_show_user_profile()
{
$headers = [
'Accept' => 'application/vnd.KATSANA.v1+json',
'Authorization' => 'Bearer AckfSECXIvnK5r28GVIWUAxmbBSjTsmF',
'Authorization' => 'Bearer '.static::ACCESS_TOKEN,
];

$faker = Faker::create()
->call('GET', $headers)
->expectEndpointIs('https://api.katsana.com/profile')
->shouldResponseWith(200, '{"id":73,"email":"crynobone@gmail.com","address":"Lot 2805, Jalan Damansara,\r\n60000 Kuala Lumpur.","phone_home":"60123456789","phone_mobile":"60123456789","fullname":"Mior Muhammad Zaki","meta":{"emergency":{"fullname":"","phone":{"home":"","mobile":""}}},"avatar":null,"timezone":"Asia/Kuala_Lumpur","created_at":"2016-09-06 21:23:53","updated_at":"2016-12-18 12:10:20"}');

$response = (new Client($faker->http(), 'homestead', 'secret'))
->useVersion('v1')
->setAccessToken('AckfSECXIvnK5r28GVIWUAxmbBSjTsmF')
$response = $this->makeClientWithAccessToken($faker)
->uses('Profile')
->show();

Expand All @@ -44,8 +49,7 @@ public function it_can_show_user_profile()
*/
public function it_cant_show_user_profile_without_access_token()
{
(new Client(Faker::create()->http(), 'homestead', 'secret'))
->useVersion('v1')
$this->makeClient(Faker::create())
->uses('Profile')
->show();
}
Expand All @@ -55,17 +59,15 @@ public function it_can_verify_valid_password()
{
$headers = [
'Accept' => 'application/vnd.KATSANA.v1+json',
'Authorization' => 'Bearer AckfSECXIvnK5r28GVIWUAxmbBSjTsmF',
'Authorization' => 'Bearer '.static::ACCESS_TOKEN,
];

$faker = Faker::create()
->call('POST', $headers, 'password=secret%21')
->expectEndpointIs('https://api.katsana.com/auth/verify')
->shouldResponseWith(200, '{"success":true}');

$response = (new Client($faker->http(), 'homestead', 'secret'))
->useVersion('v1')
->setAccessToken('AckfSECXIvnK5r28GVIWUAxmbBSjTsmF')
$response = $this->makeClientWithAccessToken($faker)
->uses('Profile')
->verifyPassword('secret!');

Expand All @@ -77,17 +79,15 @@ public function it_cant_verify_invalid_password()
{
$headers = [
'Accept' => 'application/vnd.KATSANA.v1+json',
'Authorization' => 'Bearer AckfSECXIvnK5r28GVIWUAxmbBSjTsmF',
'Authorization' => 'Bearer '.static::ACCESS_TOKEN,
];

$faker = Faker::create()
->call('POST', $headers, 'password=secret%21%21')
->expectEndpointIs('https://api.katsana.com/auth/verify')
->shouldResponseWith(200, '{"success":false}');

$response = (new Client($faker->http(), 'homestead', 'secret'))
->useVersion('v1')
->setAccessToken('AckfSECXIvnK5r28GVIWUAxmbBSjTsmF')
$response = $this->makeClientWithAccessToken($faker)
->uses('Profile')
->verifyPassword('secret!!');

Expand All @@ -101,8 +101,7 @@ public function it_cant_verify_invalid_password()
*/
public function it_cant_verify_valid_password_without_access_token()
{
(new Client(Faker::create()->http(), 'homestead', 'secret'))
->useVersion('v1')
$this->makeClient(Faker::create())
->uses('Profile')
->verifyPassword('secret!');
}
Expand All @@ -115,9 +114,7 @@ public function it_can_upload_avatar()
->expectEndpointIs('https://api.katsana.com/profile/avatar')
->shouldResponseWith(200, '{"url":"https://my.katsana.com/pictures/user-73/0153cf08-31e2-11e6-99b7-08002777c33d.png","thumb":"https://my.katsana.com/pictures/user-73/0153cf08-31e2-11e6-99b7-08002777c33d.thumb.png","marker":"https://my.katsana.com/pictures/user-73/0153cf08-31e2-11e6-99b7-08002777c33d.marker.png"}');

$response = (new Client($faker->http(), 'homestead', 'secret'))
->useVersion('v1')
->setAccessToken('AckfSECXIvnK5r28GVIWUAxmbBSjTsmF')
$response = $this->makeClientWithAccessToken($faker)
->uses('Profile')
->uploadAvatar(__DIR__.'/../stubs/katsana-logo.png');

Expand All @@ -136,8 +133,7 @@ public function it_can_upload_avatar()
*/
public function it_cant_upload_avatar_without_access_token()
{
(new Client(Faker::create()->http(), 'homestead', 'secret'))
->useVersion('v1')
$this->makeClient(Faker::create())
->uses('Profile')
->uploadAvatar(__DIR__.'/../stubs/katsana-logo.png');
}
Expand Down
16 changes: 10 additions & 6 deletions tests/One/WelcomeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@

class WelcomeTest extends TestCase
{
/**
* API Version.
*
* @var string
*/
private $apiVersion = 'v1';

/** @test */
public function it_can_show_welcome_message()
{
Expand All @@ -17,8 +24,7 @@ public function it_can_show_welcome_message()
->expectEndpointIs('https://api.katsana.com')
->shouldResponseWith(200, '{"platform":"v5.0.0","api":["v1"]}');

$response = (new Client($faker->http(), 'homestead', 'secret'))
->useVersion('v1')
$response = $this->makeClient($faker)
->uses('Welcome')
->show();

Expand All @@ -32,17 +38,15 @@ public function it_can_show_welcome_message_with_access_token()
{
$headers = [
'Accept' => 'application/vnd.KATSANA.v1+json',
'Authorization' => 'Bearer AckfSECXIvnK5r28GVIWUAxmbBSjTsmF',
'Authorization' => 'Bearer '.static::ACCESS_TOKEN,
];

$faker = Faker::create()
->call('GET', $headers)
->expectEndpointIs('https://api.katsana.com')
->shouldResponseWith(200, '{"platform":"v5.0.0","api":["v1"]}');

$response = (new Client($faker->http(), 'homestead', 'secret'))
->useVersion('v1')
->setAccessToken('AckfSECXIvnK5r28GVIWUAxmbBSjTsmF')
$response = $this->makeClientWithAccessToken($faker)
->uses('Welcome')
->show();

Expand Down
4 changes: 2 additions & 2 deletions tests/Passport/PasswordGrantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class PasswordGrantTest extends TestCase
public function it_can_get_access_token()
{
$headers = ['Accept' => 'application/json'];
$payloads = 'scope=%2A&grant_type=password&client_id=homestead&client_secret=secret&username=contact%40katsana.com&password=secret';
$payloads = 'scope=%2A&grant_type=password&client_id='.static::CLIENT_ID.'&client_secret='.static::CLIENT_SECRET.'&username=contact%40katsana.com&password=secret';

$faker = Faker::create()
->call('POST', $headers, $payloads)
Expand All @@ -41,7 +41,7 @@ public function it_can_get_access_token()
public function it_cant_get_access_token_when_authentication_return_other_than_200_http_status()
{
$headers = ['Accept' => 'application/json'];
$payloads = 'scope=%2A&grant_type=password&client_id=homestead&client_secret=secret&username=dummy%40katsana.com&password=secret%21';
$payloads = 'scope=%2A&grant_type=password&client_id='.static::CLIENT_ID.'&client_secret='.static::CLIENT_SECRET.'&username=dummy%40katsana.com&password=secret%21';

$faker = Faker::create()
->call('POST', $headers, $payloads)
Expand Down
22 changes: 19 additions & 3 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@

abstract class TestCase extends PHPUnit
{
const CLIENT_ID = 'homestead';
const CLIENT_SECRET = 'secret';
const ACCESS_TOKEN = 'AckfSECXIvnK5r28GVIWUAxmbBSjTsmF';

/**
* API Version.
*
* @var string
*/
protected $apiVersion;
private $apiVersion;

/**
* Teardown the test environment.
Expand All @@ -37,13 +41,25 @@ protected function tearDown(): void
protected function makeClient(Faker $faker): Client
{
$client = (new Client($faker->http()))
->setClientId('homestead')
->setClientSecret('secret');
->setClientId(static::CLIENT_ID)
->setClientSecret(static::CLIENT_SECRET);

if (! is_null($this->apiVersion)) {
$client->useVersion($this->apiVersion);
}

return $client;
}

/**
* Make KATSANA SDK Client.
*
* @param \Laravie\Codex\Testing\Faker $faker
*
* @return \Katsana\Sdk\Client
*/
protected function makeClientWithAccessToken(Faker $faker): Client
{
return $this->makeClient($faker)->setAccessToken(static::ACCESS_TOKEN);
}
}

0 comments on commit d011c79

Please sign in to comment.