2

phpunit test:

public function testSizeOver64K() {
        try {
            $this->login();
            $scriptname = 'test script4';
            $this->fixture->installScript($scriptname, $this->scripts[$scriptname]);
        }
        catch (Exception $expected) {
            return;
        }
        $this->fail('An expected exception has not been raised.');

    }

Functions methods it calls

function installScript($scriptname, $script, $makeactive = false)
    {
        $this->cmdPutScript($scriptname, $script);

        if ($makeactive)
            $this->cmdSetActive($scriptname);

        return true;
    }
private function cmdPutScript($scriptname, $scriptdata)
    {
        if (self::STATE_TRANSACTION != $this->state) {
            $msg = 'Not currently in TRANSACTION state';
            $code = 1;
            throw new Exception($msg, $code);
        }

        $stringLength = $this->getLineLength($scriptdata);

        $this->doCmd(sprintf("PUTSCRIPT \"%s\" {%d+}\r\n%s", $scriptname, $stringLength, $scriptdata));

        return true;
    }
private function getLineLength($string) {
        if (extension_loaded('mbstring') || @dl(PHP_SHLIB_PREFIX.'mbstring.'.PHP_SHLIB_SUFFIX)) {
            $lenght = mb_strlen($string,'8bit');
            if ( $lenght > 65536 ) {
                $msg = "Script is over 64K";
                throw new Exception($msg);
            }   
            return $lenght;
        } else {
            $lenght = strlen($string);
            if ( $lenght > 65536 ) {
                $msg = "Script is over 64K";
                throw new Exception($msg);
            }   
            return $lenght;
        }
    }

Can someone give tips why phpunit doesnt catch the exception?

2
  • Sorry, no idea why $this->fixture->installScript doesn't work as you expect it to. Is error reporting turned on? Maybe use a debugger :) or put in some output before the ->installScript call and into cmdPutScript to see if your code actually works there. --- I don't see any reason why phpunit would cause a problem there so i just have to guess it's your code
    – edorian
    Commented Mar 2, 2011 at 8:38
  • when i run the orginal code it works as suposed and throws exception. It seems phpunit doesn't execute installScript() for some reasons
    – martins
    Commented Mar 2, 2011 at 8:44

2 Answers 2

3

Use a debugger and step through the testcase to make sure your code actually throws the Exception in the first place. It's impossible to tell from your code whether the environment is setup in a way that would result in an Exception.


On a sidenote, you are supposed to throw less generic exceptions. You are using try/catch so the following is not applicable to your problem at hand, but take note of

Implemented GH-88: @expectedException (and setExpectedException()) no longer accept Exception as the expected exception class.

See changelog for PHPUnit 3.6 and https://github.com/sebastianbergmann/phpunit/pull/88

2

Chances are the problem is in your code and is not with phpunit at all.

See this test case:

<?php

function foo() {
    throw new Exception("boom");
}
class MyFooTest extends PHPUnit_Framework_TestCase {

    public function testArguments() {
        try {
            foo();
        } catch (Exception $e) {
            return;
        }
        $this->fail("nope");

    }
}

it prints:

 phpunit mep.php
PHPUnit 3.5.12 by Sebastian Bergmann.

.

Time: 0 seconds, Memory: 3.00Mb

OK (1 test, 0 assertions)

so boiled down to the essentials it works. It would be pretty strange if phpunit would change any of that behavior

Not the answer you're looking for? Browse other questions tagged or ask your own question.