Flag of Ukraine
SymfonyCasts stands united with the people of Ukraine

Testing the Bundle

Keep on Learning!

If you liked what you've learned so far, dive in!
Subscribe to get access to this tutorial plus
video, code and script downloads.

Start your All-Access Pass
Buy just this tutorial for $8.00

With a Subscription, click any sentence in the script to jump to that part of the video!

Login Subscribe

Hey! Someone already made some tests for our bundle!

... lines 1 - 2
namespace App\Tests\Service;
... lines 4 - 7
class KnpUIpsumTest extends TestCase
{
public function testGetWords()
{
$ipsum = new KnpUIpsum();
$words = $ipsum->getWords(1);
$this->assertInternalType('string', $words);
$this->assertCount(1, explode(' ', $words));
$words = $ipsum->getWords(10);
$this->assertCount(10, explode(' ', $words));
$words = $ipsum->getWords(10, true);
$this->assertCount(10, $words);
}
... lines 24 - 65
}

Tip

The assertInternalType() method has been removed, you can use assertIsString() instead:

$this->assertIsString($words);

If you want to know more about this: https://github.com/sebastianbergmann/phpunit/issues/3369

So nice! Right now, they live in the app, but moving them into the bundle is our next job! But first... let's make sure they're still working.

Find the terminal tab for the application and run:

./vendor/bin/simple-phpunit

The first time you run this, it'll download PHPUnit behind the scenes. Then... it does not pass!

Class App\Service\KnpUIpsum not found

Of course! When we moved this class into the new namespace, we did not update the test! No problem - just re-type KnpUIpsum and hit tab to auto-complete and get the new use statement.

... lines 1 - 4
use KnpU\LoremIpsumBundle\KnpUIpsum;
... lines 6 - 67

Perfect! But... I can already see another problem! When we added the first constructor argument to KnpUIpsum, we also didn't update the test. I could use mocking here, but it's just as easy to say new KnpUWordProvider. Repeat that in the two other places.

... lines 1 - 5
use KnpU\LoremIpsumBundle\KnpUWordProvider;
... lines 7 - 8
class KnpUIpsumTest extends TestCase
{
public function testGetWords()
{
$ipsum = new KnpUIpsum(new KnpUWordProvider());
... lines 14 - 23
}
... line 25
public function testGetSentences()
{
$ipsum = new KnpUIpsum(new KnpUWordProvider());
... lines 29 - 37
}
... line 39
public function testGetParagraphs()
{
... lines 42 - 43
for ($i = 0; $i < 100; $i++) {
$ipsum = new KnpUIpsum(new KnpUWordProvider());
... lines 46 - 64
}
}
}

Ok, try those tests again!

./vendor/bin/simple-phpunit

Got it!

Adding Tests to your Bundle & autoload-dev

Time to move this into our bundle. We already have a src/ directory. Now create a new directory next to that called tests/. Copy the KnpUIpsumTest and put that directly in this new folder. I'm putting it directly in tests/ because the KnpUIpsum class itself lives directly in src/.

And the test file is now gone from the app.

But really... we shouldn't need to update much... or anything in the test class itself. In fact, the only thing we need to change is the namespace. Instead of App\Tests\Services, start with the same namespace as the rest of the bundle. So, KnpU\LoremIpsumBundle\Tests.

... lines 1 - 2
namespace KnpU\LoremIpsumBundle\Tests;
... lines 4 - 67

But, if we're going to start putting classes in the tests/ directory, we need to make sure that Composer can autoload these files. This isn't strictly required to make PHPUnit work, but it will be needed if you add any helper or dummy classes to the directory and want to use them in your tests.

And, it's easy! We basically want to add a second PSR-4 rule that says that the KnpU\LoremIpsumBundle\Tests namespace lives in the tests/ directory. But... don't! Instead, copy the entire section, paste and rename it to autoload-dev. Change the namespace to end in Tests\\ and point this at the tests/ directory.

... lines 1 - 19
"autoload-dev": {
"psr-4": {
"KnpU\\LoremIpsumBundle\\Tests\\": "tests/"
}
}
... lines 25 - 26

Why autoload-dev? The issue is that our end users will not be using anything in the tests/ directory: this config exists just to help us when we are working directly on the bundle. By putting it in autoload-dev, the autoload rules for the tests/ directory will not be added to the autoload matrix of our users' applications, which will give them a slight performance boost.

Installing symfony/phpunit-bridge

Ok: our test is ready. So let's run it! Move over to the terminal for the bundle and run... uh... wait a second. Run, what? We haven't installed PHPUnit! Heck, we don't even have a vendor/ directory yet. Sure, you can run composer install to get a vendor/ directory... but with nothing inside.

This should be no surprise: if we want to test our bundle, the bundle itself needs to require PHPUnit. Go back to the terminal and run:

composer require symfony/phpunit-bridge --dev

Two important things. First, we're using Symfony's PHPUnit bridge because it has a few extra features... and ultimately uses PHPUnit behind-the-scenes. Second, just like with autoloading, our end users do not need to have symfony/phpunit-bridge installed in their vendor directory. We only need this when we're working on the bundle itself. By adding it to require-dev, when a user installs our bundle, it will not also install symfony/phpunit-bridge.

Ignoring composer.lock

Now that we've run composer install, we have a composer.lock file! So, commit it! Wait, don't! Libraries and bundles should actually not commit this file - there's just no purpose to lock the dependencies: it doesn't affect our end-users at all. Instead, open the .gitignore file and ignore composer.lock. Now when we run git status, yep! It's gone.

... line 1
composer.lock

phpunit.xml.dist

Ok, let's finally run the tests!

./vendor/bin/simple-phpunit

It - of course - downloads PHPUnit behind the scenes the first time and then... nothing! It... just prints out the options??? What the heck? Well... our bundle doesn't have a phpunit.xml.dist file yet... so it has no idea where our test files live or anything else!

A good phpunit.xml.dist file is pretty simple... and I usually steal one from a bundle I trust. For example, Go to github.com/knpuniversity/oauth2-client-bundle. Find the phpunit.xml.dist file, view the raw version and copy it. Back at our bundle, create that file and paste it in.

<?xml version="1.0" encoding="UTF-8"?>
<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="./vendor/autoload.php"
>
... lines 9 - 26
</phpunit>

Oh, and before I forget, in .gitignore, also ignore phpunit.xml. The .dist version is committed, but this allows anyone to have a custom version on their local copy that they do not commit.

... lines 1 - 2
phpunit.xml

Check out the new file: the really important thing is that we set the bootstrap key to vendor/autoload.php so that we get Composer's autoloading. This also sets a few php.ini settings and... yes: we tell PHPUnit where our test files live.

Now I think it will work. Find your terminal and try it again:

./vendor/bin/simple-phpunit

It passes! Woo!

After seeing these fancy green colors, you might be thinking that our bundle is working! And if you did... you'd be half right. Next, we'll build a functional test... which is totally going to fail.

Leave a comment!

5
Login or Register to join the conversation
Helmi Avatar
Helmi Avatar Helmi | posted 4 years ago | edited

When i run ./vendor/bin/simple-phpunit it raises an exception

Fatal error: Uncaught RuntimeException: Could not find https://github.com/sebastianbergmann/phpunit/archive/6.5.zip in C:\www\sf_bundles_scrach\vendor\symfony\p<br />hpunit-bridge\bin\simple-phpunit:65<br />Stack trace:<br />#0 {main}<br /> thrown in C:\www\sf_bundles_scrach\vendor\symfony\phpunit-bridge\bin\simple-phpunit on line 65<br />

It seem that the corresponding branches have been deleted from github.

You can then install manually <b>symfony/phpunit-bridge</b> to resolve.

Reply

Hey Helmi,

Sorry for the very long reply! Yeah, it looks like PHPUnit 6.5 support was dropped, you need to upgrade "symfony/phpunit-bridge" package, i.e. call "composer update symfony/phpunit-bridge" to be able to call "bin/phpunit" or "./vendor/bin/simple-phpunit". Thank you for reporting this! I upgraded the tutorial's code so it should work out of the box now.

Cheers!

Reply
Dmitry V. Avatar
Dmitry V. Avatar Dmitry V. | posted 4 years ago

Hello, could you tell me, from what course this PHPUnit test exists here?

Reply

Hey Dmitriy,

This test was added in *this* course, but we did it behind the scene because we don't want to cover testing in this course. If you're interested in testing with PHPUnit, check out our PHPUnit course: https://symfonycasts.com/sc...

Cheers!

Reply
Dmitry V. Avatar

it's clear

Reply
Cat in space

"Houston: no signs of life"
Start the conversation!

This tutorial is built using Symfony 4, but most of the concepts apply fine to Symfony 5!

What PHP libraries does this tutorial use?

// composer.json
{
    "require": {
        "php": "^7.1.3",
        "ext-iconv": "*",
        "doctrine/annotations": "^1.8", // v1.8.0
        "knplabs/knp-markdown-bundle": "^1.7", // 1.7.0
        "knpuniversity/lorem-ipsum-bundle": "*@dev", // dev-master
        "nexylan/slack-bundle": "^2.0,<2.2", // v2.0.1
        "php-http/guzzle6-adapter": "^1.1", // v1.1.1
        "sensio/framework-extra-bundle": "^5.1", // v5.1.6
        "symfony/asset": "^4.0", // v4.0.6
        "symfony/console": "^4.0", // v4.0.6
        "symfony/flex": "^1.0", // v1.18.7
        "symfony/framework-bundle": "^4.0", // v4.0.6
        "symfony/lts": "^4@dev", // dev-master
        "symfony/twig-bundle": "^4.0", // v4.0.6
        "symfony/web-server-bundle": "^4.0", // v4.0.6
        "symfony/yaml": "^4.0", // v4.0.6
        "weaverryan_test/lorem-ipsum-bundle": "^1.0" // v1.0.0
    },
    "require-dev": {
        "easycorp/easy-log-handler": "^1.0.2", // v1.0.4
        "sensiolabs/security-checker": "^4.1", // v4.1.8
        "symfony/debug-bundle": "^3.3|^4.0", // v4.0.6
        "symfony/dotenv": "^4.0", // v4.0.6
        "symfony/maker-bundle": "^1.0", // v1.1.1
        "symfony/monolog-bundle": "^3.0", // v3.2.0
        "symfony/phpunit-bridge": "^3.3|^4.0", // v4.3.3
        "symfony/stopwatch": "^3.3|^4.0", // v4.0.6
        "symfony/var-dumper": "^3.3|^4.0", // v4.0.6
        "symfony/web-profiler-bundle": "^3.3|^4.0" // v4.0.6
    }
}
userVoice