Installing Symfony 3, Behat, and more


Before we get started, a word of caution: Symfony 3 is new. Many of the packages you will have absolutely no problems with in Symfony 2 may very well not work immediately in Symfony 3. The process of updating these packages is a work in progress (at the time of writing), so please - use Symfony 2.7 if you need long term support / don't need to be cutting edge.

Installing Symfony 3 is no different to installing Symfony 2. In this video, and in the real world, I strongly advise you to make use of the Symfony installer.

As mentioned in this video, the way I work tends to be without Vagrant / shared folders. This has the unfortunate side affect of not keeping your local code and the code on your server directly in sync. If you use an IDE like PHPStorm then one of these directions will be resolved for you - that is, local code changes can be automatically uploaded to your development server.

However, changes on the server side will not be pulled down by default. This is great in some respects - it means you won't end up with all the cached folder contents polluting your hard drive. But it will mean you need to manually pull down your composer.lock file, or log files, during development.

You may wish to work differently, that is your choice. I have used this way for a long time and have come to accept the limitations as acceptable when compared to the benefits I find this method provides.

Generally I find the easiest way is to install the Symfony installer on to the server, then run symfony new {project-name-here}, run a composer update, then use rsync to pull down the contents from the server to my local development machine.

If working in this way, be sure to update your local hosts file (/etc/hosts on mac / linux) to include an entry for your server, so you can access your site via hostname.

The rsync command would be :

cd /your/local/dev/directory
rsync -avzh your_user@your-server.dev:/var/www/your-project.dev .
mv your-project.dev/* .
rm -rf your-project.dev

Looks a bit weird. There's probably a better way of doing this (feel free to leave a comment).

To walk through it:

cd /your/local/dev/directory Change directory to a folder on your local machine where you want to store your code.

rsync -avzh your_user@your-server.dev:/var/www/your-project.dev . Copy the new symfony site data from the server to your current folder - BUT this will create a folder inside your current folder called your-project.dev. So...

mv your-project.dev/* . Move everything out of this nested subfolder, into your current directory, leaving this second directory empty, and then...

rm -rf your-project.dev Remove that left over folder.

Lastly, you will need to update your composer.json file with all the necessary dependencies.

Now, at this point we hit on a major issue with Symfony 3 - at the time of writing at least. There are a number of incompatible libraries / packages for Behat and Symfony 3.

So, here's the composer.json file for Symfony 2:

{
    "name": "your-name/your-project.dev",
    "license": "proprietary",
    "type": "project",
    "autoload": {
        "psr-4": {
            "": "src/"
        }
    },
    "require": {
        "php": ">=5.3.9",
        "symfony/symfony": "2.8.*",
        "doctrine/orm": "^2.4.8",
        "doctrine/doctrine-bundle": "~1.4",
        "symfony/assetic-bundle": "~2.3",
        "symfony/swiftmailer-bundle": "~2.3",
        "symfony/monolog-bundle": "~2.4",
        "sensio/distribution-bundle": "~4.0",
        "sensio/framework-extra-bundle": "^3.0.2",
        "incenteev/composer-parameter-handler": "~2.0",

        "nelmio/cors-bundle": "~1.4",
        "nelmio/api-doc-bundle": "~2.9@dev",
        "friendsofsymfony/user-bundle": "2.*@dev",
        "lexik/jwt-authentication-bundle": "^1.3",
        "csa/guzzle-bundle": "^1.3",
        "friendsofsymfony/rest-bundle": "^1.7",
        "jms/serializer-bundle": "^1.0",
        "oneup/flysystem-bundle": "^1.2"
    },
    "require-dev": {
        "sensio/generator-bundle": "~2.3",
        "behat/behat": "~3.0",
        "behat/symfony2-extension": "~2.0",
        "behat/mink": "~1.5",
        "behat/mink-extension": "~2.0",
        "behat/mink-browserkit-driver": "~1.1",
        "behat/web-api-extension": "~1.0@dev",
        "behatch/contexts": "dev-master",
        "phpspec/phpspec": "~2.0"
    },
    "scripts": {
        "post-install-cmd": [
            "Incenteev\\ParameterHandler\\ScriptHandler::buildParameters",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::prepareDeploymentTarget"
        ],
        "post-update-cmd": [
            "Incenteev\\ParameterHandler\\ScriptHandler::buildParameters",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::prepareDeploymentTarget"
        ]
    },
    "config": {
        "bin-dir": "bin"
    },
    "extra": {
        "symfony-app-dir": "app",
        "symfony-web-dir": "web",
        "symfony-assets-install": "relative",
        "incenteev-parameters": {
            "file": "app/config/parameters.yml"
        }
    }
}

And a similar one for Symfony 3:

{
    "name": "your-name/your-project.dev",
    "license": "proprietary",
    "type": "project",
    "autoload": {
        "psr-4": {
            "": "src/"
        },
        "classmap": [
            "app/AppKernel.php",
            "app/AppCache.php"
        ]
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
    "require": {
        "php": ">=5.5.9",
        "symfony/symfony": "3.0.*",
        "doctrine/orm": "^2.5",
        "doctrine/doctrine-bundle": "^1.6",
        "doctrine/doctrine-cache-bundle": "^1.2",
        "symfony/swiftmailer-bundle": "^2.3",
        "symfony/monolog-bundle": "^2.8",
        "sensio/distribution-bundle": "^5.0",
        "sensio/framework-extra-bundle": "^3.0.2",
        "incenteev/composer-parameter-handler": "^2.0",

        "nelmio/cors-bundle": "^1.4",
        "nelmio/api-doc-bundle": "^2.11",
        "friendsofsymfony/rest-bundle": "^1.7",
        "csa/guzzle-bundle": "^2.0",
        "jms/serializer-bundle": "^1.1",
        "oneup/flysystem-bundle": "^1.2",
        "friendsofsymfony/user-bundle": "dev-master",
        "lexik/jwt-authentication-bundle": "dev-master"
    },
    "require-dev": {
        "sensio/generator-bundle": "^3.0",
        "symfony/phpunit-bridge": "^2.7",

        "phpspec/phpspec": "^2.4",
        "behat/behat": "dev-master"
    },
    "minimum-stability": "dev",
    "scripts": {
        "post-install-cmd": [
            "Incenteev\\ParameterHandler\\ScriptHandler::buildParameters",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::prepareDeploymentTarget"
        ],
        "post-update-cmd": [
            "Incenteev\\ParameterHandler\\ScriptHandler::buildParameters",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::prepareDeploymentTarget"
        ]
    },
    "extra": {
        "symfony-app-dir": "app",
        "symfony-bin-dir": "bin",
        "symfony-var-dir": "var",
        "symfony-web-dir": "web",
        "symfony-tests-dir": "tests",
        "symfony-assets-install": "relative",
        "incenteev-parameters": {
            "file": "app/config/parameters.yml"
        }
    }
}

It's largely the same, except for the require-dev section and the inclusion of the "minimum-stability": "dev", option inside the Symfony 3 composer.json file.

I needed to lower the minimum stability to get access to the Symfony 3 compatible branches.

And lacking the Behat libraries is a big issue, which will hopefully be resolved asap. It's not going to stop us writing our features though, which is exactly what we are going to do next.

Code For This Course

Get the code for this course.

Episodes