How to implement Functional Test in Symfony 4 with Kahlan 4

In this tutorial, we are going to discuss about how to implement the functional testing in Symfony 4 with Kahlan 4. In IT world, whenever we are creating an application/website, we need to test the application/website thouroghly. To perform the functional testing we have a bundle in Symfony 4. 

The simplest way is define Symfony 4 skeleton bootstrap in kahlan config, and use its property at specs, for example, we configure config at kahlan-config.php as follows:

suite()->root();
    $root->beforeAll(function () {
        $this->request = Request::createFromGlobals();
        $this->kernel  = new Kernel('test', false);
    });
    return $next();
});

Above settings are minimal, if you need more setup, you can define there. If you didn’t require kahlan/kahlan:^4.0, you can require via composer:

$ composer require --dev kahlan/kahlan:^4.0

Give a try

Let’s try testing a famous /lucky/number from LuckyController. We have the following controller:

render('lucky/number.html.twig', [
            'number' => $number,
        ]);
    }
}

And our twig file is:

{# templates/lucky/number.html.twig #}
<h1>Your lucky number is {{ number }}</h1>

We can place test under spec directory at root directory, for its test, we can create a spec/Controller directory:

kahlan.config.php
    spec
        Controller

Now, we can create the test as follows with make request to the ‘/lucky/number’ page and get its response. We can use toMatchEcho matcher provided with regex to get match random number of mt_rand(0, 100) that printed inside a response html content:

request->create('/lucky/number', 'GET');
            $response = $this->kernel->handle($request);
 
            expect(function () use ($response) {
                $response->send();
            })->toMatchEcho(
                "#Your lucky number is ([0-9]|[1-8][0-9]|9[0-9]|100)#"
            );
 
        });
 
    });
 
});

Time to run it with command:

$ vendor/bin/kahlan 

By running the above commmand, you will get an Output.

Display personalised content in Drupal - Smart Content Paragraphs

In this tutorial we are going to discuss about, how to display the personalised content (Paragraphs) for each person (both anonymous and authenticated users) in Drupal.

Smart content module allows personalised blocks only. If we need to display the personalised Paragraphs, then we need to go for Smart Content Paragraphs module which will supports Paragraphs as well. 

Dependent Modules 

  • Paragraphs (https://www.drupal.org/project/paragraphs)
  • Smart Content (https://www.drupal.org/project/smart_content)
  • Smart Content Segments (https://www.drupal.org/project/smart_content_segments) 
Smart Content Paragraphs uses conditions, within Segments, to determine whether or not to display the reaction(s) defined within a Decision Block. A Condition is a single case that can be tested and determined to be either true or false, multiple conditions can be included in a Segment and a Segment Set is one or more Segments grouped together.

Click Here to Donwload the Module

Click Here to view the releases