|
| 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 | +} |
0 commit comments