drewdan
3 months ago

Testing Guzzle Api Call

Posted 3 months ago by drewdan

Hi All,

I am trying to do a feature test which will validate that a user can pass data to insightly CRM. However, I of course only want to mock this, and not ping Insightly everytime I run the script.

I figured I had to mock the client, pass this into the service container and I could then run my feature test, however, every time I do, it still hits the Insightly server.

/** @test */
    public function testStoreInsightly() {
        $client = $this->createMockClient(200, json_encode(['status' => 200]));
        $this->app->bind(Client::class, $client);


        $params = [
            '[email protected]' => [
            'method' => 'post',
            'route' => '/crms',
            'responseCode' => 302,
            'assertData' => '',
            'affectedTable' => '',
            'testData' => [
                    'crm' => 'insightly',
                    'key' => '0a71976b-bcb2-b8bd-221a-0cea63510721',
                ],
            ],
        ];
        $response = $this->featureTest($params);
    }

    public function createMockClient($status = 200, $body = '') {
        $mock = new MockHandler([
            new Response($status, [], $body),
        ]);
        $stack = HandlerStack::create($mock);
        return new Client(['handler' => $stack]);
    }

that is my test.

The feature test function it calls to run the test is:

protected function featureTest($testParams) {
        foreach ($testParams as $user => $params) {
            foreach ($params as $key => $param) {
                $$key = $param;
            }

            $this->signIn($user);
            $response = $this->$method($route, $testData);

            if ($assertData === true) {
                $this->assertDatabaseHas($affectedTable, $testData);
            } elseif ($assertData === false) {
                $this->assertDatabaseMissing($affectedTable, $testData);
            }
            $response->assertStatus($responseCode);
            //return($response);
        }
    }

I am unsure as to how to get this work. I thought mocking the Guzzle client would be it, but every time I get a response from the server.

Any advice or tips would be great!

Please sign in or create an account to participate in this conversation.