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

automica's avatar

Tests fail in pipelines

I've been setting up my current laravel 8 project with a pipeline to run tests on bitbucket before publishing to my live server.

My application is able to migrate and seed data ok but when I run the php artisan test step I get failures for the feature tests.

A typical feature test looks like:

    /** @test */
    public function gets_list_of_activities(): void
    {
        $response = $this->get('/api/v1/activities');

        $response
            ->assertSuccessful();
    }

and the failure message is

 SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known 

and full bitbucket pipeline

image: php:7.4-fpm

pipelines:
  branches:
    '{develop,feature/*}':
      - step:
          name: Run tests and deploy to dev
          deployment: test
          caches:
            - composer
          services:
            - mysql
          artifacts: # defining the artifacts to be passed to each future step.
            - storage/logs/*.log
            - reports/*.txt
          after-script:
            - find /opt/atlassian/pipelines/agent/build/ -name *laravel*.log
          script:
            - echo "master, dev, or feature/*"
            - mkdir /usr/share/man/man1/
            - apt-get update && apt-get install -qy git zip curl libmcrypt-dev default-mysql-client libxml2-dev default-jre wget maven
           # - yes | pecl install mcrypt-1.0.1
            - docker-php-ext-install pdo_mysql bcmath
            - curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
            - composer install
            - ln -f -s .env.pipelines .env
            - php artisan optimize:clear
            - php artisan migrate:fresh
            - php artisan db:seed
            - php artisan serve &
            - sleep 5
            - php artisan test
definitions:
  services:
    mysql:
      image: mysql:5.7
      command: --max_allowed_packet=100M
      environment:
        MYSQL_DATABASE: 'homestead'
        MYSQL_RANDOM_ROOT_PASSWORD: 'yes'
        MYSQL_USER: 'homestead'
        MYSQL_PASSWORD: 'secret'

Does anyone know what I should do with DNS so phpunit can access the application?

0 likes
7 replies
laracoft's avatar

Can show code of the test that is causing this error?

SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known

I normally fake my HTTP requests.

1 like
automica's avatar

@laracoft in my .env for my pipeline I'm specifying host as

APP_URL=http://127.0.0.1:8000
DB_HOST=127.0.0.1

The code I'm running my test against is:

class IndexController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return AnonymousResourceCollection
     */
    public function __invoke(): AnonymousResourceCollection
    {
        return ActivityResource::collection(Activity::paginate());
    }
}

When I run php artisan migrate the tables are being created and I'm then able to seed data into it. This suggests application is running in the pipeline.

As for faking the http requests, I'd do that I was hitting an external api but these tests are to test my endpoints work so need to hit the live local route.

laracoft's avatar

I meant the YAML file, not the .env, is the MYSQL_HOST missing?

I'm trying to understand the nature of your error. I think it was caused by some PHP code related to SQL.

SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known

1 like
automica's avatar

@laracoft looks like the issue was to do with .env variables not being set correctly on the pipelines, atleast the respect of phpunit not being able to see what was in .env.

I've added

        <server name="DB_HOST" value="127.0.0.1" />
        <server name="DB_USERNAME" value="homestead" />
        <server name="DB_PASSWORD" value="secret" />
        <server name="DB_DATABASE" value="homestead" />

into phpunit.xml for the pipeline and it can see the db correctly now when I run my tests.

I'm not 100% sure why that doesn't work without it but at least I can carry on my day...

laracoft's avatar

Ok great :)

Btw, what I do is to add <server name="DB_CONNECTION" value="testing"/> in my phpunit.xml, this will use the info found in config/database.php under the key testing.

This way, all my phpunit.xml are the standardized.

automica's avatar

@laracoft I've got a separate .env.testing, which will get copied to .env to switch database settings when php artisan test is ran.

The issue seems to be that when I cache clear on the pipeline docker that its not clearing config cache.

Please or to participate in this conversation.