class BatchBuilderTest

Same name in other branches
  1. 8.9.x core/tests/Drupal/Tests/Core/Batch/BatchBuilderTest.php \Drupal\Tests\Core\Batch\BatchBuilderTest
  2. 10 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 16

Namespace

Drupal\Tests\Core\Batch
View source
class BatchBuilderTest extends UnitTestCase {
    
    /**
     * Tests the default values.
     *
     * @covers ::toArray
     */
    public function testDefaultValues() {
        $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() {
        $batch = (new BatchBuilder())->setTitle(new TranslatableMarkup('New Title'))
            ->toArray();
        $this->assertEquals(new TranslatableMarkup('New Title'), $batch['title']);
    }
    
    /**
     * Tests setFinishCallback().
     *
     * @covers ::setFinishCallback
     */
    public function testSetFinishCallback() {
        $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() {
        $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() {
        $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() {
        $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() {
        $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() {
        $batch = (new BatchBuilder())->setLibraries([
            'only/library',
        ])
            ->toArray();
        $this->assertEquals([
            'only/library',
        ], $batch['library']);
    }
    
    /**
     * Tests setProgressive().
     *
     * @covers ::setProgressive
     */
    public function testSetProgressive() {
        $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() {
        $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() {
        $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() {
        $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() {
        $options = [
            'absolute' => TRUE,
            'language' => 'de',
        ];
        $batch = (new BatchBuilder())->setUrlOptions($options)
            ->toArray();
        $this->assertEquals($options, $batch['url_options']);
    }
    
    /**
     * Tests addOperation().
     *
     * @covers ::addOperation
     */
    public function testAddOperation() {
        $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.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals Deprecated protected function Asserts if two arrays are equal by sorting them first.
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::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUp protected function 338
UnitTestCase::setUpBeforeClass public static function

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