class MigrateExecutableMemoryExceededTest

Same name and namespace in other branches
  1. 9 core/modules/migrate/tests/src/Unit/MigrateExecutableMemoryExceededTest.php \Drupal\Tests\migrate\Unit\MigrateExecutableMemoryExceededTest
  2. 8.9.x core/modules/migrate/tests/src/Unit/MigrateExecutableMemoryExceededTest.php \Drupal\Tests\migrate\Unit\MigrateExecutableMemoryExceededTest
  3. 11.x core/modules/migrate/tests/src/Unit/MigrateExecutableMemoryExceededTest.php \Drupal\Tests\migrate\Unit\MigrateExecutableMemoryExceededTest

Tests the \Drupal\migrate\MigrateExecutable::memoryExceeded() method.

@group migrate

Hierarchy

Expanded class hierarchy of MigrateExecutableMemoryExceededTest

File

core/modules/migrate/tests/src/Unit/MigrateExecutableMemoryExceededTest.php, line 14

Namespace

Drupal\Tests\migrate\Unit
View source
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|\Prophecy\Prophecy\ObjectProphecy
   */
  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->prophesize('Drupal\\migrate\\MigrateMessageInterface');
    $this->executable = new TestMigrateExecutable($this->migration, $this->message
      ->reveal());
    $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->message
        ->display(Argument::that(fn(string $subject) => str_contains($subject, 'reclaiming memory')), 'warning')
        ->shouldBeCalledOnce();
      $this->message
        ->display(Argument::that(fn(string $subject) => str_contains($subject, $message)), 'warning')
        ->shouldBeCalledOnce();
    }
    else {
      $this->message
        ->display(Argument::cetera())
        ->shouldNotBeCalled();
    }
    $result = $this->executable
      ->memoryExceeded();
    $this->assertEquals($memory_exceeded, $result);
  }
  
  /**
   * Tests memoryExceeded method when a new batch is needed.
   */
  public function testMemoryExceededNewBatch() : void {
    // 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() : void {
    $this->runMemoryExceededTest('reclaimed enough', FALSE, $this->memoryLimit, $this->memoryLimit * 0.75);
  }
  
  /**
   * Tests memoryExceeded when memory usage is not exceeded.
   */
  public function testMemoryNotExceeded() : void {
    $this->runMemoryExceededTest('', FALSE, floor($this->memoryLimit * 0.85) - 1);
  }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title Overrides
MigrateExecutableMemoryExceededTest::$executable protected property The tested migrate executable.
MigrateExecutableMemoryExceededTest::$memoryLimit protected property The php.ini memory_limit value.
MigrateExecutableMemoryExceededTest::$message protected property The mocked migrate message.
MigrateExecutableMemoryExceededTest::$migration protected property The mocked migration entity.
MigrateExecutableMemoryExceededTest::$migrationConfiguration protected property The migration configuration, initialized to set the ID to test. Overrides MigrateTestCase::$migrationConfiguration
MigrateExecutableMemoryExceededTest::runMemoryExceededTest protected function Runs the actual test.
MigrateExecutableMemoryExceededTest::setUp protected function Overrides UnitTestCase::setUp
MigrateExecutableMemoryExceededTest::testMemoryExceededClearedEnough public function Tests memoryExceeded method when enough is cleared.
MigrateExecutableMemoryExceededTest::testMemoryExceededNewBatch public function Tests memoryExceeded method when a new batch is needed.
MigrateExecutableMemoryExceededTest::testMemoryNotExceeded public function Tests memoryExceeded when memory usage is not exceeded.
MigrateTestCase::$idMap protected property The migration ID map.
MigrateTestCase::$migrationStatus protected property Local store for mocking setStatus()/getStatus().
MigrateTestCase::createSchemaFromRow protected function Generates a table schema from a row.
MigrateTestCase::getDatabase protected function Gets an SQLite database connection object for use in tests.
MigrateTestCase::getMigration protected function Retrieves a mocked migration.
MigrateTestCase::getValue protected function Gets the value on a row for a given key.
MigrateTestCase::queryResultTest public function Tests a query.
MigrateTestCase::retrievalAssertHelper protected function Asserts tested values during test retrieval.
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
RandomGeneratorTrait::randomStringValidate Deprecated public function Callback for random string validation.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUpBeforeClass public static function
UnitTestCase::__get public function

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