Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jerlandsson's avatar

gulp TDD fails - curl works

Hello again.

I'm trying to get back on the horse with TDD and laravel after being stuck in a legacy project for two years. So - I'm doing a standard test to see if my API endpoint exists - like this:

    public function testThatModuleApiEndpointExists()
    {
        $this->json('GET', $this->baseUrl.'/modules' )->seeStatusCode( 200 );
    }

Which gives me the response:

1) ModulesTest::testThatModuleApiEndpointExists
Failed asserting that 500 matches expected 200.

/Users/jonaserlandsson/www/gar.dev/api/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php:366
/Users/jonaserlandsson/www/gar.dev/api/tests/ModulesTest.php:9

But when I do it with cURL -i I get a proper response:

→ curl http://api.gar.dev/modules -i
HTTP/1.1 200 OK
Server: nginx/1.9.9
Content-Type: application/json
Transfer-Encoding: chunked
Connection: keep-alive
Cache-Control: private, must-revalidate
ETag: "3f296f04e70e3816917904a8c72dca6d"
Date: Sat, 12 Mar 2016 16:42:40 GMT

{"active":[{"id":1,"name":"aModule","version":"1.0","active":1,"website":"http:\/\/www.someURL","author":"someCompany","email":"email@mail.com","description":"Lorem Ipsum","created_at":"2016-03-10 13:54:21","updated_at":"2016-03-10 13:54:21","dependencies":[]}],"inactive":[{"id":2,"name":"InactiveModule","version":"1.0","active":0,"website":"http:\/\/www.webaddress.com","author":"anotherCompany","email":"email@mail.com","description":"Lorem Ipsum","created_at":"2016-03-10 13:54:21","updated_at":"2016-03-10 13:54:21","dependencies":[{"name":"aModule","version":"1.0"},{"name":"SomeOtherModule","version":"1.0"}]},{"id":3,"name":"AnotherInactiveModule","version":"2.0","active":0,"website":"http:\/\/www.someURL.com","author":"Webbish","email":"email@mail.com","description":"Lorem Ipsum","created_at":"2016-03-10 13:54:21","updated_at":"2016-03-10 13:54:21","dependencies":[]}]}%

My test-file:

<?php

class ModulesTest extends TestCase
{

    public function testThatModuleApiEndpointExists()
    {
        $this->json('GET', $this->baseUrl.'/modules' )->seeStatusCode( 200 );
    }

    public function testActivatingModuleThatDoesNotHaveAllDependenciesShouldReturnA412WithAnErrorMessage()
    {
        $this->json( 'POST', 'modules/2/activate' )->seeStatusCode( 412 )->seeJsonStructure( [
            'error' => [
                'message',
                'code'
            ]
        ] );
    }
}

Am I missing something vital here? I've also tried without $this->baseUrl so that shouldn't be the case.

0 likes
1 reply
jerlandsson's avatar
jerlandsson
OP
Best Answer
Level 19

Ok. So I found out that the problem relates to that I'm running the tests on my localhost while the webserver runs in a vagrant box. I found out by changing the call to the URI like this instead:

    public function testThatModuleApiEndpointExists()
    {
            $response = $this->call('GET', '/modules');
            var_dump($response);
    }

which gave me the clue on:

Starting test 'ModulesTest::testThatModuleApiEndpointExists'.
object(Dingo\Api\Http\Response)#350 (9) {
  ["binding":protected]=>
  NULL
  ["original"]=>
  array(4) {
    ["message"]=>
    string(41) "SQLSTATE[HY000] [2002] Connection refused"
    ["code"]=>
    int(2002)
    ["status_code"]=>
    int(500)
    ["debug"]=>
    array(4) {
      ["line"]=>
      int(55)
      ["file"]=>
      string(112) "/Users/developer/www/gar.dev/api/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php"
      ["class"]=>
      string(12) "PDOException"
      ["trace"]=>
      array(68) {
        [0]=>
        string(189) "#0 /Users/developer/www/gar.dev/api/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php(55): PDO->__construct('mysql:host=127....', 'homestead', 'secret', Array)"
        [1]=>

After updating the .env to DB_HOST=192.168.10.10 all is good. Hope that can help someone else...

Please or to participate in this conversation.