1

I have the follwoing configuration:

  1. PHP 5.3.8 from xampp 1.7.7
  2. PHPUnit 3.7.13.

I am running my tests from both the commandline and from Netbeans on Windows XP. Here is my code.

class Wrapper
{
    public function wrap($text, $maxLength) {
        if(strlen($text) > $maxLength)
            return substr($text, 0, $maxLength) . "\n" . substr($text, $maxLength);
        return $text;
    }
}

class WrapperTest extends PHPUnit_Framework_TestCase
{
    protected $wrapper;
    protected function setUp() {
        $this->wrapper = new Wrapper;
    }

    public function testWrap() {
        $text = '';
        $this->assertEquals($text, $this->wrapper->wrap($text));
    }
}

The problem is that the test passes although the function is obviously missing an argument. When using Ubuntu, the test fails as expected.

5
  • does the test fail is you throw an exception yourself? What is printed if you echo $this->wrapper->wrap($text)? Commented Jan 25, 2013 at 9:53
  • /** * @expectedException InvalidArgumentException */ public function testShouldWrapEmptyString() { $textToWrap = ''; $this->assertEquals('', $this->wrapper->wrap($textToWrap)); } The echo test doesn't return anything.</br>An the test fails of course. Commented Jan 25, 2013 at 10:18
  • You should be getting a PHP error from the missing parameter, which will stop the test from executing, and not having it actually pass, but have the code stopped. Can you post the output if you run this from the command line? Commented Jan 25, 2013 at 19:33
  • This is the output from the commandline: PHPUnit 3.7.13 by Sebastian Bergmann. . Time: 0 seconds, Memory: 3.50Mb OK (1 test, 1 assertion) Commented Jan 28, 2013 at 7:22
  • Did you check your error reporting levels? Commented Jun 23, 2013 at 19:27

1 Answer 1

1

This is a catchable type of error. In order to catch the error, you can create a custom error handler. See ( http://php.net/manual/en/function.set-error-handler.php ).

In order to catch this. You can try the following.

class Wrapper
{
    public function wrap($text, $maxLength) {
        if(strlen($text) > $maxLength)
            return substr($text, 0, $maxLength) . "\n" . substr($text, $maxLength);
        return $text;
    }
}

class WrapperTest extends PHPUnit_Framework_TestCase
{
    protected $wrapper;

    protected function setUp()
    {
        set_error_handler(array($this, 'errorHandler'));
    }

    public function errorHandler($errno, $errstr, $errfile, $errline)
    {
        throw new \InvalidArgumentException(
            sprintf(
                'Missing argument. %s %s %s %s',
                $errno,
                $errstr,
                $errfile,
                $errline
            )
        );
    }

    public function testShouldThrowExceptionWhenTheresNoParamPassed()
    {
        $this->setExpectedException('\InvalidArgumentException');
        new Wrapper;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.