Jmrtech's avatar

Integration test for twilio autoresponder

I have an application that uses twilio for a sms autoresponder. I'm running into trouble with setting the test up. Here is part of incomingsms.php:

public function smsResponse($response, Request $request, $client)
    {
        switch ($response) {
            case '1':
                $message = "Text the word refill to start a prescription request.";
                $this->twilio->responseToIncomingSms($message);
                break;
            case '2':
                $message = "Text the word food to start a food order request.";
                $this->twilio->responseToIncomingSms($message);
                break;
            default:
                $message = "Text menu for options or help to unsubscribe.";
                $this->twilio->responseToIncomingSms($message);
        }
    }

Here is the response to incoming function:

public function responseToIncomingSms($body)
    {
        $response = new Services_Twilio_Twiml();
        $response->message($body);
        echo $response;
    }

I'm not sure if part of the problem is the fact that twilio just echo's rather than return. If I have to test it manually I can but I was just trying to find a way to incorporate this with the other test I'm doing. Thanks

0 likes
4 replies
Jmrtech's avatar
public function testProviderSms()
    {
        \Session::start();

        $plan = \App\Plan::find(1);

        $hospital = factory(App\Hospital::class)->create([
            'hospitalPhone' => '3104445555',
            'mobilePhone' => '15005122523',
            'plan_id' => $plan->id
        ]);

        $user = factory(App\User::class)->create([
            'hospital_id' => $hospital->id,
        ]);

        $client = factory(App\Client::class)->create([
            'hospital_id' => $hospital->id
        ]);

        $provider = factory(App\Provider::class)->create([
            'hospital_id' => $hospital->id
        ]);

        $params = [
            '_token' => csrf_token(),
            'To' => '+'.$hospital->mobilePhone,
            'From' => '+'.$provider->phone,
            'Body' => '3105558899'
        ];

        $message = "The phone number 3105585969 did not match any clients in the system, but we can still complete the request if you are sure you have the correct number. If there is an error and you would like to start over reply \"x\" to cancel the request. Would you like to call or text 3105559988?";

    $response = new Services_Twilio_Twiml();
        $response->message($body);

        //$this->expectOutputString($response->message);


        $this->assertXmlStringEqualsXmlString($response->message, $this->post('/incomingsms', $params)->getActualOutput());
    }
ifpingram's avatar

You don't seem to be testing your code. Instead you seem to be trying to test the Services_Twilio_Twiml() object, as opposed to the smsResponse() method. The idea is surely to test your code, not your dependencies?

ctorresr's avatar

Also you should use Twilio test credentials for testing purposes. ;)

Please or to participate in this conversation.