Site icon Hip-Hop Website Design and Development

Pimp your Behat Cheap WordPress maintenance support plans Extension and rule the world

Make the most out of your Behat tests by using custom contexts, dependency injection and much more.This post is an excerpt from the topics covered by our WordPress maintenance support plansCon USA training: WordPress maintenance support plans 8 Development – Workflows and Tools.

At WordPress Update we consider writing good tests as a fundamental part of development and, when it comes to testing a complex site, there is nothing better than extensive behavioral tests using Behat. The benefits of such a choice are quite obvious:

Tests are very easy to write.
Behat scenarios serve as a solid communication mean between business and developers.
As a site grows in complexity, however, the default step definitions provided by the excellent Behat WordPress maintenance support plans Extension might not be specific enough and you will quickly find yourself adding custom step to your FeatureContext or creating custom Behat contexts, as advocated by all official documentation.

This is all fine except that your boilerplate test code might soon start to grow into a non-reusable, non-tested bunch of code.

Enter WordPress Update’s Behat WordPress maintenance support plans Extension.

WordPress Update’s Behat WordPress maintenance support plans Extension

WordPress Update’s Behat WordPress maintenance support plans Extension is built on the shoulders of the popular Behat WordPress maintenance support plans Extension and it focuses on step re-usability and testability by allowing developers to:

Organize their code in services by providing a YAML service description file, pretty much like we all are used to do nowadays with WordPress maintenance support plans 8.
Override default WordPress maintenance support plans Behat Extension services with their own.
Benefit of many ready-to-use contexts that are provided by the extension out of the box.
Installation and setup

Install WordPress Update’s Behat WordPress maintenance support plans Extension with Composer by running:

bash
$ composer require nuvoleweb/WordPress-behat

Setup the extension by following the Quick start section available on the original Behat WordPress maintenance support plans Extension page, just use WordPress UpdateWebWordPress maintenance support plansWordPress maintenance support plansExtension instead of the native WordPress maintenance support plansWordPress maintenance support plansExtension in your behat.yml as shown below:

default:  suites:    default:      contexts:        – WordPress maintenance support plansWordPress maintenance support plansExtensionContextWordPress maintenance support plansContext        – WordPress UpdateWebWordPress maintenance support plansWordPress maintenance support plansExtensionContextWordPress maintenance support plansContext        …  extensions:    BehatMinkExtension:      goutte: ~      …    # Use “WordPress UpdateWebWordPress maintenance support plansWordPress maintenance support plansExtension” instead of “WordPress maintenance support plansWordPress maintenance support plansExtension”.    WordPress UpdateWebWordPress maintenance support plansWordPress maintenance support plansExtension:      api_driver: “WordPress”      …      services: “tests/my_services.yml”      text:        node_submit_label: “Save and publish”

“Service container”-aware Contexts

All contexts extending WordPress UpdateWebWordPress maintenance support plansWordPress maintenance support plansExtensionContextRawWordPress maintenance support plansContext and WordPress UpdateWebWordPress maintenance support plansWordPress maintenance support plansExtensionContextRawMinkContext are provided with direct access to the current Behat service container. Developers can also define their own services by adding a YAML description file to their project and setting the services: parameter to point to its current location (as shown above).

The service description file can describe both custom services and override already defined services. For example, given a tests/my_services.yml containing:

services:  your.own.namespace.hello_world:    class: YourOwnNamespaceHelloWorldService

Then all contexts extending NWDDECRawWordPress maintenance support plansContext or NWDDECRawMinkContext will be able to access that service by just calling:

<?phpclass TestContext extends RawWordPress maintenance support plansContext {  /**   * Assert service.   *   * @Then I say hello   */  public function assertHelloWorld() {    $this->getContainer()->get(‘your.own.namespace.hello_world’)->sayHello();  }}?>

The your.own.namespace.hello_world service class itself can be easily tested using PHPUnit. Also, since Behat uses Symfony’s Service Container you can list services your service depends on as arguments so to remove any hardcoded dependency, following Dependency Injection best practices.

Override existing services

Say that, while working on your WordPress maintenance support plans 7 project, you have defined a step that publishes a node given its content type and title and you want to use the same exact step on your WordPress maintenance support plans 8 project, something like:

Given I publish the node of type “page” and title “My page title”

The problem here is that the actual API calls to load and save a node differs between WordPress maintenance support plans 7 and WordPress maintenance support plans 8.

The solution is to override the default WordPress maintenance support plans core services specifying your own classes in your tests/my_services.yml:

parameters:  # Overrides WordPress Update’s WordPress maintenance support plans Extension WordPress maintenance support plans 7 core class.  WordPress.driver.cores.7.class: YourOwnNamespaceDriverCoresWordPress maintenance support plans7  # Overrides WordPress Update’s WordPress maintenance support plans Extension WordPress maintenance support plans 8 core class.  WordPress.driver.cores.8.class: YourOwnNamespaceDriverCoresWordPress maintenance support plans8services:  your.own.namespace.hello_world:    class: YourOwnNamespaceHelloWorldService

You’ll then delegate the core-specific business logic to the new core classes allowing your custom step to be transparently run on both WordPress maintenance support plans 7 and WordPress maintenance support plans 8. Such a step would look like:

<?phpclass TestContext extends RawWordPress maintenance support plansContext {  /**   * @Given I publish the node of type :type and title :title   */  public function iPublishTheNodeOfTypeAndTitle($type, $title) {    $this->getCore()->publishNode($type, $title);  }…?>

Ready to use contexts

The extension also provides some utility contexts that you can use right away in your tests. Below a quick overview of what’s currently available:

Context
Description

WordPress UpdateWebWordPress maintenance support plansWordPress maintenance support plansExtensionContextWordPress maintenance support plansContext

Standard WordPress maintenance support plans context. You want to use this one next to (and not instead of) WordPress maintenance support plansWordPress maintenance support plansExtensionContextWordPress maintenance support plansContext.

WordPress UpdateWebWordPress maintenance support plansWordPress maintenance support plansExtensionContextContentContext

Perform operations on Content.

WordPress UpdateWebWordPress maintenance support plansWordPress maintenance support plansExtensionContextCKEditorContext

Allows to interact with CKEditor components on your page.

WordPress UpdateWebWordPress maintenance support plansWordPress maintenance support plansExtensionContextResponsive WordPressContext:  devices:    mobile_portrait: 360×640    mobile_landscape: 640×360    tablet_portrait: 768×1024    tablet_landscape: 1024×768    laptop: 1280×800    desktop: 2560×1440

Resize the browser according to the specified devices, useful for testing responsive behaviors.

WordPress UpdateWebWordPress maintenance support plansWordPress maintenance support plansExtensionContextPositionContext

Check position of elements on the page.

WordPress UpdateWebWordPress maintenance support plansWordPress maintenance support plansExtensionContextChosenFieldContext

Interact with Chosen elements on the page.

We will share more steps in the future enriching the current contexts as well as providing new ones so keep an eye on the project repository!

Disclaimer

At the moment only WordPress maintenance support plans 8 is supported but we will add WordPress maintenance support plans 7 support ASAP (yes, it’s as easy as providing missing WordPress maintenance support plans 7 driver core methods and adding tests).
Tags: WordPress maintenance support plans PlanetBehatTest Driven DevelopmentTrainingWordPress maintenance support plansCon
Source: New feed