Minimal BDD CMS with PHP and PHPSpec Part II: Keep the Arrange-Act-Assert

After a short wasups, I decided to refactor the code for the first pst. It must be clear the Arrange-Act-Assert Pattern. We need our tests to be easy to read, follow, understand and maintain. Also our tests nees to show:
  • What is being set up and initialized in the arrange section
  • What method is being executed in the act section
  • What determines the outcome of the test in the assert section

Thanks to @davicgc again for his comments on Part I.

The Arrange-Act-Assert and the When Context

In the Part I code, I had the Arrange in the before function. The before function is called when before PHPSpec executes any test, and it provides a global function that can set global variables across the PHPSpec script.

Besides this, the class nomenclature can be more descriptive for a Context/Specificacion and When/ItShould BDD test suite

¿ it is the Object creation part of the ACT ?

The Act can be considered mixed with the Arrange part, I commented that on the code of part II. Because the object to be tested  (the CMS) is common across all the DescribeWhen*Whatever*BDDCMS* functions in this spec, I put it the Act in the before function too. This is changed in this Part II code, perhaps it’s more apparatous but more “sexy”.

Resuming, if I consider the Act includes the creationf the object to be tested then I can move from the before to the ItShould… function tests, the object is created on the ItShould… functions) so .. the ACT passes from global to local.

The Assert, of course, is handled for the -> that comes after each spec.

In this case, the context to execute the SUT is stablished in the before function, but depends on the complexity of the tests, we can move later this to another specific context-setup fuction scheme.

Regarding to a better Context/Specification understanding, the test class is now called DescribeWhenCreatingNewCoreCMS. So, when I pass the tests, the ouput is:
should be an behaviour driven design content management system

Finished in 0.163836 seconds
1 example

Lets begin refactoring our test class like this: (It has to begin with “Describe”)

class DescribeWhenCreatingNewCoreCMS extends \PHPSpec\Context
private $CoreCMS;

// ARRANGE parte of the Arrange-Act-Assert Pattern
// Info at
// Here we define the CONTEXT for the specs
// the Before function is first called when text executes

public function Before()
// ARRANGE all necessary preconditions and inputs.

// In this case, as a CMS, the input will be an URL
// Here we define the PHP Variables that "simulates"
// an URL request through any normal browser

// Our CMS only will need this two "enironment" global variables to work as if
// were installed on any normal webserver wich php AKA Apache, ngnix ;)

$_SERVER['REQUEST_URI'] = 'http:\\\index.php';
$_SERVER['SCRIPT_NAME'] = 'index.php';

// Our index.php script will have only the instation of the CoreCMS class so
// we only instantiate it
// A typical index.php will look like this, the minimal code to work ;)
//  index.php
//    $cms= new BDDCMS\CoreCMS(); // Establish the environment
//    $cms->route(); // Dispatch the request
// All URL requests that are not 200 OK are rerouted to indexphp by an
// apache .htaccess that looks like:
// (I use xampp home directory in this example, in the rewriterule)
// Options +FollowSymLinks
// IndexIgnore */*
//    # Turn on the RewriteEngine
//    RewriteEngine On
//    #  Rules
//    RewriteCond %{REQUEST_FILENAME} !-f
//    RewriteCond %{REQUEST_FILENAME} !-d
//     RewriteRule ^(.*)\?*$ /xampp/bddcms/index.php?_route_=$1 [L,QSA]

// Here, we can define the ACT, common for all itShould... functions
// In this context, WhenCreatingANewBDDCMD, we only will test the correct
// component creation. If we need more complex Specs, we will need to create
// new DescribeWhatever... classes

$this->CoreCMS = new BDDCMS\Core\CoreCMS();

public function itShouldBeAnBehaviourDrivenDesignContentManagementSystem() {

// ACT on the object or method under test.
// We execute the SUT (System Under Test) here

$result= $this->spec($this->CoreCMS);

// ASSERT that the expected results have occurred



And that’s all for the moment … keep coding ;)

3 thoughts on “Minimal BDD CMS with PHP and PHPSpec Part II: Keep the Arrange-Act-Assert

  1. Pingback: Manu º¹ºº¹¹º¹º¹¹¨¨

  2. Pingback: Manu º¹ºº¹¹º¹º¹¹¨¨

  3. Pingback: Manu º¹ºº¹¹º¹º¹¹¨¨

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>