DEV Community

Cover image for Github Actions for Symfony 5 PHPUnit and more
Romain
Romain

Posted on

Github Actions for Symfony 5 PHPUnit and more

My Workflow

This Github Actions will help you to guarantee an optimal quality and the good functioning of the code thanks to the launching of automatic tests.
But also warn you about the known vulnerabilities of your dependencies.

PHP Setup

First, this action setup PHP, composer and install all composer dependencies.

Security / Quality

After that, it check vulnerabilities in your dependencies and check your quality code according to PSR12 coding style using PHP_CodeSniffer and PHPStan

Webpack Encore

For the Webpack Encore part, it install all npm dependencies and build all your assets.

Tests

Finally, it run your PHPUnit tests. (and Behat, if you have).

Submission Category:

Maintainer Must-Haves

Yaml File

name: Symfony 5 Tests
on:
  push:
    branches:
      - main
      - dev
  pull_request:

jobs:
  symfony:
    name: Symfony 5.0 (PHP ${{ matrix.php-versions }})
    # https://hub.docker.com/_/ubuntu/
    runs-on: ubuntu-latest
    strategy:
      fail-fast: true
      matrix:
        php-versions: ['7.4']
    steps:
      # https://github.com/actions/checkout (official)
      - name: Checkout
        uses: actions/checkout@v2

      # https://github.com/shivammathur/setup-php (community)
      - name: Setup PHP, extensions and composer with shivammathur/setup-php
        uses: shivammathur/setup-php@verbose
        with:
          php-version: ${{ matrix.php-versions }}
          extensions: mbstring, xml, ctype, iconv, intl, pdo_sqlite, dom, filter, gd, iconv, json, mbstring, pdo

      # Composer
      - name: Get composer cache directory
        id: composer-cache
        run: echo "::set-output name=dir::$(composer config cache-files-dir)"

        # https://help.github.com/en/actions/configuring-and-managing-workflows/caching-dependencies-to-speed-up-workflows
      - name: Cache composer dependencies
        uses: actions/cache@v1
        with:
          path: ${{ steps.composer-cache.outputs.dir }}
          key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
          restore-keys: ${{ runner.os }}-composer-

      - name: Install Composer dependencies
        run: composer install --no-progress --no-suggest --prefer-dist --optimize-autoloader

      # https://github.com/sensiolabs/security-checker
      - name: Security check installed dependencies
        uses: symfonycorp/security-checker-action@v2

      # https://github.com/chekalsky/phpcs-action (community)
      - name: Check PSR12 code style (PHP_CodeSniffer)
        uses: chekalsky/phpcs-action@v1.2.0
        with:
          enable_warnings: true
          installed_paths: '${{ github.workspace }}/vendor/squizlabs/php_codesniffer'
          phpcs_bin_path: './vendor/bin/phpcs src --ignore="Migrations/"'

      # https://github.com/phpmd/phpmd
      # - name: Analyses PHP Code (PHP Mess Detector)
      #  run: vendor/bin/phpmd src,tests text .phpmd-ruleset.xml

      # https://github.com/phpstan/phpstan
      - name: Analyse PHP Code (PHPStan)
        run: vendor/bin/phpstan analyse src

      - name: Cache node_modules
        uses: actions/cache@v1
        id: yarn-cache-node-modules
        with:
          path: node_modules
          key: ${{ runner.os }}-yarn-cache-node-modules-${{ hashFiles('**/yarn.lock') }}
          restore-keys: |
            ${{ runner.os }}-yarn-cache-node-modules-

      - name: Yarn install
        if: steps.yarn-cache-node-modules.outputs.cache-hit != 'true'
        run: yarn install

      - name: Yarn build
        run: yarn run encore production

      - name: Archive production artifacts
        uses: actions/upload-artifact@v1
        with:
          name: build
          path: public/build

      # Symfony
      - name: Check the Symfony console
        run: |
          php bin/console -V
          php bin/console about
      # Tests
      - name: Run unit and functional tests
        run: |
          php bin/phpunit --stop-on-failure

      # - name: Run Behat/Mink tests
      #  run: |
      #    php vendor/bin/behat
Enter fullscreen mode Exit fullscreen mode

Additional Resources / Info

You can remove all yarn related parts if you don't use Webpack Encore

Top comments (3)

Collapse
 
insan3 profile image
Oscar Nevárez

I'd love to see an Github Action that stack up Flex recipes. Actually, are flex recipes dead already? It's been a long time since my Sf days

Collapse
 
romaixn profile image
Romain

Under what circumstances could recipes be interesting in a Github Actions?

Collapse
 
insan3 profile image
Oscar Nevárez • Edited

Probably just my imagination and no real-world usage here but...
I guess you could use it to compose base applications then put your on top of that base.

Smth like:

-
- uses romain/flex
   with:
     - recipe1
     - recipe2
     - recipe3
- uses: actions/checkout@v2
- merge, test, build variant, etc [...]
Enter fullscreen mode Exit fullscreen mode