Skip to content

Commit dd934f8

Browse files
committed
fix(tests): add test for opt and sms
1 parent a81fd10 commit dd934f8

File tree

2 files changed

+163
-0
lines changed

2 files changed

+163
-0
lines changed

tests/OTPTest.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
namespace Craftsys\Tests\Msg91;
4+
5+
use Craftsys\Msg91\Client;
6+
use GuzzleHttp\Client as HttpClient;
7+
use GuzzleHttp\Handler\MockHandler;
8+
use GuzzleHttp\HandlerStack;
9+
use GuzzleHttp\Psr7\Response;
10+
use GuzzleHttp\Middleware;
11+
use Craftsys\Msg91\Support\Response as CraftsysResponse;
12+
13+
class OTPTest extends TestCase
14+
{
15+
protected $container = [];
16+
17+
/**
18+
* Define environment setup.
19+
*
20+
* @param \Illuminate\Foundation\Application $app
21+
*
22+
* @return void
23+
*/
24+
protected function getEnvironmentSetUp($app)
25+
{
26+
$app['config']->set('services.msg91.key', 'my_api_key');
27+
}
28+
29+
public function test_send_otp()
30+
{
31+
/** @var \Craftsys\Msg91\Client $client */
32+
$client = app(Client::class);
33+
$response = $client
34+
->setHttpClient($this->createMockHttpClient())
35+
->otp()
36+
->to(91999999999)
37+
->send();
38+
39+
$this->assertInstanceOf(CraftsysResponse::class, $response);
40+
41+
// make sure there was exacly on request
42+
$this->assertCount(1, $this->container);
43+
$this->container = [];
44+
}
45+
46+
public function test_resend_otp()
47+
{
48+
/** @var \Craftsys\Msg91\Client $client */
49+
$client = app(Client::class);
50+
$response = $client
51+
->setHttpClient($this->createMockHttpClient())
52+
->otp()
53+
->to(91999999999)
54+
->viaText()
55+
->send();
56+
57+
$this->assertInstanceOf(CraftsysResponse::class, $response);
58+
59+
// make sure there was exacly on request
60+
$this->assertCount(1, $this->container);
61+
$this->container = [];
62+
}
63+
64+
public function test_verify_otp()
65+
{
66+
/** @var \Craftsys\Msg91\Client $client */
67+
$client = app(Client::class);
68+
$response = $client
69+
->setHttpClient($this->createMockHttpClient())
70+
->otp(123123)
71+
->to(91999999999)
72+
->verify();
73+
74+
$this->assertInstanceOf(CraftsysResponse::class, $response);
75+
76+
// make sure there was exacly on request
77+
$this->assertCount(1, $this->container);
78+
$this->container = [];
79+
}
80+
81+
protected function createMockHttpClient(
82+
$status_code = 200,
83+
$body = [
84+
"type" => "success", "message" => "Send successfully"
85+
]
86+
): HttpClient {
87+
$history = Middleware::history($this->container);
88+
$mock = new MockHandler([
89+
new Response($status_code, [], json_encode($body)),
90+
]);
91+
92+
$handler = HandlerStack::create($mock);
93+
$handler->push($history);
94+
95+
$client = new HttpClient(['handler' => $handler]);
96+
return $client;
97+
}
98+
}

tests/SMSTest.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace Craftsys\Tests\Msg91;
4+
5+
use Craftsys\Msg91\Client;
6+
use GuzzleHttp\Client as HttpClient;
7+
use GuzzleHttp\Handler\MockHandler;
8+
use GuzzleHttp\HandlerStack;
9+
use GuzzleHttp\Psr7\Response;
10+
use GuzzleHttp\Middleware;
11+
use Craftsys\Msg91\Support\Response as CraftsysResponse;
12+
13+
class SMSTest extends TestCase
14+
{
15+
protected $container = [];
16+
17+
/**
18+
* Define environment setup.
19+
*
20+
* @param \Illuminate\Foundation\Application $app
21+
*
22+
* @return void
23+
*/
24+
protected function getEnvironmentSetUp($app)
25+
{
26+
$app['config']->set('services.msg91.key', 'my_api_key');
27+
}
28+
29+
public function test_send_otp()
30+
{
31+
/** @var \Craftsys\Msg91\Client $client */
32+
$client = app(Client::class);
33+
$response = $client
34+
->setHttpClient($this->createMockHttpClient())
35+
->sms()
36+
->flow('flow_id')
37+
->to(91999999999)
38+
->variable('day', 'this')
39+
->send();
40+
41+
$this->assertInstanceOf(CraftsysResponse::class, $response);
42+
43+
// make sure there was exacly on request
44+
$this->assertCount(1, $this->container);
45+
$this->container = [];
46+
}
47+
48+
protected function createMockHttpClient(
49+
$status_code = 200,
50+
$body = [
51+
"type" => "success", "message" => "Send successfully"
52+
]
53+
): HttpClient {
54+
$history = Middleware::history($this->container);
55+
$mock = new MockHandler([
56+
new Response($status_code, [], json_encode($body)),
57+
]);
58+
59+
$handler = HandlerStack::create($mock);
60+
$handler->push($history);
61+
62+
$client = new HttpClient(['handler' => $handler]);
63+
return $client;
64+
}
65+
}

0 commit comments

Comments
 (0)