📜 ⬆️ ⬇️

Testing individual symfony 2 bundles

I'll start with a short background. I had the task to write a reservation for hotel rooms, I climbed on all of us favorite packagist , in search of a ready-made solution and, to my deep disappointment, I did not find anything. Well, we need to do it - we'll do it. The code is written, covered with functional tests in the application. After a couple of weeks, I decided to post a written bundle on github. But I faced the question: when testing a separate bundle, we don’t have the application itself. He began to google, and again did not find anything worthwhile. In general, I had to collect information bit by bit, and now I want to share my experience with you.

Dependencies


The first thing we want to do is create a new repository for our bundle and add files related to it to it. Our bundle, of course, has external dependencies. To resolve them, we will use composer . I have it installed globally make an amendment to this. Let's start:

$ composer init #


We initialized our project. The composer.json file was created in the root directory. It has several sections of interest to us: require , require-dev , suggest . Let's go through each of them:

Install the necessary components with the command
$ composer install

Well, with the dependencies sorted out.
')

Core


For testing services and functional testing, we will need an almost full-featured application.
Autoload classes

We will need to create our own bootstrap.php in the directory with our tests and indicate that phpunit should use it.
 <?php use Doctrine\Common\Annotations\AnnotationRegistry; //    Doctrine   use Composer\Autoload\ClassLoader; /** * @var ClassLoader $loader */ $loader = require __DIR__.'/../vendor/autoload.php'; AnnotationRegistry::registerLoader(array($loader, 'loadClass')); //    Doctrine   return $loader; 


Once again I want to draw your attention to the line AnnotationRegistry::registerLoader(array($loader, 'loadClass')); . My bundle uses Doctrine and annotations in full and for me it was a great surprise when I got an exception with the text "Annotation can not be loaded" over and over again.

Open our phpunit.xml.dist and indicate where our bootstrap.php
 <phpunit bootstrap="./Tests/bootstrap.php"> 

AppKernel and Console

The next step is to initialize the application. I created the Tests/fixtures folder where the files related to our test application will be located. The key class of any symfony application is AppKernel , create it in the folder Tests/fixtures/app
 <?php use Symfony\Component\HttpKernel\Kernel; use Symfony\Component\Config\Loader\LoaderInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\HttpKernelInterface; class AppKernel extends Kernel { /** * @return array */ public function registerBundles() { $bundles = array( //     ); return $bundles; } /** * @param \Symfony\Component\Config\Loader\LoaderInterface $loader */ public function registerContainerConfiguration(LoaderInterface $loader) { $loader->load(__DIR__ . '/config/config.yml'); } } 

Next we will need to configure our container, for this we will create the file Tests/fixtures/app/config/config.yml .

If you need a console, simply create a Tests/fixtures/app/console file with the following content:
 #!/usr/bin/env php <?php // if you don't want to setup permissions the proper way, just uncomment the following PHP line // read http://symfony.com/doc/current/book/installation.html#configuration-and-setup for more information //umask(0000); set_time_limit(0); require_once __DIR__.'/../../bootstrap.php'; require_once __DIR__.'/AppKernel.php'; use Symfony\Bundle\FrameworkBundle\Console\Application; use Symfony\Component\Console\Input\ArgvInput; use Symfony\Component\Debug\Debug; $input = new ArgvInput(); $env = $input->getParameterOption(array('--env', '-e'), getenv('SYMFONY_ENV') ?: 'dev'); $debug = getenv('SYMFONY_DEBUG') !== '0' && !$input->hasParameterOption(array('--no-debug', '')) && $env !== 'prod'; if ($debug) { Debug::enable(); } $kernel = new AppKernel($env, $debug); $application = new Application($kernel); $application->run($input); 

Now we will indicate where the core of our application is, add the following directive to phpunit.xml.dist :
 <php> <server name="KERNEL_DIR" value="Tests/Fixtures/app/" /> </php> 


After these simple manipulations, we got our test application and opened the way for using Symfony\Bundle\FrameworkBundle\Test\WebTestCase , with which we can test our services and stuff in the context of a Symfony 2 application.

View the full code here.

PS: In the next article I will try to break through the subtleties of testing Doctrine in our bundles.

Source: https://habr.com/ru/post/221259/


All Articles