|
32 | 32 | use function is_string;
|
33 | 33 | use function libxml_clear_errors;
|
34 | 34 | use function method_exists;
|
| 35 | +use function ob_clean; |
35 | 36 | use function ob_end_clean;
|
36 |
| -use function ob_get_clean; |
| 37 | +use function ob_end_flush; |
| 38 | +use function ob_flush; |
37 | 39 | use function ob_get_contents;
|
38 | 40 | use function ob_get_level;
|
| 41 | +use function ob_get_status; |
39 | 42 | use function ob_start;
|
40 | 43 | use function preg_match;
|
41 | 44 | use function restore_error_handler;
|
@@ -173,6 +176,7 @@ abstract class TestCase extends Assert implements Reorderable, SelfDescribing, T
|
173 | 176 | private ?string $outputExpectedString = null;
|
174 | 177 | private bool $outputBufferingActive = false;
|
175 | 178 | private int $outputBufferingLevel;
|
| 179 | + private ?int $outputBufferingFlushed = null; |
176 | 180 | private bool $outputRetrievedForAssertion = false;
|
177 | 181 | private bool $doesNotPerformAssertions = false;
|
178 | 182 |
|
@@ -544,11 +548,13 @@ final public function runBare(): void
|
544 | 548 | $outputBufferingStopped = false;
|
545 | 549 |
|
546 | 550 | if (!isset($e) &&
|
547 |
| - $this->hasExpectationOnOutput() && |
548 |
| - $this->stopOutputBuffering()) { |
| 551 | + $this->hasExpectationOnOutput()) { |
| 552 | + // if it fails now, we shouldn't try again later either |
549 | 553 | $outputBufferingStopped = true;
|
550 | 554 |
|
551 |
| - $this->performAssertionsOnOutput(); |
| 555 | + if ($this->stopOutputBuffering()) { |
| 556 | + $this->performAssertionsOnOutput(); |
| 557 | + } |
552 | 558 | }
|
553 | 559 |
|
554 | 560 | if ($this->status->isSuccess()) {
|
@@ -1434,43 +1440,235 @@ private function markSkippedForMissingDependency(ExecutionOrderDependency $depen
|
1434 | 1440 | $this->status = TestStatus::skipped($message);
|
1435 | 1441 | }
|
1436 | 1442 |
|
| 1443 | + private function outputBufferingCallback(string $output, int $phase): string |
| 1444 | + { |
| 1445 | + // assign it here to not get output from uncleanable children at the end |
| 1446 | + // as well as to ensure we have all output available to check |
| 1447 | + // if any children buffers have a low chunk size and already returned data |
| 1448 | + // or ob_flush was called |
| 1449 | + if ($this->outputBufferingActive) { |
| 1450 | + $this->output .= $output; |
| 1451 | + |
| 1452 | + if (($phase & PHP_OUTPUT_HANDLER_FINAL) === PHP_OUTPUT_HANDLER_FINAL) { |
| 1453 | + // don't handle, since we report an error already if our handler is missing |
| 1454 | + // since it's inconsistent here with ob_end_flush/ob_end_clean |
| 1455 | + return ''; |
| 1456 | + } |
| 1457 | + |
| 1458 | + if (($phase & PHP_OUTPUT_HANDLER_CLEAN) === PHP_OUTPUT_HANDLER_CLEAN) { |
| 1459 | + $this->outputBufferingFlushed = PHP_OUTPUT_HANDLER_CLEAN; |
| 1460 | + } elseif (($phase & PHP_OUTPUT_HANDLER_FLUSH) === PHP_OUTPUT_HANDLER_FLUSH) { |
| 1461 | + $this->outputBufferingFlushed = PHP_OUTPUT_HANDLER_FLUSH; |
| 1462 | + } |
| 1463 | + } |
| 1464 | + |
| 1465 | + return ''; |
| 1466 | + } |
| 1467 | + |
| 1468 | + // private to ensure it cannot be restarted after ending it from outside this class |
1437 | 1469 | private function startOutputBuffering(): void
|
1438 | 1470 | {
|
1439 |
| - ob_start(); |
| 1471 | + ob_start([$this, 'outputBufferingCallback']); |
1440 | 1472 |
|
1441 | 1473 | $this->outputBufferingActive = true;
|
1442 | 1474 | $this->outputBufferingLevel = ob_get_level();
|
1443 | 1475 | }
|
1444 | 1476 |
|
| 1477 | + /** |
| 1478 | + * @throws Exception |
| 1479 | + * @throws NoPreviousThrowableException |
| 1480 | + */ |
1445 | 1481 | private function stopOutputBuffering(): bool
|
1446 | 1482 | {
|
1447 |
| - $bufferingLevel = ob_get_level(); |
| 1483 | + $bufferingLevel = ob_get_level(); |
| 1484 | + $bufferingStatus = ob_get_status(); |
| 1485 | + $bufferingCallbackName = $bufferingStatus['name'] ?? ''; |
| 1486 | + $expectedBufferingCallable = static::class . '::outputBufferingCallback'; |
| 1487 | + |
| 1488 | + if ($bufferingLevel !== $this->outputBufferingLevel || |
| 1489 | + ($this->outputBufferingActive && $bufferingCallbackName !== $expectedBufferingCallable) || |
| 1490 | + $this->outputBufferingFlushed !== null) { |
| 1491 | + $asFailure = true; |
1448 | 1492 |
|
1449 |
| - if ($bufferingLevel !== $this->outputBufferingLevel) { |
1450 | 1493 | if ($bufferingLevel > $this->outputBufferingLevel) {
|
1451 | 1494 | $message = 'Test code or tested code did not close its own output buffers';
|
1452 |
| - } else { |
| 1495 | + } elseif ($bufferingLevel < $this->outputBufferingLevel) { |
1453 | 1496 | $message = 'Test code or tested code closed output buffers other than its own';
|
| 1497 | + } elseif ($this->outputBufferingActive && $bufferingCallbackName !== $expectedBufferingCallable) { |
| 1498 | + $message = 'Test code or tested code first closed output buffers other than its own and later started output buffers it did not close'; |
| 1499 | + } elseif ($this->outputBufferingFlushed !== null) { |
| 1500 | + // if we weren't in phpunit this would lead to a PHP notice |
| 1501 | + $message = 'Test code or tested code flushed or cleaned global output buffers other than its own'; |
| 1502 | + } else { |
| 1503 | + $this->outputBufferingLevel = ob_get_level(); |
| 1504 | + |
| 1505 | + return true; |
| 1506 | + } |
| 1507 | + |
| 1508 | + $hasExpectedCallable = false; |
| 1509 | + |
| 1510 | + if ($this->outputBufferingActive) { |
| 1511 | + $fullObStatus = ob_get_status(true); |
| 1512 | + $bufferingCallbackNameLevel = $fullObStatus[$this->outputBufferingLevel - 1]['name'] ?? ''; |
| 1513 | + $bufferingCallbackSizeUsed = $fullObStatus[$this->outputBufferingLevel - 1]['buffer_used'] ?? PHP_INT_MAX; |
| 1514 | + |
| 1515 | + if ($bufferingCallbackNameLevel === $expectedBufferingCallable) { |
| 1516 | + $hasExpectedCallable = true; |
| 1517 | + |
| 1518 | + foreach ($fullObStatus as $index => $obStatus) { |
| 1519 | + if ($index < $this->outputBufferingLevel) { |
| 1520 | + continue; |
| 1521 | + } |
| 1522 | + |
| 1523 | + if (($obStatus['flags'] & PHP_OUTPUT_HANDLER_REMOVABLE) === PHP_OUTPUT_HANDLER_REMOVABLE) { |
| 1524 | + continue; |
| 1525 | + } |
| 1526 | + |
| 1527 | + if (!$this->inIsolation && !$this->shouldRunInSeparateProcess()) { |
| 1528 | + $message = 'Test code contains a non-removable output buffer - run test in separate process to avoid side-effects'; |
| 1529 | + $hasExpectedCallable = false; |
| 1530 | + |
| 1531 | + break; |
| 1532 | + } |
| 1533 | + |
| 1534 | + // allow non-removable handler 1 level deeper than our handler to allow unit tests for non-removable handlers |
| 1535 | + // however only if our own handler is empty, as we cannot retrieve that from our handler if we are in a non-removable handler in a level deeper |
| 1536 | + if ($index === $this->outputBufferingLevel && $bufferingCallbackSizeUsed === 0) { |
| 1537 | + continue; |
| 1538 | + } |
| 1539 | + |
| 1540 | + if ($index === $this->outputBufferingLevel) { |
| 1541 | + $message = 'Tests with non-removable output buffer handlers must not call flush on them and the chunk size must be bigger than the expected output'; |
| 1542 | + } else { |
| 1543 | + // we cannot get the data from the handlers between our handler and the topmost non-removable handler |
| 1544 | + $message = 'Tests with multiple output buffers where any, except the first, are non-removable are not supported'; |
| 1545 | + } |
| 1546 | + |
| 1547 | + $hasExpectedCallable = false; |
| 1548 | + |
| 1549 | + break; |
| 1550 | + } |
| 1551 | + |
| 1552 | + if ($hasExpectedCallable && $this->outputBufferingFlushed === null) { |
| 1553 | + $asFailure = false; |
| 1554 | + } |
| 1555 | + } else { |
| 1556 | + // the original buffer doesn't exist anymore at that level, which means it was closed |
| 1557 | + $message = 'Test code or tested code first closed output buffers other than its own and later started output buffers it did not close'; |
| 1558 | + } |
| 1559 | + } else { |
| 1560 | + $asFailure = false; |
1454 | 1561 | }
|
1455 | 1562 |
|
1456 | 1563 | while (ob_get_level() >= $this->outputBufferingLevel) {
|
1457 |
| - ob_end_clean(); |
| 1564 | + $obStatus = ob_get_status(); |
| 1565 | + |
| 1566 | + if ($obStatus === []) { |
| 1567 | + break; |
| 1568 | + } |
| 1569 | + |
| 1570 | + // 'level' is off by 1 because 0-indexed |
| 1571 | + if ($hasExpectedCallable && $obStatus['name'] === $expectedBufferingCallable && $obStatus['level'] + 1 === $this->outputBufferingLevel) { |
| 1572 | + // our own handler |
| 1573 | + ob_end_clean(); |
| 1574 | + |
| 1575 | + continue; |
| 1576 | + } |
| 1577 | + |
| 1578 | + if (($obStatus['flags'] & PHP_OUTPUT_HANDLER_REMOVABLE) === PHP_OUTPUT_HANDLER_REMOVABLE) { |
| 1579 | + // bubble it up |
| 1580 | + ob_end_flush(); |
| 1581 | + |
| 1582 | + continue; |
| 1583 | + } |
| 1584 | + |
| 1585 | + if ($hasExpectedCallable && $obStatus['level'] === $this->outputBufferingLevel) { |
| 1586 | + $fullObStatus = ob_get_status(true); |
| 1587 | + $bufferingCallbackSizeUsed = $fullObStatus[$this->outputBufferingLevel - 1]['buffer_used'] ?? PHP_INT_MAX; |
| 1588 | + |
| 1589 | + // we are 1 level deeper than our buffer |
| 1590 | + // check again to be sure, as we cannot retrieve what's in the buffer |
| 1591 | + if ($bufferingCallbackSizeUsed !== 0) { |
| 1592 | + $hasExpectedCallable = false; |
| 1593 | + $asFailure = true; |
| 1594 | + $message = 'Tests with non-removable output buffer handlers must not call flush on them and the chunk size must be bigger than the expected output'; |
| 1595 | + } else { |
| 1596 | + // assign it since we cannot trigger our callback |
| 1597 | + // this is the reason why it's risky even then, since the ob callback of the non-removable buffer isn't called |
| 1598 | + // which could modify the output |
| 1599 | + $this->output .= (string) ob_get_contents(); |
| 1600 | + |
| 1601 | + // if we have the default output handler which doesn't modify output |
| 1602 | + // this isn't even risky |
| 1603 | + if ($obStatus['name'] === 'default output handler' && $this->outputBufferingFlushed === null) { |
| 1604 | + $message = null; |
| 1605 | + } elseif ($this->outputBufferingFlushed === null) { |
| 1606 | + $message = 'Non-removable output handler callback was not called, which could alter output'; |
| 1607 | + } else { |
| 1608 | + $asFailure = true; |
| 1609 | + } |
| 1610 | + } |
| 1611 | + } elseif (($obStatus['flags'] & PHP_OUTPUT_HANDLER_FLUSHABLE) === PHP_OUTPUT_HANDLER_FLUSHABLE) { |
| 1612 | + // bubble it up |
| 1613 | + ob_flush(); |
| 1614 | + } |
| 1615 | + |
| 1616 | + if (($obStatus['flags'] & PHP_OUTPUT_HANDLER_CLEANABLE) === PHP_OUTPUT_HANDLER_CLEANABLE) { |
| 1617 | + // make sure it's empty for subsequent runs to reduce unrelated errors |
| 1618 | + ob_clean(); |
| 1619 | + } |
| 1620 | + |
| 1621 | + // can't end any parents either |
| 1622 | + break; |
1458 | 1623 | }
|
1459 | 1624 |
|
1460 |
| - Event\Facade::emitter()->testConsideredRisky( |
| 1625 | + // reset it to stop adding more output |
| 1626 | + $this->outputBufferingActive = false; |
| 1627 | + $this->outputBufferingFlushed = null; |
| 1628 | + $this->outputBufferingLevel = ob_get_level(); |
| 1629 | + |
| 1630 | + if ($message === null) { |
| 1631 | + return true; |
| 1632 | + } |
| 1633 | + |
| 1634 | + if (!$asFailure) { |
| 1635 | + Event\Facade::emitter()->testConsideredRisky( |
| 1636 | + $this->valueObjectForEvents(), |
| 1637 | + $message, |
| 1638 | + ); |
| 1639 | + |
| 1640 | + $this->status = TestStatus::risky($message); |
| 1641 | + |
| 1642 | + return true; |
| 1643 | + } |
| 1644 | + |
| 1645 | + // it's impossible to tell if there were any PHP errors or failed assertions |
| 1646 | + $this->status = TestStatus::failure($message); |
| 1647 | + |
| 1648 | + Event\Facade::emitter()->testFailed( |
1461 | 1649 | $this->valueObjectForEvents(),
|
1462 |
| - $message, |
| 1650 | + Event\Code\ThrowableBuilder::from(new Exception($message)), |
| 1651 | + null, |
1463 | 1652 | );
|
1464 | 1653 |
|
1465 |
| - $this->status = TestStatus::risky($message); |
| 1654 | + if ($this->numberOfAssertionsPerformed() === 0 && |
| 1655 | + $this->hasExpectationOnOutput()) { |
| 1656 | + // no error that no assertions were performed |
| 1657 | + $this->addToAssertionCount(1); |
| 1658 | + } |
1466 | 1659 |
|
1467 | 1660 | return false;
|
1468 | 1661 | }
|
1469 | 1662 |
|
1470 |
| - $this->output = ob_get_clean(); |
| 1663 | + if (!$this->outputBufferingActive) { |
| 1664 | + return true; |
| 1665 | + } |
1471 | 1666 |
|
1472 |
| - $this->outputBufferingActive = false; |
1473 |
| - $this->outputBufferingLevel = ob_get_level(); |
| 1667 | + ob_end_clean(); |
| 1668 | + |
| 1669 | + $this->outputBufferingActive = false; |
| 1670 | + $this->outputBufferingFlushed = null; |
| 1671 | + $this->outputBufferingLevel = ob_get_level(); |
1474 | 1672 |
|
1475 | 1673 | return true;
|
1476 | 1674 | }
|
|
0 commit comments