Saturday, 3 November 2018

Add behat in Symfony 4

To add behat in Symfony 4 i have generally followed this link

However i ran into problems so i thought of sharing this info.

The steps are

run composer req behat/behat --dev

but stumbled on the following error:

~/work/thefootie-behat $ composer req behat/behat --dev

Using version ^3.5 for behat/behat

./composer.json has been updated

Loading composer repositories with package information

Updating dependencies (including require-dev)

Restricting packages listed in "symfony/symfony" to "4.1.*"

Your requirements could not be resolved to an installable set of packages.

  Problem 1

    - Installation request for behat/behat ^3.5 -> satisfiable by behat/behat[v3.5.0].

    - behat/behat v3.5.0 requires symfony/class-loader ~2.1||~3.0 -> satisfiable by symfony/class-loader[3.4.x-dev] but these conflict with your requirements or minimum-stability.

Installation failed, reverting ./composer.json to its original content.

It seems I had to choose to downgrade my minimum stability to dev to install this, as this is dev only packages I don't really mind.

~/work/thefootie-behat $ composer config minimum-stability dev

Following which I did not have any problems with the rest of the instructions.

Installation and adding config in behat.yml was fine, but when i tried to run behat i got the error below:

~/work/thefootie-behat $ ./vendor/behat/behat/bin/behat

PHP Warning:  require_once(/Users/devuser/work/thefootie-behat): failed to open stream: Undefined error: 0 in /Users/devuser/work/thefootie-behat/vendor/symfony/dependency-injection/ContainerBuilder.php on line 1097

Warning: require_once(/Users/devuser/work/thefootie-behat): failed to open stream: Undefined error: 0 in /Users/devuser/work/thefootie-behat/vendor/symfony/dependency-injection/ContainerBuilder.php on line 1097

PHP Fatal error:  require_once(): Failed opening required '/Users/devuser/work/thefootie-behat/' (include_path='.:/usr/local/Cellar/php/7.2.5/share/php/pear') in /Users/devuser/work/thefootie-behat/vendor/symfony/dependency-injection/ContainerBuilder.php on line 1097

Fatal error: require_once(): Failed opening required '/Users/devuser/work/thefootie-behat/' (include_path='.:/usr/local/Cellar/php/7.2.5/share/php/pear') in /Users/devuser/work/thefootie-behat/vendor/symfony/dependency-injection/ContainerBuilder.php on line 1097

After some googling i came across, a post and downgraded the behat/symfony2-extension to 2.1.5 - this seemed to have resolved the issue.

Turned out we used the behat/symfony2-extension with dev-master stability.

"Downgrading" to 2.1.5 resolved the issue.

After downgrading i can see the demo steps passed!

~/work/thefootie-behat $ ./vendor/behat/behat/bin/behat 
  In order to prove that the Behat Symfony extension is correctly installed
  As a user
  I want to have a demo scenario

  Scenario: It receives a response from Symfony's kernel # features/demo.feature:10
    When a demo scenario sends a request to "/"          # FeatureContext::aDemoScenarioSendsARequestTo()
    Then the response should be received                 # FeatureContext::theResponseShouldBeReceived()

1 scenario (1 passed)
2 steps (2 passed)
0m0.20s (20.80Mb)

Monday, 15 October 2018

Update Drupal 8 DB to allow Updates to be run again for testing and dev purposes

When working with Drupal 7 and indeed Drupal 8 we may sometimes need to run updates (hook_update) again. In Drupal 7 updating system table should have been sufficient. In D8 things are a little bit different.

Run a database update as follows:
  • SELECT collection, name, substring(value, 1, 2500) FROM key_value WHERE collection="system.schema" and name="modulenameinquestion"
    • The above will give us the last update hook that was run on this database.
  • UPDATE key_value SET value = 'i:9999' where collection="system.schema" and name="modulenameinquestion"

Hope this helps.

Monday, 18 June 2018

Use Behat BDD tests with Drupal 7

Behat is a framework that allows us to write BDD tests, aka given-when-then tests. The language of test specification is quite simple and easy to read and is more business friendly, to know more about BDD you can go here

To use BDD with our drupal 7 site we need to go in the code base, create a folder under sites folder called behat. Installing Behat is done best using composer, so we need to create a composer.json file with the following content

  "require": {
    "drupal/drupal-extension": "~3.0"
  "config": {
    "bin-dir": "bin/"
and save as composer.json 

Instructions are from adding starting steps or steps to make things clear.

Assume composer is installed system wide (you can find better guides to install composer by searching on google).

composer install

This will install all required dependencies (exactly why composer is such a good idea).

Your console will fill up with a lot of information like this:

Once everything is installed, we are ready to start writing tests - but we need to tell behat where to store our context and features first, so wait ... what are context and features? more about that in the excellent BDD guide i linked above. 

Lets just say context is where you have your definitions, transformations and hooks, and features where you define your acceptance criteria or your tests in the given-when-then format.

The first step we need to do is initialise behat, to do that we say

./bin/behat --init

This creates the features and context as discussed above and shows you the following message.

If we have installed behat correctly we should be able to see a lot of context already defined for use in our Drupal projects thanks to Drupal Behat extensions. To see the context and maybe use it for reference later do this

./bin/behat -dl

This should list all the context as below:

There are quite a few context defined, in fact they are enough to get us started and take us much further than that. For eg. we will not write a single custom context but try to reuse existing context and write our features to add content and validate.

Remember this if your BDD tests are not working as expected, this could be more because you have modules that have hooks that modify default drupal behaviour and you may have to write your own context by extending the DrupalContext. 

Add behat in Symfony 4

To add behat in Symfony 4 i have generally followed this link How...