class StateTest

Same name in other branches
  1. 9 core/modules/workflows/tests/src/Unit/StateTest.php \Drupal\Tests\workflows\Unit\StateTest
  2. 8.9.x core/modules/workflows/tests/src/Unit/StateTest.php \Drupal\Tests\workflows\Unit\StateTest
  3. 11.x core/modules/workflows/tests/src/Unit/StateTest.php \Drupal\Tests\workflows\Unit\StateTest
  4. 11.x core/tests/Drupal/Tests/Core/State/StateTest.php \Drupal\Tests\Core\State\StateTest

@coversDefaultClass \Drupal\workflows\State

@group workflows

Hierarchy

Expanded class hierarchy of StateTest

File

core/modules/workflows/tests/src/Unit/StateTest.php, line 17

Namespace

Drupal\Tests\workflows\Unit
View source
class StateTest extends UnitTestCase {
    
    /**
     * @covers ::__construct
     * @covers ::id
     * @covers ::label
     * @covers ::weight
     */
    public function testGetters() : void {
        $state = new State($this->prophesize(WorkflowTypeInterface::class)
            ->reveal(), 'draft', 'Draft', 3);
        $this->assertEquals('draft', $state->id());
        $this->assertEquals('Draft', $state->label());
        $this->assertEquals(3, $state->weight());
    }
    
    /**
     * @covers ::canTransitionTo
     */
    public function testCanTransitionTo() : void {
        $workflow_type = new TestType([], '', []);
        $workflow_type->addState('draft', 'Draft')
            ->addState('published', 'Published')
            ->addTransition('publish', 'Publish', [
            'draft',
        ], 'published');
        $state = $workflow_type->getState('draft');
        $this->assertTrue($state->canTransitionTo('published'));
        $this->assertFalse($state->canTransitionTo('some_other_state'));
        $workflow_type->deleteTransition('publish');
        $this->assertFalse($state->canTransitionTo('published'));
    }
    
    /**
     * @covers ::getTransitionTo
     */
    public function testGetTransitionTo() : void {
        $workflow_type = new TestType([], '', []);
        $workflow_type->addState('draft', 'Draft')
            ->addState('published', 'Published')
            ->addTransition('publish', 'Publish', [
            'draft',
        ], 'published');
        $state = $workflow_type->getState('draft');
        $transition = $state->getTransitionTo('published');
        $this->assertEquals('Publish', $transition->label());
    }
    
    /**
     * @covers ::getTransitionTo
     */
    public function testGetTransitionToException() : void {
        $this->expectException(\InvalidArgumentException::class);
        $this->expectExceptionMessage("Can not transition to 'published' state");
        $workflow_type = new TestType([], '', []);
        $workflow_type->addState('draft', 'Draft');
        $state = $workflow_type->getState('draft');
        $state->getTransitionTo('published');
    }
    
    /**
     * @covers ::getTransitions
     */
    public function testGetTransitions() : void {
        $workflow_type = new TestType([], '', []);
        $workflow_type->addState('draft', 'Draft')
            ->addState('published', 'Published')
            ->addState('archived', 'Archived')
            ->addTransition('create_new_draft', 'Create new draft', [
            'draft',
        ], 'draft')
            ->addTransition('publish', 'Publish', [
            'draft',
        ], 'published')
            ->addTransition('archive', 'Archive', [
            'published',
        ], 'archived');
        $state = $workflow_type->getState('draft');
        $transitions = $state->getTransitions();
        $this->assertCount(2, $transitions);
        $this->assertEquals('Create new draft', $transitions['create_new_draft']->label());
        $this->assertEquals('Publish', $transitions['publish']->label());
    }
    
    /**
     * @covers ::labelCallback
     */
    public function testLabelCallback() : void {
        $workflow_type = $this->prophesize(WorkflowTypeInterface::class)
            ->reveal();
        $states = [
            new State($workflow_type, 'draft', 'Draft'),
            new State($workflow_type, 'published', 'Published'),
        ];
        $this->assertEquals([
            'Draft',
            'Published',
        ], array_map([
            State::class,
            'labelCallback',
        ], $states));
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overrides
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.
StateTest::testCanTransitionTo public function @covers ::canTransitionTo
StateTest::testGetters public function @covers ::__construct
@covers ::id
@covers ::label
@covers ::weight
StateTest::testGetTransitions public function @covers ::getTransitions
StateTest::testGetTransitionTo public function @covers ::getTransitionTo
StateTest::testGetTransitionToException public function @covers ::getTransitionTo
StateTest::testLabelCallback public function @covers ::labelCallback
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.