Search code examples
phpunitphp-7monolog

PHPUnit integration test for fatal error: Allowed memory size exhausted


I would like to test, if my application logs the Fatal error

PHP Fatal error:  Allowed memory size exhausted

to my monolog handler file. I would like to write an integration test for that. The problem is that when I trigger an error, PHPUnit will stop.

/**
 * @return void
 */
public function testMemoryLimitExhaustedLogToMonolog(): void
{
    // set memory limit 100mb
    ini_set('memory_limit', '100M');

    while(true) {
        $memoryExhaustingArray[] = 1;
    }
    $this->assertThatMyLogFileHasCatchedTheOutOfMemoryException();
}

Is there a way to test a fatal out of memory error in PHP7?


Solution

  • I think, there is no way of testing this fatal error, because PHPUnit crashes when it is out of memory. This error triggers script termination and can not be caught:

    Safely catch a 'Allowed memory size exhausted' error in PHP