Wednesday, April 28, 2021

Deploy Symfony project on AWS ElasticBeanstalk using Github Actions

 In this article I will build a simple CI/CD flow which will have the following steps:

- push code to master branch and trigger Github Action to start

- run composer install

- run any tests or static analysis tools you have, for this example I will just run PHPStan

- if everything goes OK wrap all in a zip file and deploy it to an existing AWS ElasticBeanstalk environment.

- update database as a postdeploy step

I use Github Secrets for storing AWS key access but for the other parameters from .env that I want to override I store them directly in the AWS ElasticBeanstalk > Environment > Configuration > Software.

Example of using secrets parameters in yaml config file:

  aws_access_key: ${{ secrets.AWS_ACCESS_KEY_ID }}
  aws_secret_key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}

In order to get started with Github Actions add to the root of your project a folder called ".github" and inside of it another one called  "workflows" and finally a file with a name for example "my_workflow.yml".

.github/workflows/my_workflow.yml

In my example the Symfony project is in a subdirectory of the repository so I have to set a working directory and also pass it as parameter for composer.

# This is a basic workflow to help you get started with Actions

name: Build and test

# Controls when the action will run. 
on:
  # Triggers the workflow on push or pull request events but only for the master branch
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:
jobs:
  php-unit-and-functional-tests:
    runs-on: ubuntu-20.04
    defaults:
      run:
        working-directory: ./html
    strategy:
      fail-fast: true
      matrix:
        php-versions: ['7.4']
    steps:
      # —— Setup Github actions —————————————————————————————————————————————
      # https://github.com/actions/checkout (official)
      - name: Git checkout placeholder-service
        uses: actions/checkout@v2
      # https://github.com/shivammathur/setup-php (community)
      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php-versions }}
          coverage: none
          tools: composer:v2
          extensions: mbstring, xml, ctype, iconv, intl, pdo_sqlite, dom, filter, gd, iconv, json, mbstring, pdo
        env:
          update: true
      - name: Check PHP Version
        run: php -v
    #  --- Install backend dependencies (Composer) ----------------
      - uses: "ramsey/composer-install@v1"
        with:
          composer-options: "--ignore-platform-reqs --working-dir=html"
      # —— Symfony ——————————————————————————————————————————————————————————
      - name: Check the Symfony console
        run: bin/console -V
      # —— PHPStan --------------------------
      - name: PHPstan
        run: composer phpstan

      - name: Generate deployment package
        run: zip -r deploy.zip . -x '*.git*'

      - name: Deploy to EB
        uses: einaregilsson/beanstalk-deploy@v16
        with:
          aws_access_key: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws_secret_key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          application_name: MyApp
          environment_name: MyApp-env
          version_label: ${{ github.sha }}
          region: us-west-2
          deployment_package: ./html/deploy.zip

After pushing a commit to master branch you can go your Github account and check the Actions tab to see the result.
In order to update database after deploy you will need to add a postdeploy step (this is a AWS ElasticBeanstalk feature). Create in your project the following folders structure:

.platform/hooks/postdeploy/01-database_update.sh 

In my case this is inside the html folder because I deploy only the content of that folder. The file 01-database_update.sh looks like this:



#!/bin/bash
#Create a copy of the environment variable file to be accessible for other CLI scripts
cp /opt/elasticbeanstalk/deployment/env /opt/elasticbeanstalk/deployment/custom_env_var

#Set permissions to the custom_env_var file so this file can be accessed by any user on the instance. You can restrict permissions as per your requirements.
chmod 644 /opt/elasticbeanstalk/deployment/custom_env_var

#Remove duplicate files upon deployment.
rm -f /opt/elasticbeanstalk/deployment/*.bak

#export env vars and update database
export $(cat /opt/elasticbeanstalk/deployment/env | xargs) && php bin/console doctrine:schema:update --force

There is an extra effort to make available to the script the environment variables (the one I setted in AWS ElasticBeanstalk > Environment > Configuration > Software) as I do not store them in git repository and they are specific for this environment.

Extra

Fine tune your ElasticBeanstalk environment using .ebextensions folder. These extensions are executed in the order of the files names so it's a good practice to call them  "01-my_extension.config" "02-my_other_action.config". Make sure to have the extension ".config".
For example you may want to set the right public directory (you can do this action also from AWS web interface). We will add a file called "01-environment.conf"


option_settings:
  "aws:elasticbeanstalk:container:php:phpini":
    document_root: /public
    memory_limit: 512M

Check AWS docs on how to tweak this here:  https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_PHP.container.html

And now you can just sit back and enjoy your state of art build and deploy flow.

Saturday, February 20, 2021

Doctrine: Given object is not an instance of the class this property was declared in

 This error confused me a little bit. I was trying to add an EntityType field to my form and I was keep getting this error. 

Executing:

php bin/console doctrine:schema:validate

Was showing that the Doctrine annotations were correctly set, but still the error was there. Trying to manually query all records from that entity in controller showed me the problem:

$companies = $em->getRepository(Company::class)->findAll();

Looking at the list of companies it was a list of objects from User class! Evrika!  I've made the repository class for Company class by copy pasting it from User class and I forgot to change the entityClass parameter in constructor:

parent::__construct($registry, Company::class);

Have a bug free day! 

Tuesday, December 15, 2020

Watch out! XDebug 3 will not work with some older PHPStorm versions

 I lost like half a day till a figure it out why XDebug from my Docker container will not work properly with PHPStorm.

In Preferences > Languages & Frameworks > PHP, add a new CLI Interpreter. Choose the Docker option, and PHPStorm will automatically find the Xdebug image for you.

At the end of this step I would see correct version of PHP, correct version of XDebug but an error message like: Xdebug: [Step Debug] Could not connect to debugging client (tried using xdebug.client_host ..).

It was driving me nuts, the debugger would stop at first line in code and when going to next breakpoint would just idle.

Short term solution: edit dockerfile to install older XDebug version:

# install xdebug
RUN pecl install xdebug-2.6.1
RUN docker-php-ext-enable xdebug

Long term solution: buy a newer PHPStorm version :) 

Tuesday, October 22, 2019

Symfony file upload mysterious error

Uncaught PHP Exception Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException: "The file "" does not exist" at /var/app/current/vendor/symfony/symfony/src/Symfony/Component/HttpFoundation/File/MimeType/MimeTypeGuesser.php line 116 {"exception":"[object] (Symfony\\Component\\HttpFoundation\\File\\Exception\\FileNotFoundException(code: 0): The file \"\" does not exist at /var/app/current/vendor/symfony/symfony/src/Symfony/Component/HttpFoundation/File/MimeType/MimeTypeGuesser.php:116)"}


If you see this error the odds are high that you are trying to upload a file larger than the max values set in you php.ini config file (upload_max_filesize and post_max_size).

Wednesday, October 2, 2019

Symfony service container: binding arguments by name or type

This cool feature was introduced in Symfony 3.4 (https://symfony.com/blog/new-in-symfony-3-4-local-service-binding) but I didn't use till today.

Example: create a custom Monolog channel and inject the service in controller (Symfony 4).
I was thinking that I will have to create some sort of custom service definition in order to inject the service "monolog.logger.mychannel" into my controller, but no, it was way easier.

In services.yaml  just add:



services:
    # default configuration for services in *this* file
    _defaults:
        bind:
            # pass this service to any $mychannelLogger argument for any
            # service that's defined in this file
            $mychannelLogger: '@monolog.logger.mychannel'

Now is super easy to inject the service in controllers by adding "$mychannelLogger" in the list of function parameters:


/**
     * @Route("/{id}", name="project_update", methods={"POST"})
     */
    public function delete(Request $request, LoggerInterface $mychannelLogger): Response
    {
    


Read more about it in Symfony docs: https://symfony.com/doc/current/service_container.html#services-binding


Wednesday, September 18, 2019

Custom user provider using multiple database connections with Doctrine and Symfony 4

Situation :

I am using Symfony 4.3 and autowiring.

I have 2 Doctrine database connections, a default and "users" connection:



doctrine:
    dbal:
        default_connection: default
        connections:

           .....
    orm:
        auto_generate_proxy_classes: true
        default_entity_manager: default
        entity_managers:
            default:
                connection: default
                mappings:
                    Main:
                        is_bundle: false
                        type: annotation
                        dir: '%kernel.project_dir%/src/Entity/Main'
                        prefix: 'App\Entity\Main'
                        alias: Main
            users:
                connection: users
                mappings:
                    Users:
                        is_bundle: false
                        type: annotation
                        dir: '%kernel.project_dir%/src/Entity/UsersManagement'
                        prefix: 'App\Entity\UsersManagement'
                        alias: Users

So my User entity was managed by the secondary connection "users". Checking from command line would show me that everything is mapped correctly:

php bin/console doctrine:mapping:info --em=users

 Found 7 mapped entities:

 [OK]   App\Entity\UsersManagement\User

Also when loading users I am using a customer query by making UserRepository implement the UserLoaderInterface. (https://symfony.com/doc/current/security/user_provider.html).

security:
    # https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
    providers:
        app_user_provider:
            entity:
                class: App\Entity\UsersManagement\User

But when I try to login, ugly surprise this error pops:
 "The class 'App\Entity\UsersManagement\User' was not found in the chain configured namespaces App\Entity\Main" 

 After googling a little bit I get to this life saving comment from stof: https://github.com/symfony/symfony/pull/8187#issuecomment-18910167


Here is how you configure it:

security:
    providers:
        my_provider:
            entity:
                class: Acme\DemoBundle\Entity\User
                manager_name: users  #non default entity manager

So there is a 'secret' parameter "manager_name" that you need to add to security.yml and is not enough that the entity is correctly mapped by Doctrine.

If somebody ever finds this useful please add a comment :D

Thursday, April 20, 2017

Automated API testing using Codeception

I was looking for a tool for testing an API, and I remembered about my old friend Codeception, which I used for some functional testing a while ago. I need now to test an API which is receiving requests and gives responses using JSON format.


Install Codeception via Composer
$ composer require "codeception/codeception" 
Create an alias so you do not have to write the entire path every time you execute a command:
$ alias codecept='./vendor/bin/codecept'
Create an API test suite:
$ codecept generate:suite api
Edit the configuration file  tests/api.suite.yml:


class_name: ApiTester
modules:
    enabled:
        - \Helper\Api
        - REST:
            url: "http://yourwebste.com/"
            depends: PhpBrowser
            part: Json

I will be using "Cest" format:  "Cest combines scenario-driven test approach with OOP design. In case you want to group a few testing scenarios into one you should consider using Cest format."

$ codecept generate:cest api UsersTestCest
This command is creating a PHP file under tests/api/.  
Before starting to write my first tests I need to find a way to pass a username and password to all tests, as it is needed for connecting to the API.
I can add any parameters to the api.suite.yml and access them inside my test classes:
api.suite.yml


class_name: ApiTester
modules:
    enabled:
        - \Helper\Api
        - REST:
            url: "http://yourwebsite.com"
            depends: PhpBrowser
            part: Json
params:
    username: "username"
    password: "password"


 And inside tests/api/UsersTestCest.php

class UsersTestCest
{
    private $username;
    private $password;

    public function _before(ApiTester $I)
}
    {
        $config = \Codeception\Configuration::config();
        $apiSettings = \Codeception\Configuration::suiteSettings('api', $config);
        $this->username = $apiSettings['params']['username'];
        $this->password = $apiSettings['params']['password'];
    }
}


The function "_before()" will be executed as it names says, before actual test functions, and it will  initialize the variables with values from api.suite.yml file.

Let's write the first test function:


public function createUser(ApiTester $I)
    {
        $action = "create_user";
        $I->haveHttpHeader('Content-Type', 'application/json');

        $array=[
            "action"=> $action,
            "username"=> $this->username,
            "key"=> $this->password,
            "user_username"=> "new_username",
            "user_email"=> "email@company.com",
            "user_password"=> "password"
            ]
        ];
        $string = json_encode($array);
        $I->sendPOST('api.php',$string);
        $I->seeResponseCodeIs(\Codeception\Util\HttpCode::OK); // 200
        $I->seeResponseIsJson();
        $I->seeResponseMatchesJsonType([
            'operation' => 'string',
            'status' => 'string',
        ]);
    }

The link where the request is sent with the function sendPost() is the concatenation between the URL parameter from api.suite.yml and the first parameter in sentPost() function.
You can check if the response has HTTP status 200, if it is in JSON format and if the JSON has the expected structure.
Even more you can create your own assertions. These custom functions needs to be added to the file "tests/_support/ApiTester.php".
I will add a function to verify if the "status" field from the response is equal to '1'




<?php
namespace Helper;
class will be available in $I
use Codeception\TestInterface;

class Api extends \Codeception\Module
{
    public function checkOperationStatusSuccess()
    {
        $response = $this->getModule('REST')->response;
        $array = json_decode($response, true);
        $this->assertEquals('1',$array['status'],"Operation status should be '1' for success.");
    }
}

Run all tests with this command:
codecept run api -v

Or only tests from certain class:
$ codecept run tests/api/UsersTestCest.php -v
  
The tests are executed in the alphabetic order of the classes, but you can use the annotation "@depends" to indicated that your test function depends on other one(s). 
Enjoy!