Laravel API Test - How to test API that has external API call
<p>My API code</p>

<pre><code>public function store (Request $request, $profileId)
{
    $all = $request-&gt;all();
    $token = AccessToken::with('users')-&gt;where('access_token',$request-&gt;input('access_token'))-&gt;first();

    if($token &amp;&amp; $token-&gt;users-&gt;isOwnProfile($profileId))
    {
        $rules = [
            'access_token'     =&gt; 'required',
            'title'            =&gt; 'required',
            'description'      =&gt; 'required',
            'file_id'          =&gt; 'required',
            'audience_control' =&gt; 'required|in:' . join(',', PostRepository::$AUDIENCE_CONTROL),
            'tags'             =&gt; 'required',
        ];
        $validator = Validator::make($all, $rules);

        $error = $validator-&gt;errors()-&gt;toArray();
        if ($validator-&gt;fails())
        {
            return $this-&gt;setStatusCode(401)
                -&gt;setStatusMessage(trans('api.validation failed'))
                -&gt;respondValidationMessage($error);
        }
        try {
            $response = $this-&gt;postRepository-&gt;save($request, $profileId);
            if(isset($response['error']))
                return $this-&gt;messageSet([
                    'message' =&gt; $response['error']['message'],
                ], $response['error']['status_code']);

            return $this-&gt;setDataType('post_id')
                -&gt;setStatusCode('200')
                -&gt;respondWithCreatedId(trans('api.Post created'), $response-&gt;id);
        } catch (\Exception $e) {
            return $this-&gt;respondInternalError(trans('api.processing error'));
        }
    }
    return $this-&gt;respondInternalError('404 page');

}
</code></pre>

<p>From save method it calls another method that calls an external API.</p>

<pre><code>/*
 * this function returns some response where it has profile_id for 
 * the file which in other save function is matched that the
 * profile_id passed as parameter is same with file profile_id
 */
public function getFileDetails($file_id)
{
    try
    {
        $response = json_decode((new Client())-&gt;request('GET', env('xyz','http://abc.xyz/api/v1').'/files/' . $file_id)-&gt;getBody()-&gt;getContents(), true);
    }
    catch (RequestException $e)
    {
        $response = json_decode($e-&gt;getResponse()-&gt;getBody()-&gt;getContents(), true);
    }

    return $response;

}
</code></pre>

<p>Now this is my test function for API.</p>

<pre><code>public function testPostCreateChecksProfileMatchesCorrectly()
{
    $this-&gt;json('POST', 'profiles/' . $this-&gt;getProfile()-&gt;id . '/posts' . '?access_token=' . $this-&gt;getAccessToken(), [
        'title' =&gt; 'api testing title',
        'description' =&gt; 'api testing description',
        'audience_control' =&gt; 'public',
        'tags' =&gt; [
            'Animals',
            'City'
        ],
        'file_id' =&gt; '281'
    ])-&gt;seeJsonStructure([
        'success' =&gt; [
            'message',
            'post_id',
            'status',
        ],
    ]);
}
</code></pre>

<blockquote>
  <p><strong>Now my question is how can I create a fake response for the external</strong>
  <strong>API when I am testing.</strong></p>
</blockquote>

<p>I am using <strong><code>PHPUnit</code></strong> &amp; <strong><code>Laravel 5.2</code></strong>.</p>