Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AssertionError "Undefined index: file" #2394

Closed
Fleshgrinder opened this issue Dec 13, 2016 · 5 comments
Closed

AssertionError "Undefined index: file" #2394

Fleshgrinder opened this issue Dec 13, 2016 · 5 comments
Assignees
Labels
type/bug Something is broken

Comments

@Fleshgrinder
Copy link

Fleshgrinder commented Dec 13, 2016

Handling of AssertionErrors was introduced in 5.5 (see this commit) where we have the following lines of code:

        } catch (AssertionError $e) {
            // ...
            $frame   = $e->getTrace()[0];
            $e = new PHPUnit_Framework_AssertionFailedError(
                sprintf(
                    '%s in %s:%s',
                    $e->getMessage(),
                    $frame['file'],
                    $frame['line']
                )
            );
        }

The problem is that this code is not compatible with PHP 5 and a custom AssertionError that is created from the assert callback feature. We have this at our company but also for instance Drupal 8 has it.

The first frame in such a case does not contain a file nor a line member since that one contains a closure and the second frame is the correct one. First entries from an example stack trace:

$frames = $e->getTrace();
$frames[0] = [
    'function' => '{closure}',
    'args' => [
        '/path/to/file.php',
        42,
        '',
        'assertion message',
    ],
];
$frames[1] = [
    'file' => '/path/to/file.php',
    'line' => 42,
    'function' => 'assert',
    'args' => [
        false,
        'assertion message',
    ],
];

A simple and supposed fix for this would be to investigate if the first frame contains {closure} in the function member and then check if the next frame is from assert. Of course, this is no guarantee that one just found the right frame.

What do you think how we should handle this? It is definitely a bug and makes all versions that contain the current logic incompatible with code that uses custom assertion errors via callbacks paired with PHP 5.

@sebastianbergmann
Copy link
Owner

The only solution I see is to only execute the code in question on PHP 7 where AssertionError is a built-in class.

@sebastianbergmann sebastianbergmann added the type/bug Something is broken label Dec 13, 2016
@sebastianbergmann sebastianbergmann self-assigned this Dec 13, 2016
@Fleshgrinder
Copy link
Author

It would definitely mitigate all possible edge cases and you can continue accessing the first frame directly since you can be sure that it has the right format. 👍

@sebastianbergmann
Copy link
Owner

Does 3ad5426 solve the issue for you?

@Fleshgrinder
Copy link
Author

Yes, this does solve the problem nicely and kudos for being so fast in replying to tickets. 👍

@sebastianbergmann
Copy link
Owner

Thank you for the feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/bug Something is broken
Projects
None yet
Development

No branches or pull requests

2 participants