Skip to content

Add expectErrorLog() for expecting error_log() output#6118

Closed
staabm wants to merge 15 commits intosebastianbergmann:mainfrom
staabm:bug2155
Closed

Add expectErrorLog() for expecting error_log() output#6118
staabm wants to merge 15 commits intosebastianbergmann:mainfrom
staabm:bug2155

Conversation

@staabm
Copy link
Copy Markdown
Contributor

@staabm staabm commented Feb 5, 2025

summary:

  • when a test which invokes error_log() without a call to expectsErrorLog(), the output is directly rendered in-between the summary (as tested in Regression test for error_log() #6127)
  • when a test which invokes error_log() with a call to expectsErrorLog(), no more output is rendered in-between the test summary printing. when no error_log() is invoked the test is considered failed.

backstory:

while looking into #2155 (comment) I realized PHPunit is already somehow capturing the error_log() output.

I think this means we don't need a workaround like #2155 (comment) in phpunit itself, but just a API which allows us to make assertions on the already captured output.

DX wise I think there is also some room for improvement since the error_log'ged string just shows up in the test-debug output without any indication where this string is coming from (the test-author needs to look into the code beeing tested to realize that this string originates from a error_log) or how to make assertions on it.

With this PR I am just sending a test so we can see the status quo and discuss where to move from here

closes #2155

@staabm
Copy link
Copy Markdown
Contributor Author

staabm commented Feb 7, 2025

I tried debugging it a bit more to find out how phpunit knows what the unit-under-test passed to error_log in the PRs test-case.

PHPUnit Started (PHPUnit 12.0-gd39bf28f4 using PHP 8.3.16 (cli) on Darwin)
Test Runner Configured
Bootstrap Finished (/Users/m.staab/Documents/GitHub/phpunit/tests/bootstrap.php)
Event Facade Sealed
Test Suite Loaded (1 test)
Test Runner Started
Test Suite Sorted
Test Runner Execution Started (1 test)
Test Suite Started (PHPUnit\TestFixture\Issue2155\Issue2155Test, 1 test)
Test Preparation Started (PHPUnit\TestFixture\Issue2155\Issue2155Test::testOne)
Test Prepared (PHPUnit\TestFixture\Issue2155\Issue2155Test::testOne)
logged a side effect
Test Passed (PHPUnit\TestFixture\Issue2155\Issue2155Test::testOne)
Test Finished (PHPUnit\TestFixture\Issue2155\Issue2155Test::testOne)
Test Suite Finished (PHPUnit\TestFixture\Issue2155\Issue2155Test, 1 test)
Test Runner Execution Finished
Test Runner Finished
PHPUnit Finished (Shell Exit Code: 0)

I can see the string also be mentioned in the event log stream of --debug.
@localheinz could you give me a hint how I can find out where this logged a side effect string is coming from?

@staabm
Copy link
Copy Markdown
Contributor Author

staabm commented Feb 7, 2025

ohh I think I understand the problem now. error_log is just writing into stderr. its not a event happening and beeing rendered by the event system

@clxmstaab clxmstaab force-pushed the bug2155 branch 2 times, most recently from 1507fae to fa8961d Compare February 7, 2025 13:11
@staabm staabm changed the title Test error_log() in tests Allow assertions on error_log() output Feb 7, 2025
Comment thread tests/end-to-end/regression/2155/Issue2155Test.php Outdated
@codecov
Copy link
Copy Markdown

codecov Bot commented Feb 7, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.29%. Comparing base (f5fe9d1) to head (f3d62cf).

Additional details and impacted files
@@            Coverage Diff            @@
##               main    #6118   +/-   ##
=========================================
  Coverage     95.29%   95.29%           
- Complexity     6755     6758    +3     
=========================================
  Files           726      726           
  Lines         21300    21311   +11     
=========================================
+ Hits          20298    20309   +11     
  Misses         1002     1002           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@staabm staabm marked this pull request as ready for review February 7, 2025 13:28
@sebastianbergmann sebastianbergmann added type/enhancement A new idea that should be implemented feature/assertion Issues related to assertions and expectations labels Feb 7, 2025
@sebastianbergmann sebastianbergmann added this to the PHPUnit 12.1 milestone Feb 7, 2025
Comment thread tests/end-to-end/regression/2155/Issue2155Test.php
@staabm staabm marked this pull request as draft February 14, 2025 10:22
@staabm staabm changed the title Allow assertions on error_log() output Support expectsErrorLog() Feb 14, 2025
@staabm staabm force-pushed the bug2155 branch 2 times, most recently from a078ae1 to 5b5c7ee Compare February 15, 2025 08:09
@staabm staabm marked this pull request as ready for review February 15, 2025 08:39
@staabm
Copy link
Copy Markdown
Contributor Author

staabm commented Feb 15, 2025

I think we are in better shape now (put a new summary into the PR description)

Comment thread src/Framework/TestCase.php Outdated
$this->outputExpectedString = $expectedString;
}

final protected function expectsErrorLog(): void
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

This should be expectErrorLog() to be consistent with expectOutputString, for example.

@sebastianbergmann sebastianbergmann changed the title Support expectsErrorLog() Add expectErrorLog() for expecting error_log() output Feb 17, 2025
@@ -0,0 +1,31 @@
--TEST--
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

expectErrorLog() is a new feature. Therefore, we should not put its end-to-end tests into the regression directory. The generic would be acceptable for now.

@staabm
Copy link
Copy Markdown
Contributor Author

staabm commented Feb 17, 2025

great catch, fixed it. thank you!

@sebastianbergmann
Copy link
Copy Markdown
Owner

Merged manually, thanks.

$testArguments = array_merge($this->data, array_values($this->dependencyInput));

$capture = tmpfile();
$errorLogPrevious = ini_set('error_log', stream_get_meta_data($capture)['uri']);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I saw your php-src feature request to support in memory stream. I think stream wrapper would work here - https://www.php.net/manual/en/stream.streamwrapper.example-1.php

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Also creating tmp. file for every testcase is far from ideal.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

feel free to improve the implementation with a followup PR

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I reported the main issue in #6197.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think stream wrapper would work here - https://www.php.net/manual/en/stream.streamwrapper.example-1.php

sadly even this is currently not possible - php/php-src#18530

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Apr 14, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

feature/assertion Issues related to assertions and expectations type/enhancement A new idea that should be implemented

Projects

None yet

Development

Successfully merging this pull request may close these issues.

expectOutputString and error_log

3 participants