fmalk/codeigniter-phpunit

Cause unit test to fail on php warnings and notices

rreynier opened this issue · 4 comments

Currently php warrnings and notices do not cause a unit test to fail. I propose updating /system/core/Exceptions.php with the following addition:

<?php
        if (defined('PHPUNIT_TEST')) {
            throw new PHPUnit_Framework_Exception($message, 500);
        }

This would make that method look like:

<?php
    /**
     * Native PHP error handler
     *
     * @access  private
     * @param   string  the error severity
     * @param   string  the error string
     * @param   string  the error filepath
     * @param   string  the error line number
     * @return  string
     */
    function show_php_error($severity, $message, $filepath, $line)
    {
        if (defined('PHPUNIT_TEST')) {
            throw new PHPUnit_Framework_Exception($message, 500);
        }
        $severity = ( ! isset($this->levels[$severity])) ? $severity : $this->levels[$severity];

        $filepath = str_replace("\\", "/", $filepath);

        // For safety reasons we do not show the full file path
        if (FALSE !== strpos($filepath, '/'))
        {
            $x = explode('/', $filepath);
            $filepath = $x[count($x)-2].'/'.end($x);
        }

        if (ob_get_level() > $this->ob_level + 1)
        {
            ob_end_flush();
        }
        ob_start();
        include(APPPATH.'errors/error_php.php');
        $buffer = ob_get_contents();
        ob_end_clean();
        echo $buffer;
    }

Thoughts?

you should use /application/core/MY_Exceptions.php instead.

IMO, it's not good idea to overwrite /system/core too much.

Thanks Feryardiant, you are absolutely correct.

@rreynier there's no need to hack or change anything in your code, these scenarios are supported by PHPUnit, you can change your phpunit.xml to enable convertErrorsToExceptions:

<phpunit bootstrap="application/tests/bootstrap.php"
...
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true">
...
</phpunitl>

So any of those will be throw as Exceptions for you test cases.

Ah ha, thats even better!