class BatchBuilderTest

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Core/Batch/BatchBuilderTest.php \Drupal\Tests\Core\Batch\BatchBuilderTest
  2. 8.9.x core/tests/Drupal/Tests/Core/Batch/BatchBuilderTest.php \Drupal\Tests\Core\Batch\BatchBuilderTest
  3. 11.x core/tests/Drupal/Tests/Core/Batch/BatchBuilderTest.php \Drupal\Tests\Core\Batch\BatchBuilderTest

Tests for the batch builder class.

@coversDefaultClass \Drupal\Core\Batch\BatchBuilder

@group system

Hierarchy

Expanded class hierarchy of BatchBuilderTest

File

core/tests/Drupal/Tests/Core/Batch/BatchBuilderTest.php, line 18

Namespace

Drupal\Tests\Core\Batch
View source
class BatchBuilderTest extends UnitTestCase {
  
  /**
   * Tests the default values.
   *
   * @covers ::toArray
   */
  public function testDefaultValues() : void {
    $batch = (new BatchBuilder())->toArray();
    $this->assertIsArray($batch);
    $this->assertArrayHasKey('operations', $batch);
    $this->assertIsArray($batch['operations']);
    $this->assertEmpty($batch['operations'], 'Operations array is empty.');
    $this->assertEquals(new TranslatableMarkup('Processing'), $batch['title']);
    $this->assertEquals(new TranslatableMarkup('Initializing.'), $batch['init_message']);
    $this->assertEquals(new TranslatableMarkup('Completed @current of @total.'), $batch['progress_message']);
    $this->assertEquals(new TranslatableMarkup('An error has occurred.'), $batch['error_message']);
    $this->assertNull($batch['finished']);
    $this->assertNull($batch['file']);
    $this->assertArrayHasKey('library', $batch);
    $this->assertIsArray($batch['library']);
    $this->assertEmpty($batch['library']);
    $this->assertArrayHasKey('url_options', $batch);
    $this->assertIsArray($batch['url_options']);
    $this->assertEmpty($batch['url_options']);
    $this->assertArrayHasKey('progressive', $batch);
    $this->assertTrue($batch['progressive']);
    $this->assertArrayNotHasKey('queue', $batch);
  }
  
  /**
   * Tests setTitle().
   *
   * @covers ::setTitle
   */
  public function testSetTitle() : void {
    $batch = (new BatchBuilder())->setTitle(new TranslatableMarkup('New Title'))
      ->toArray();
    $this->assertEquals(new TranslatableMarkup('New Title'), $batch['title']);
  }
  
  /**
   * Tests setFinishCallback().
   *
   * @covers ::setFinishCallback
   */
  public function testSetFinishCallback() : void {
    $batch = (new BatchBuilder())->setFinishCallback('\\Drupal\\Tests\\Core\\Batch\\BatchBuilderTest::finishedCallback')
      ->toArray();
    $this->assertEquals('\\Drupal\\Tests\\Core\\Batch\\BatchBuilderTest::finishedCallback', $batch['finished']);
  }
  
  /**
   * Tests setInitMessage().
   *
   * @covers ::setInitMessage
   */
  public function testSetInitMessage() : void {
    $batch = (new BatchBuilder())->setInitMessage(new TranslatableMarkup('New initialization message.'))
      ->toArray();
    $this->assertEquals(new TranslatableMarkup('New initialization message.'), $batch['init_message']);
  }
  
  /**
   * Tests setProgressMessage().
   *
   * @covers ::setProgressMessage
   */
  public function testSetProgressMessage() : void {
    $batch = (new BatchBuilder())->setProgressMessage(new TranslatableMarkup('Batch in progress...'))
      ->toArray();
    $this->assertEquals(new TranslatableMarkup('Batch in progress...'), $batch['progress_message']);
  }
  
  /**
   * Tests setErrorMessage().
   */
  public function testSetErrorMessage() : void {
    $batch = (new BatchBuilder())->setErrorMessage(new TranslatableMarkup('Oops. An error has occurred :('))
      ->toArray();
    $this->assertEquals(new TranslatableMarkup('Oops. An error has occurred :('), $batch['error_message']);
  }
  
  /**
   * Tests setFile().
   *
   * @covers ::setFile
   */
  public function testSetFile() : void {
    $filename = dirname(__DIR__, 6) . '/core/modules/system/tests/modules/batch_test/batch_test.callbacks.inc';
    $this->assertIsNotCallable('_batch_test_callback_1');
    $this->assertIsNotCallable('_batch_test_finished_1');
    $batch = (new BatchBuilder())->setFile($filename)
      ->setFinishCallback('_batch_test_finished_1')
      ->addOperation('_batch_test_callback_1', [])
      ->toArray();
    $this->assertEquals($filename, $batch['file']);
    $this->assertEquals([
      [
        '_batch_test_callback_1',
        [],
      ],
    ], $batch['operations']);
    $this->assertEquals('_batch_test_finished_1', $batch['finished']);
    $this->assertIsCallable('_batch_test_callback_1');
    $this->assertIsCallable('_batch_test_finished_1');
  }
  
  /**
   * Tests setting and adding libraries.
   *
   * @covers ::setLibraries
   */
  public function testAddingLibraries() : void {
    $batch = (new BatchBuilder())->setLibraries([
      'only/library',
    ])
      ->toArray();
    $this->assertEquals([
      'only/library',
    ], $batch['library']);
  }
  
  /**
   * Tests setProgressive().
   *
   * @covers ::setProgressive
   */
  public function testSetProgressive() : void {
    $batch_builder = new BatchBuilder();
    $batch = $batch_builder->setProgressive(FALSE)
      ->toArray();
    $this->assertFalse($batch['progressive']);
    $batch = $batch_builder->setProgressive(TRUE)
      ->toArray();
    $this->assertTrue($batch['progressive']);
  }
  
  /**
   * Tests setQueue().
   *
   * @covers ::setQueue
   */
  public function testSetQueue() : void {
    $batch = (new BatchBuilder())->setQueue('BatchName', '\\Drupal\\Core\\Queue\\Batch')
      ->toArray();
    $this->assertEquals([
      'name' => 'BatchName',
      'class' => '\\Drupal\\Core\\Queue\\Batch',
    ], $batch['queue'], 'Batch queue has been set.');
  }
  
  /**
   * Tests queue class exists.
   *
   * @covers ::setQueue
   */
  public function testQueueExists() : void {
    $batch_builder = new BatchBuilder();
    $this->expectException(\InvalidArgumentException::class);
    $this->expectExceptionMessage('Class \\ThisIsNotAClass does not exist.');
    $batch_builder->setQueue('BatchName', '\\ThisIsNotAClass');
  }
  
  /**
   * Tests queue class implements \Drupal\Core\Queue\QueueInterface.
   *
   * @covers ::setQueue
   */
  public function testQueueImplements() : void {
    $batch_builder = new BatchBuilder();
    $this->expectException(\InvalidArgumentException::class);
    $this->expectExceptionMessage('Class Exception does not implement \\Drupal\\Core\\Queue\\QueueInterface.');
    $batch_builder->setQueue('BatchName', \Exception::class);
  }
  
  /**
   * Tests setUrlOptions().
   *
   * @covers ::setUrlOptions
   */
  public function testSetUrlOptions() : void {
    $options = [
      'absolute' => TRUE,
      'language' => 'de',
    ];
    $batch = (new BatchBuilder())->setUrlOptions($options)
      ->toArray();
    $this->assertEquals($options, $batch['url_options']);
  }
  
  /**
   * Tests addOperation().
   *
   * @covers ::addOperation
   */
  public function testAddOperation() : void {
    $batch_builder = new BatchBuilder();
    $batch = $batch_builder->addOperation('\\Drupal\\Tests\\Core\\Batch\\BatchBuilderTest::operationCallback')
      ->toArray();
    $this->assertEquals([
      [
        '\\Drupal\\Tests\\Core\\Batch\\BatchBuilderTest::operationCallback',
        [],
      ],
    ], $batch['operations']);
    $batch = $batch_builder->addOperation('\\Drupal\\Tests\\Core\\Batch\\BatchBuilderTest::operationCallback', [
      2,
    ])
      ->addOperation('\\Drupal\\Tests\\Core\\Batch\\BatchBuilderTest::operationCallback', [
      3,
    ])
      ->toArray();
    $this->assertEquals([
      [
        '\\Drupal\\Tests\\Core\\Batch\\BatchBuilderTest::operationCallback',
        [],
      ],
      [
        '\\Drupal\\Tests\\Core\\Batch\\BatchBuilderTest::operationCallback',
        [
          2,
        ],
      ],
      [
        '\\Drupal\\Tests\\Core\\Batch\\BatchBuilderTest::operationCallback',
        [
          3,
        ],
      ],
    ], $batch['operations']);
  }
  
  /**
   * Empty callback for the tests.
   *
   * @internal
   */
  public static function finishedCallback() {
  }
  
  /**
   * Empty callback for the tests.
   *
   * @internal
   */
  public static function operationCallback() {
  }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overrides
BatchBuilderTest::finishedCallback public static function Empty callback for the tests.
BatchBuilderTest::operationCallback public static function Empty callback for the tests.
BatchBuilderTest::testAddingLibraries public function Tests setting and adding libraries.
BatchBuilderTest::testAddOperation public function Tests addOperation().
BatchBuilderTest::testDefaultValues public function Tests the default values.
BatchBuilderTest::testQueueExists public function Tests queue class exists.
BatchBuilderTest::testQueueImplements public function Tests queue class implements \Drupal\Core\Queue\QueueInterface.
BatchBuilderTest::testSetErrorMessage public function Tests setErrorMessage().
BatchBuilderTest::testSetFile public function Tests setFile().
BatchBuilderTest::testSetFinishCallback public function Tests setFinishCallback().
BatchBuilderTest::testSetInitMessage public function Tests setInitMessage().
BatchBuilderTest::testSetProgressive public function Tests setProgressive().
BatchBuilderTest::testSetProgressMessage public function Tests setProgressMessage().
BatchBuilderTest::testSetQueue public function Tests setQueue().
BatchBuilderTest::testSetTitle public function Tests setTitle().
BatchBuilderTest::testSetUrlOptions public function Tests setUrlOptions().
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::setUp protected function 358
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.