Debugging Tests

Keep on Learning!

If you liked what you've learned so far, dive in!
Subscribe to get access to this tutorial plus
video, code and script downloads.

Start your All-Access Pass
Buy just this tutorial for $12.00

Debugging Tests

But what if this had failed? Could we debug it?

Let’s pretend we coded something wrong by throwing a big ugly exception in our controller:

// src/KnpU/CodeBattle/Controller/Api/ProgrammerController.php
// ...

public function updateAction($nickname, Request $request)
{
    $programmer = $this->getProgrammerRepository()->findOneByNickname($nickname);

    if (!$programmer) {
        $this->throw404();
    }

    throw new \Exception('This is scary!');

    // ...
}

Now run the test again:

$ php vendor/bin/behat

Not surprisingly, we’re getting a 500 error instead of 200. But we can’t really see what’s going on because we can’t see the big error page!

But don’t worry! First, I’ve done my best to configure Behat so when something fails, part of the last response is printed below.

Tip

This functionality works by returning the h1 and h2 elements of the HTML page. If your app shows errors with different markup, tweak the ApiFeatureContext::printLastResponseOnError method to your liking.

If this doesn’t tell you enough, we can print out the last response in its entirety. To do this, add “And print last response” to our scenario, just before the failing line:

// features/api/programmer.feature
// ...

Scenario: PUT to update a programmer
  Given the following programmers exist:
    | nickname    | avatarNumber | tagLine |
    | CowboyCoder | 5            | foo     |
  And I have the payload:
    """
    {
      "nickname": "CowboyCoder",
      "avatarNumber" : 2,
      "tagLine": "foo"
    }
    """
  When I request "PUT /api/programmers/CowboyCoder"
  And print last response
  Then the response status code should be 200
  And the "avatarNumber" property should equal "2"

Now just re-run the test:

$ php vendor/bin/behat

It may be ugly, but the entire response of the last request our test made is printed out, including all the header information on top. Once you’ve figured out and fixed the problem, just take the print last response line out and keep going!

Leave a comment!

This tutorial uses a deprecated micro-framework called Silex. The fundamentals of REST are still 💯valid, but the code we use can't be used in a real application.

What PHP libraries does this tutorial use?

// composer.json
{
    "require": {
        "silex/silex": "~1.0", // v1.3.2
        "symfony/twig-bridge": "~2.1", // v2.7.3
        "symfony/security": "~2.4", // v2.7.3
        "doctrine/dbal": "^2.5.4", // v2.5.4
        "monolog/monolog": "~1.7.0", // 1.7.0
        "symfony/validator": "~2.4", // v2.7.3
        "symfony/expression-language": "~2.4" // v2.7.3
    },
    "require-dev": {
        "behat/mink": "~1.5", // v1.5.0
        "behat/mink-goutte-driver": "~1.0.9", // v1.0.9
        "behat/mink-selenium2-driver": "~1.1.1", // v1.1.1
        "behat/behat": "~2.5", // v2.5.5
        "behat/mink-extension": "~1.2.0", // v1.2.0
        "phpunit/phpunit": "~5.7.0", // 5.7.27
        "guzzle/guzzle": "~3.7" // v3.9.3
    }
}