php-actions / phpunit

Run PHPUnit tests in Github Actions.
106 stars 23 forks source link

Error:SQLSTATE[HY000] [2002] No such file or directory #26

Closed twbworld closed 3 years ago

twbworld commented 3 years ago

Github-Actions Config

jobs:
  test:
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@v2
      - uses: php-actions/composer@v5
      - uses: php-actions/phpunit@v2
        with:
          configuration: ./phpunit.xml
          version: 9.5.2
          php_version: 7.4
          php_extensions: xdebug mbstring mysqli pdo_mysql

PHP

try {
    $dsn = 'mysql:dbname=mysql;host=localhost';
    $db = new \PDO($dsn, 'root', 'root');
    $db->exec('set names utf8');
} catch (\PDOException $e) {
    echo '数据库连接失败丫 : ' . $e->getMessage();
}

Error

..E数据库连接失败丫 : SQLSTATE[HY000] [2002] No such file or directory
g105b commented 3 years ago

Hi @twbworld ,

The reason for the error message is because there is no MySQL server running at the specified hostname. Behind the scenes, PDO makes a socket connection to the specified DSN, and when a socket fails to connect it emits the error "No such file or directory" - that's a bit confusing, but that's how it is (at least with PDO).

Really the error message is synonymous with "No MySQL server running at localhost", which is accurate. So I need to ask for your help on this. Can you think of the best way we could achieve what you're trying to do?

Are you sure that creating a connection to a real database connection is the right thing to do in unit tests (rather than mocking)?

If you definitely need to unit test a real database connection, do you think it would be acceptable to run MySQL in a separate concurrent Github Actions runner, and connect to that from the PHPUnit runner? If so, could you achieve this yourself or would you like me to help accomplish this?

Thanks, Greg.

twbworld commented 3 years ago

thank you @g105b ,

ubuntu-20.04 comes with mysql8.0, so I use localhost or 127.0.0.1 to connect to it, and the result was an error.

I tried to set pdo_mysql.default_socket=/var/run/mysqld/mysqld.sock in php.ini, but still got an error:No such file or directory

Finally I gave up using php-actions/phpunit@v2

My way (mysql connection is successful)

jobs:
  unit-test:
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@v2
      - uses: php-actions/composer@v5
      - name: mysql
        run: |
          sudo systemctl restart mysql
          sudo mysqladmin -uroot -proot create db_name
          sudo mysql -uroot -proot db_name < data/users.sql
      - name: use-php7.4
        run: |
          sudo ln -sf /bin/php7.4 /bin/php
          sudo ln -sf /usr/bin/php7.4 /usr/bin/php
      - name: phpunit
        run: sudo XDEBUG_MODE=coverage ./vendor/bin/phpunit
      # - uses: php-actions/phpunit@v2
      #   with:
      #     configuration: ./phpunit.xml
      #     version: 9.5.2
      #     php_version: 7.4
      #     php_extensions: xdebug mbstring mysqli pdo_mysql
shamotj commented 3 years ago

I have similar problem with PostgreSQL. But I do have service configuration. I did test it like in example here https://github.com/actions/example-services/blob/master/.github/workflows/postgres-service.yml and it worked. But once I try to connect from phpunit, it fails with message "could not connect to server: Connection refused". Here is my configuration, hope it helps.

name: PHP Composer

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

jobs:
  build:
    runs-on: ubuntu-latest

    services:
      postgres:
        image: postgres:10.8
        env:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres
          POSTGRES_DB: postgres
        ports:
          - 5432/tcp
        options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5

    steps:
    - uses: actions/checkout@v2

    - name: Cache Composer packages
      id: composer-cache
      uses: actions/cache@v2
      with:
        path: vendor
        key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
        restore-keys: |
          ${{ runner.os }}-php-

    - uses: nanasess/setup-php@master
      with:
        php-version: '7.4'

    - name: Validate composer.json and composer.lock
      run: composer validate --strict

    - name: Install dependencies
      run: composer install --no-progress --no-suggest

    # Add a test script to composer.json, for instance: "test": "vendor/bin/phpunit"
    # Docs: https://getcomposer.org/doc/articles/scripts.md
    - name: PHPUnit tests
      uses: php-actions/phpunit@v2
      with:
        php_version: 7.4
        version: 7.5
        php_extensions: pdo pdo_pgsql
      env:
        POSTGRES_PORT: ${{ job.services.postgres.ports[5432] }}
        POSTGRES_HOST: localhost