1

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 Answers4

1

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.

  • Thanks. This looks like a promising solution. I will have to try this out when I next in the 'BDD developer's role'. – John Walker Sep 04 '19 at 12:14
0

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.

  • This looks like a comment more than an answer – GabrieleMartini Feb 05 '19 at 09:31
  • Thanks for your suggestion. The "getFatalErrors" is actually a custom method on the object being tested. It returns an array of error messages that the object has collected during its operation. It is not anything to do with PHP in-built Errors. Potentially we could refactor it to use PHP errors, but for now I am just trying to quickly understand the code/tests that I inherited. – John Walker Feb 06 '19 at 11:03
0

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

gvf
  • 1,039
  • 7
  • 6
  • Thanks. Useful to know. But, this still does not show the contents of arrays. I'd really like to have the freedom to write custom output from within a test case so I can 'hack' to 'see' stuff. `$ bin/phpspec run -v XMLCounterCLI 42 - it returns a path problem ... >makeCall([obj:Prophecy\Prophecy\ObjectProphecy], "setRecursive", [array:1]) ...` – John Walker Feb 06 '19 at 11:11
  • I have just tried it. I added a few v's to run the command as: `$ bin/phpspec run -vvv` and I still could not write out any custom debug output via; echo,fwrite or $this->display("foo"); – John Walker Feb 28 '19 at 16:55
0

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");
    }


}