diff --git a/tests/OTPTest.php b/tests/OTPTest.php new file mode 100644 index 0000000..cba59c6 --- /dev/null +++ b/tests/OTPTest.php @@ -0,0 +1,98 @@ +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; + } +} diff --git a/tests/SMSTest.php b/tests/SMSTest.php new file mode 100644 index 0000000..ae9d0d4 --- /dev/null +++ b/tests/SMSTest.php @@ -0,0 +1,65 @@ +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; + } +}