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

Fix error location #882

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 30 additions & 5 deletions classes/asserter/exception.php
Expand Up @@ -19,14 +19,15 @@ public function __construct(atoum\asserter $asserter, $message)
$line = null;
$function = null;

foreach (array_filter(debug_backtrace(false), function ($backtrace) use ($file) {
return isset($backtrace['file']) === true && $backtrace['file'] === $file;
}) as $backtrace) {
if ($line === null && isset($backtrace['line']) === true) {
$backtrace = $this->getBacktrace($test);
if ($backtrace !== null) {
$file = $backtrace['file'] ?? $file;

if (isset($backtrace['line']) === true) {
$line = $backtrace['line'];
}

if ($function === null && isset($backtrace['object']) === true && isset($backtrace['function']) === true && $backtrace['object'] === $asserter && $backtrace['function'] !== '__call') {
if (isset($backtrace['object']) === true && isset($backtrace['function']) === true && $backtrace['object'] === $asserter && $backtrace['function'] !== '__call') {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed that this cannot work. Indeed debug_backtrace(false) will not provide object (I did not changed that).
If I change the call to debug_backtrace(), messages are changed to:

 > There are 2 failures:
=> tests\units\MyClass::testMethod1():
- In file /path/to/tests/MyClass.php on line 106, boolean() failed: boolean(false) is not true
+ In file /path/to/tests/MyClass.php on line 106, boolean::isTrue() failed: boolean(false) is not true
 -Expected
 +Actual
 @@ -1 +1 @@
 -bool(true)
 +bool(false)
=> tests\units\MyClass::testMethod2():
- In file /path/to/tests/MyClass.php on line 55, array() failed: array(3) has size 3, expected size 12
+ In file /path/to/tests/MyClass.php on line 55, array::hasSize() failed: array(3) has size 3, expected size 12

Should I fix this too ?

$function = $backtrace['function'];
}
}
Expand All @@ -45,4 +46,28 @@ public function __construct(atoum\asserter $asserter, $message)

parent::__construct($message, $code);
}


protected function getBacktrace(atoum\test $test)
{
$debugBacktrace = debug_backtrace(false);
foreach ($debugBacktrace as $key => $value) {
if (isset($value['class']) === false) {
continue;
}

if (
$value['class'] === $test->getClass()
|| is_subclass_of($test->getClass(), $value['class'], true)
) {
if (isset($debugBacktrace[$key - 1]) === true) {
$key -= 1;
}

return $debugBacktrace[$key];
}
}

return null;
}
}