Buy

Installing Flex

Our project is now on Symfony 4.0, and it still works! Well, it almost works: we would just need to remove a few references to SensioDistributionBundle and SensioGeneratorBundle.

The point is this: if you want, you can upgrade to Symfony 4, but not migrate to the new Flex project structure. That's fine.

But... since Flex is awesome... let's do it!

Flex: Composer Plugin & New BFF

Flex is a Composer plugin, and, it's pretty simple: when you install a package, it checks to see if there is a recipe for that package. A recipe can add configuration files, auto-enable the bundle, add paths to your .gitignore file and more. But, for Flex to work, you need to use the Flex directory structure.

Upgrade to Flex: The Plan

So here's the plan: we're going to bootstrap a new Flex application right inside our existing project. Then, little-by-little, we'll move our code and configuration into it. It's going to be pretty freakin' cool.

Upgrade Composer. For Real

Before we start, make sure that your Composer is at the latest version:

composer self-update

Seriously, do this. Composer recently released a bug fix that helps Flex.

Installing Flex

Ok, so... let's install Flex!

composer require symfony/flex

As soon as this is in our project, it will find and install recipes each time we add a new library to our project. In fact, check it out!

Configuring symfony/flex

Ha! Flex even installed a recipe for itself! What an over-achiever! Let's find out what it did:

git status

Of course, it modified composer.json and composer.lock. But there are two new files: .env.dist and symfony.lock. Open the first.

How did this get here? It was added by the symfony/flex recipe! More about this file later.

Next, look at symfony.lock. This file is managed by Flex: it keeps track of which recipes were installed. You should commit it, but not think about it.

Installing Missing Recipes

Because this is an existing project, our app already contains a bunch of vendor libraries... and a lot of these might have recipes that were never installed because Flex wasn't in our project yet! Lame! No problem! Empty the vendor/ directory and run composer install

rm -rf vendor
composer install

Normally, Flex only installs a recipe when you first composer require a library. But Flex knows that the recipes for these libraries were never installed. So it runs them now.

Yea! 11 recipes! Woh! And one of them is from the "contrib" repository. There are two repositories for recipes. The official one is heavily guarded for quality. The "contrib" one also has some checks, but the quality is not guaranteed. That's why you see this question. I'll type "p" to permanently allow recipes from contrib.

Run git status to see what changed:

git status

Woh! We have a new config/ directory and a lot more! Starting with nothing, Flex is scaffolding the new project around us! It's even auto-enabling all the bundles in a new bundles.php file.

Sweet!

The Flex composer.json

When you start a new Flex project, you actually clone this symfony/skeleton repository... which is literally one file: composer.json. This has a few really important things in it, including the fact that it requires symfony/framework-bundle but not symfony/symfony.

Let's work on that next!

Leave a comment!

  • 2018-04-19 Vlad

    Thank you, Ryan! Will Try this.

  • 2018-04-18 weaverryan

    Vlad yep! For now, you can "mimic" my PR and create a tests/bootstrap.php - https://github.com/symfony/.... It's a super simple change to your project, and will get you what you need. Then, soon, everyone will hopefully be using a similar approach anyways :)

  • 2018-04-18 Vlad

    Is there any way to somehow read them from .env in phpunit.xml.dist? Like using the env() function in the YAML files?

  • 2018-04-18 Diego Aguiar

    Hola! como estas? :)

    I'm afraid that you have to manually add them to both files: .env and phpunit.xml.dist
    As Ryan said, this is an annoying thing to do but Symfony people is working on to get it better

  • 2018-04-18 Vlad

    ¡Hola, Diego!
    Say, I want to store my test environmental variables in the .env file as well. How can I "feed" them into the phpunit.xml.dist file instead of declaring with there with the <env name="..." value=".../"> ?
    Or does it read them automatically from the .env file and the variables added to the phpunit.xml.dist file directly simply overwrite them?
    Gracias.

  • 2018-04-18 Diego Aguiar

    Hey Vlad

    I would stick with the file that comes from installing Symfony4 (the short one), and then add all the "env" variables that my project needs


    <php>
    <env name="SOME_API_KEY" value="secret_key"/>
    </php>

    Cheers!

  • 2018-04-18 Vlad

    Hi weaverryan!

    The duplication in phpunit.xml.dist can be replicated by simply following your Upgrade to Symfony4 and Flex!
    tutorial. It happens when Flex gets installed.

    Would you suggest removing the duplication or dumping this phpunit.xml.dist file altogether and simply going with a fresh one that is created when PHPUnit is first installed? In this case, how would I "feed" the test environmental variables? Would I put them into the test YAML files, or will they be read from .env?

    Thanks!

  • 2018-04-18 weaverryan

    Hey Vlad!

    GREAT question :). And, there are 2 things going on:

    1) Currently, when you run tests in a Symfony 4 / Flex project, it does NOT read your .env file. Instead, you're supposed to duplicate your environment variables in your phpunit.xml.dist file. That will likely change (I'm changing it, because it's annoying: https://github.com/symfony/..., but that's the current state of things. So, because of this, when Flex installs a recipe, and that recipe adds a new environment variable, it adds it to .env, .env.dist AND to phpunit.xml.dist. This is what you're seeing: the recipe causes these changes.

    2) BUT, there's also the duplication. This should not happen - it smells like some sort of Flex bug to me, tbh - because you had an existing phpunit.xml.dist file and were upgrading to Flex, this may have caused some situation that is not currently accounted for. Definitely remove that duplication. If you know the steps to replicate, you could definitely open an issue on Symfony Flex for that: https://github.com/symfony/.... I would be happy to support that issue, as I think it's valid. However, we *may*/probably will remove this phpunit.xml.dist functionality soon anyways (via my linked PR - just need to finish it!).

    Cheers!

  • 2018-04-17 Vlad

    Hello, Ryan!
    When I installed Flex, it converted my phpunit.xml.dist to this:





    <phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nonamespaceschemalocation="http://schema.phpunit.de/4.1/phpunit.xsd" backupglobals="false" colors="true" bootstrap="app/autoload.php">
    <php>
    <ini name="error_reporting" value="-1"/>





    <env name="DATABASE_URL" value="mysql://db_user:db_password@127.0.0.1:3306/db_name"/>






    <env name="MAILER_URL" value="null://localhost"/>



    <env name="APP_ENV" value="dev"/>
    <env name="APP_SECRET" value="5f0bab9ed6fbf020425f21cffc2704c9"/>






    <env name="JWT_PRIVATE_KEY_PATH" value="config/jwt/private.pem"/>
    <env name="JWT_PUBLIC_KEY_PATH" value="config/jwt/public.pem"/>
    <env name="JWT_PASSPHRASE" value="86c79d44541ebfb81f323ed06a3f0f65"/>

    </php>

    <testsuites>
    <testsuite name="Project Test Suite">
    <directory>tests</directory>
    </testsuite>
    </testsuites>

    <php>
    <server name="KERNEL_DIR" value="app/"/>





    <env name="DATABASE_URL" value="mysql://db_user:db_password@127.0.0.1:3306/db_name"/>






    <env name="MAILER_URL" value="null://localhost"/>



    <env name="APP_ENV" value="dev"/>
    <env name="APP_SECRET" value="5f0bab9ed6fbf020425f21cffc2704c9"/>






    <env name="JWT_PRIVATE_KEY_PATH" value="config/jwt/private.pem"/>
    <env name="JWT_PUBLIC_KEY_PATH" value="config/jwt/public.pem"/>
    <env name="JWT_PASSPHRASE" value="86c79d44541ebfb81f323ed06a3f0f65"/>

    </php>

    <filter>
    <whitelist>
    <directory>src</directory>
    <exclude>
    <directory>src/*Bundle/Resources</directory>
    <directory>src/*/*Bundle/Resources</directory>
    <directory>src/*/Bundle/*Bundle/Resources</directory>
    </exclude>
    </whitelist>
    </filter>
    </phpunit>


    However, when starting a new Symfony4 project from scratch and installing PHPUnit in it, I get the following phpunit.xml.dist





    <phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nonamespaceschemalocation="http://schema.phpunit.de/6.1/phpunit.xsd" backupglobals="false" colors="true" bootstrap="vendor/autoload.php">
    <php>
    <ini name="error_reporting" value="-1"/>
    <env name="KERNEL_CLASS" value="App\Kernel"/>
    <env name="APP_ENV" value="test"/>
    <env name="APP_DEBUG" value="1"/>
    <env name="APP_SECRET" value="s$cretf0rt3st"/>
    <env name="SHELL_VERBOSITY" value="-1"/>

    </php>

    <testsuites>
    <testsuite name="Project Test Suite">
    <directory>tests/</directory>
    </testsuite>
    </testsuites>

    <filter>
    <whitelist>
    <directory>./src/</directory>
    </whitelist>
    </filter>

    <listeners>
    <listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener"/>
    </listeners>
    </phpunit>


    My question is why is the top one different from the bottom and why does the top one contain 2 mostly identical sections above and below


    <testsuites>
    <testsuite name="Project Test Suite">
    <directory>tests</directory>
    </testsuite>
    </testsuites>

    Thank you!