MigrateExecutableMemoryExceededTest.php

Same filename and directory in other branches
  1. 8.9.x core/modules/migrate/tests/src/Unit/MigrateExecutableMemoryExceededTest.php
  2. 10 core/modules/migrate/tests/src/Unit/MigrateExecutableMemoryExceededTest.php
  3. 11.x core/modules/migrate/tests/src/Unit/MigrateExecutableMemoryExceededTest.php

Namespace

Drupal\Tests\migrate\Unit

File

core/modules/migrate/tests/src/Unit/MigrateExecutableMemoryExceededTest.php

View source
<?php

namespace Drupal\Tests\migrate\Unit;


/**
 * Tests the \Drupal\migrate\MigrateExecutable::memoryExceeded() method.
 *
 * @group migrate
 */
class MigrateExecutableMemoryExceededTest extends MigrateTestCase {
    
    /**
     * The mocked migration entity.
     *
     * @var \Drupal\migrate\Plugin\MigrationInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $migration;
    
    /**
     * The mocked migrate message.
     *
     * @var \Drupal\migrate\MigrateMessageInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $message;
    
    /**
     * The tested migrate executable.
     *
     * @var \Drupal\Tests\migrate\Unit\TestMigrateExecutable
     */
    protected $executable;
    
    /**
     * The migration configuration, initialized to set the ID to test.
     *
     * @var array
     */
    protected $migrationConfiguration = [
        'id' => 'test',
    ];
    
    /**
     * The php.ini memory_limit value.
     *
     * @var int
     */
    protected $memoryLimit = 10000000;
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        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());
    }
    
    /**
     * Runs the actual test.
     *
     * @param string $message
     *   The second message to assert.
     * @param bool $memory_exceeded
     *   Whether to test the memory exceeded case.
     * @param int|null $memory_usage_first
     *   (optional) The first memory usage value. Defaults to NULL.
     * @param int|null $memory_usage_second
     *   (optional) The fake amount of memory usage reported after memory reclaim.
     *   Defaults to NULL.
     * @param int|null $memory_limit
     *   (optional) The memory limit. Defaults to NULL.
     */
    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->exactly(2))
                ->method('display')
                ->withConsecutive([
                $this->callback(function ($subject) {
                    return mb_stripos((string) $subject, 'reclaiming memory') !== FALSE;
                }),
            ], [
                $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);
    }
    
    /**
     * Tests memoryExceeded method when a new batch is needed.
     */
    public function testMemoryExceededNewBatch() {
        // First case try reset and then start new batch.
        $this->runMemoryExceededTest('starting new batch', TRUE);
    }
    
    /**
     * Tests memoryExceeded method when enough is cleared.
     */
    public function testMemoryExceededClearedEnough() {
        $this->runMemoryExceededTest('reclaimed enough', FALSE, $this->memoryLimit, $this->memoryLimit * 0.75);
    }
    
    /**
     * Tests memoryExceeded when memory usage is not exceeded.
     */
    public function testMemoryNotExceeded() {
        $this->runMemoryExceededTest('', FALSE, floor($this->memoryLimit * 0.85) - 1);
    }

}

Classes

Title Deprecated Summary
MigrateExecutableMemoryExceededTest Tests the \Drupal\migrate\MigrateExecutable::memoryExceeded() method.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.