How to quickly write debug output from within a PHP Spec test method

1k Views Asked by At

I have inherited some phpspec tests.

The test is testing the value of a method called "getFatalErrors" and reporting failure with:

 expected [array:1], but got [array:1].

I would like to see the actual contents of the array.

I have tried to hack the phpspec test class by adding lines like:

<?php

namespace spec;

use MyClass;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class MyClassSpec extends ObjectBehavior
{



    public function it_returns_a_path_problem($args,\XMLFileWrapperTest $testWrapper)
    {
        echo "foo";

        ...

        var_dump(print_r($this->getFatalErrors()->getWrappedObject(), true));
        ...
        fwrite(STDOUT, "foo");
        print_r($this->getFatalErrors()->getWrappedObject(), true)



        $this->display("foo");
    }
}

--

But I can never get any output to show on my CLI output.

How can I make some arbitrary text appear in my test output so that I 'see' what is going on as I become more familiar with PHPSpec?

4

There are 4 best solutions below

2
Iulia Cazan On

Have you tried to tail the error log and in your function to add something like error_log( print_r( $this->getFatalErrors()->getWrappedObject(), 1 ) ); or error_log( print_r( $this->getFatalErrors(), 1 ) ); ? Usually, it works, as the output is written in your server error log and you can use the terminal or console to tail on that file and see in real time the result.

2
gvf On

Just run phpspec with the -v flag, it will be more verbose.

0
John Walker On

According to the phpspec documentation on Matchers > Inline Matcher, it is possible...

to print a more verbose error message

to do this you can throw

FailureException

So, this implies that it is possible to throw FailureException to output custom messages from within your PHPSpec Example methods.

I tried this and it let me write the phrase "foo message" to my test output:

<?php

namespace spec;


use PhpSpec\ObjectBehavior;
[...]
use PhpSpec\Exception\Example\FailureException;

class MyClassSpec extends ObjectBehavior
{


    public function it_tests_something()
    {

        [...]

        throw new FailureException("foo message");
    }


}
1
realerikrani On

Try a different formatter.

When the formatter pretty is selected either in phpspec.yml using the line

formatter.name: pretty

or when executing the test runner with format flag

vendor/bin/phpspec run --format=pretty

then echo output is visible in the terminal.