MigrateExecutableMemoryExceededTest.php in Drupal 8
File
core/modules/migrate/tests/src/Unit/MigrateExecutableMemoryExceededTest.php
View source
<?php
namespace Drupal\Tests\migrate\Unit;
class MigrateExecutableMemoryExceededTest extends MigrateTestCase {
protected $migration;
protected $message;
protected $executable;
protected $migrationConfiguration = [
'id' => 'test',
];
protected $memoryLimit = 10000000;
protected function setUp() {
parent::setUp();
$this->migration = $this
->getMigration();
$this->message = $this
->createMock('Drupal\\migrate\\MigrateMessageInterface');
$this->executable = new TestMigrateExecutable($this->migration, $this->message);
$this->executable
->setStringTranslation($this
->getStringTranslationStub());
}
protected function runMemoryExceededTest($message, $memory_exceeded, $memory_usage_first = NULL, $memory_usage_second = NULL, $memory_limit = NULL) {
$this->executable
->setMemoryLimit($memory_limit ?: $this->memoryLimit);
$this->executable
->setMemoryUsage($memory_usage_first ?: $this->memoryLimit, $memory_usage_second ?: $this->memoryLimit);
$this->executable
->setMemoryThreshold(0.85);
if ($message) {
$this->executable->message
->expects($this
->at(0))
->method('display')
->with($this
->callback(function ($subject) {
return mb_stripos((string) $subject, 'reclaiming memory') !== FALSE;
}));
$this->executable->message
->expects($this
->at(1))
->method('display')
->with($this
->callback(function ($subject) use ($message) {
return mb_stripos((string) $subject, $message) !== FALSE;
}));
}
else {
$this->executable->message
->expects($this
->never())
->method($this
->anything());
}
$result = $this->executable
->memoryExceeded();
$this
->assertEquals($memory_exceeded, $result);
}
public function testMemoryExceededNewBatch() {
$this
->runMemoryExceededTest('starting new batch', TRUE);
}
public function testMemoryExceededClearedEnough() {
$this
->runMemoryExceededTest('reclaimed enough', FALSE, $this->memoryLimit, $this->memoryLimit * 0.75);
}
public function testMemoryNotExceeded() {
$this
->runMemoryExceededTest('', FALSE, floor($this->memoryLimit * 0.85) - 1);
}
}