3 namespace Drupal\Tests\migrate\Unit;
6 * Tests the \Drupal\migrate\MigrateExecutable::memoryExceeded() method.
10 class MigrateExecutableMemoryExceededTest extends MigrateTestCase {
13 * The mocked migration entity.
15 * @var \Drupal\migrate\Plugin\MigrationInterface|\PHPUnit_Framework_MockObject_MockObject
20 * The mocked migrate message.
22 * @var \Drupal\migrate\MigrateMessageInterface|\PHPUnit_Framework_MockObject_MockObject
27 * The tested migrate executable.
29 * @var \Drupal\Tests\migrate\Unit\TestMigrateExecutable
31 protected $executable;
34 * The migration configuration, initialized to set the ID to test.
38 protected $migrationConfiguration = [
43 * The php.ini memory_limit value.
47 protected $memoryLimit = 10000000;
52 protected function setUp() {
54 $this->migration = $this->getMigration();
55 $this->message = $this->getMock('Drupal\migrate\MigrateMessageInterface');
57 $this->executable = new TestMigrateExecutable($this->migration, $this->message);
58 $this->executable->setStringTranslation($this->getStringTranslationStub());
62 * Runs the actual test.
64 * @param string $message
65 * The second message to assert.
66 * @param bool $memory_exceeded
67 * Whether to test the memory exceeded case.
68 * @param int|null $memory_usage_first
69 * (optional) The first memory usage value. Defaults to NULL.
70 * @param int|null $memory_usage_second
71 * (optional) The fake amount of memory usage reported after memory reclaim.
73 * @param int|null $memory_limit
74 * (optional) The memory limit. Defaults to NULL.
76 protected function runMemoryExceededTest($message, $memory_exceeded, $memory_usage_first = NULL, $memory_usage_second = NULL, $memory_limit = NULL) {
77 $this->executable->setMemoryLimit($memory_limit ?: $this->memoryLimit);
78 $this->executable->setMemoryUsage($memory_usage_first ?: $this->memoryLimit, $memory_usage_second ?: $this->memoryLimit);
79 $this->executable->setMemoryThreshold(0.85);
81 $this->executable->message->expects($this->at(0))
83 ->with($this->stringContains('reclaiming memory'));
84 $this->executable->message->expects($this->at(1))
86 ->with($this->stringContains($message));
89 $this->executable->message->expects($this->never())
90 ->method($this->anything());
92 $result = $this->executable->memoryExceeded();
93 $this->assertEquals($memory_exceeded, $result);
97 * Tests memoryExceeded method when a new batch is needed.
99 public function testMemoryExceededNewBatch() {
100 // First case try reset and then start new batch.
101 $this->runMemoryExceededTest('starting new batch', TRUE);
105 * Tests memoryExceeded method when enough is cleared.
107 public function testMemoryExceededClearedEnough() {
108 $this->runMemoryExceededTest('reclaimed enough', FALSE, $this->memoryLimit, $this->memoryLimit * 0.75);
112 * Tests memoryExceeded when memory usage is not exceeded.
114 public function testMemoryNotExceeded() {
115 $this->runMemoryExceededTest('', FALSE, floor($this->memoryLimit * 0.85) - 1);