-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(tests): add test for opt and sms
- Loading branch information
Showing
2 changed files
with
163 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
|
||
namespace Craftsys\Tests\Msg91; | ||
|
||
use Craftsys\Msg91\Client; | ||
use GuzzleHttp\Client as HttpClient; | ||
use GuzzleHttp\Handler\MockHandler; | ||
use GuzzleHttp\HandlerStack; | ||
use GuzzleHttp\Psr7\Response; | ||
use GuzzleHttp\Middleware; | ||
use Craftsys\Msg91\Support\Response as CraftsysResponse; | ||
|
||
class OTPTest extends TestCase | ||
{ | ||
protected $container = []; | ||
|
||
/** | ||
* Define environment setup. | ||
* | ||
* @param \Illuminate\Foundation\Application $app | ||
* | ||
* @return void | ||
*/ | ||
protected function getEnvironmentSetUp($app) | ||
{ | ||
$app['config']->set('services.msg91.key', 'my_api_key'); | ||
} | ||
|
||
public function test_send_otp() | ||
{ | ||
/** @var \Craftsys\Msg91\Client $client */ | ||
$client = app(Client::class); | ||
$response = $client | ||
->setHttpClient($this->createMockHttpClient()) | ||
->otp() | ||
->to(91999999999) | ||
->send(); | ||
|
||
$this->assertInstanceOf(CraftsysResponse::class, $response); | ||
|
||
// make sure there was exacly on request | ||
$this->assertCount(1, $this->container); | ||
$this->container = []; | ||
} | ||
|
||
public function test_resend_otp() | ||
{ | ||
/** @var \Craftsys\Msg91\Client $client */ | ||
$client = app(Client::class); | ||
$response = $client | ||
->setHttpClient($this->createMockHttpClient()) | ||
->otp() | ||
->to(91999999999) | ||
->viaText() | ||
->send(); | ||
|
||
$this->assertInstanceOf(CraftsysResponse::class, $response); | ||
|
||
// make sure there was exacly on request | ||
$this->assertCount(1, $this->container); | ||
$this->container = []; | ||
} | ||
|
||
public function test_verify_otp() | ||
{ | ||
/** @var \Craftsys\Msg91\Client $client */ | ||
$client = app(Client::class); | ||
$response = $client | ||
->setHttpClient($this->createMockHttpClient()) | ||
->otp(123123) | ||
->to(91999999999) | ||
->verify(); | ||
|
||
$this->assertInstanceOf(CraftsysResponse::class, $response); | ||
|
||
// make sure there was exacly on request | ||
$this->assertCount(1, $this->container); | ||
$this->container = []; | ||
} | ||
|
||
protected function createMockHttpClient( | ||
$status_code = 200, | ||
$body = [ | ||
"type" => "success", "message" => "Send successfully" | ||
] | ||
): HttpClient { | ||
$history = Middleware::history($this->container); | ||
$mock = new MockHandler([ | ||
new Response($status_code, [], json_encode($body)), | ||
]); | ||
|
||
$handler = HandlerStack::create($mock); | ||
$handler->push($history); | ||
|
||
$client = new HttpClient(['handler' => $handler]); | ||
return $client; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
namespace Craftsys\Tests\Msg91; | ||
|
||
use Craftsys\Msg91\Client; | ||
use GuzzleHttp\Client as HttpClient; | ||
use GuzzleHttp\Handler\MockHandler; | ||
use GuzzleHttp\HandlerStack; | ||
use GuzzleHttp\Psr7\Response; | ||
use GuzzleHttp\Middleware; | ||
use Craftsys\Msg91\Support\Response as CraftsysResponse; | ||
|
||
class SMSTest extends TestCase | ||
{ | ||
protected $container = []; | ||
|
||
/** | ||
* Define environment setup. | ||
* | ||
* @param \Illuminate\Foundation\Application $app | ||
* | ||
* @return void | ||
*/ | ||
protected function getEnvironmentSetUp($app) | ||
{ | ||
$app['config']->set('services.msg91.key', 'my_api_key'); | ||
} | ||
|
||
public function test_send_otp() | ||
{ | ||
/** @var \Craftsys\Msg91\Client $client */ | ||
$client = app(Client::class); | ||
$response = $client | ||
->setHttpClient($this->createMockHttpClient()) | ||
->sms() | ||
->flow('flow_id') | ||
->to(91999999999) | ||
->variable('day', 'this') | ||
->send(); | ||
|
||
$this->assertInstanceOf(CraftsysResponse::class, $response); | ||
|
||
// make sure there was exacly on request | ||
$this->assertCount(1, $this->container); | ||
$this->container = []; | ||
} | ||
|
||
protected function createMockHttpClient( | ||
$status_code = 200, | ||
$body = [ | ||
"type" => "success", "message" => "Send successfully" | ||
] | ||
): HttpClient { | ||
$history = Middleware::history($this->container); | ||
$mock = new MockHandler([ | ||
new Response($status_code, [], json_encode($body)), | ||
]); | ||
|
||
$handler = HandlerStack::create($mock); | ||
$handler->push($history); | ||
|
||
$client = new HttpClient(['handler' => $handler]); | ||
return $client; | ||
} | ||
} |