State example


class StateTest extends UnitTestCase {

  /** * @covers ::__construct * @covers ::id * @covers ::label * @covers ::weight */
  public function testGetters() {
    $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 */

  protected TestLogger $logger;

  /** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();
    $this->fixtures = new RoutingFixtures();
    $this->state = new State(new KeyValueMemoryFactory());
    $this->currentPath = new CurrentPathStack(new RequestStack());
    $this->cache = new MemoryBackend();
    $this->pathProcessor = \Drupal::service('path_processor_manager');
    $this->cacheTagsInvalidator = \Drupal::service('cache_tags.invalidator');
    $this->installEntitySchema('path_alias');

    $this->logger = new TestLogger();
  }

  /** * {@inheritdoc} */
$this->attributeHydrator->addAttribute($country$data, 'countryAttribute');
        }

        return $country;
    }

    /** * @return State */
    public function hydrateState(array $data)
    {
        $state = new State();

        $id = (int) $data['__countryState_id'];

        $translation = $this->getTranslation($data, '__countryState', []$id);
        $data = array_merge($data$translation);

        $state->setId($id);

        if (isset($data['__countryState_name'])) {
            $state->setName($data['__countryState_name']);
        }

        

  protected $state;

  /** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();

    // Construct a state object used for testing logger assertions.     $this->state = new State(new KeyValueMemoryFactory());

    // Create a mock logger to set a flag in the resulting state.     $logger = $this->prophesize('Drupal\Core\Logger\LoggerChannelInterface');
    // Safely ignore the cron success message.     $logger->info('Cron run completed.')->shouldBeCalled();
    // Set a flag to track when a message is logged by adding a callback     // function for each logging method.     foreach (get_class_methods(LoggerInterface::class) as $logger_method) {
      $logger->{$logger_method}(Argument::cetera())->will(function D) {
        \Drupal::state()->set('cron_test.message_logged', TRUE);
      });
    }
/** * Tests bubbling of bubbleable metadata added by #pre_render callbacks. * * @dataProvider providerTestBubblingWithPrerender */
  public function testBubblingWithPrerender($test_element) {
    $this->setUpRequest();
    $this->setupMemoryCache();

    // Mock the State service.     $memory_state = new State(new KeyValueMemoryFactory());
    \Drupal::getContainer()->set('state', $memory_state);
    $this->controllerResolver->expects($this->any())
      ->method('getControllerFromDefinition')
      ->willReturnArgument(0);

    // Simulate the theme system/Twig: a recursive call to Renderer::render(),     // just like the theme system or a Twig template would have done.     $this->themeManager->expects($this->any())
      ->method('render')
      ->willReturnCallback(function D$hook$vars) {
        return $this->renderer->render($vars['foo']);
      });

class PredefinedStatesWorkflowTestType extends WorkflowTypeBase {

  /** * {@inheritdoc} */
  public function getStates($state_ids = NULL) {
    return array_filter([
      'pay_blinds' => new State($this, 'pay_blinds', 'Pay Blinds'),
      'bet' => new State($this, 'bet', 'Bet'),
      'raise' => new State($this, 'raise', 'Raise'),
      'fold' => new State($this, 'fold', 'Fold'),
    ]function D$state) use ($state_ids) {
        return is_array($state_ids) ? in_array($state->id()$state_ids) : TRUE;
    });
  }

  /** * {@inheritdoc} */
  
$states = array_combine($state_idsarray_map([$this, 'getState']$state_ids));
    return static::labelWeightMultisort($states);
  }

  /** * {@inheritdoc} */
  public function getState($state_id) {
    if (!isset($this->configuration['states'][$state_id])) {
      throw new \InvalidArgumentException("The state '$state_id' does not exist in workflow.");
    }
    return new State(
      $this,
      $state_id,
      $this->configuration['states'][$state_id]['label'],
      $this->configuration['states'][$state_id]['weight']
    );
  }

  /** * {@inheritdoc} */
  public function setStateLabel($state_id$label) {
    
return $info_parser->parse($root . '/' . $file[0]);
      });

    $module_handler = $this->prophesize(ModuleHandlerInterface::class);
    $module_handler
      ->buildModuleDependencies(Argument::type('array'))
      ->willReturnArgument(0);
    $module_handler
      ->alter('system_info', Argument::type('array'), Argument::type(Extension::class), Argument::any())
      ->shouldBeCalled();

    $state = new State(new KeyValueMemoryFactory());

    $config_factory = $this->getConfigFactoryStub([
      'core.extension' => [
        'module' => [],
        'theme' => [],
        'disabled' => [
          'theme' => [],
        ],
        'theme_engine' => '',
      ],
    ]);

    

  protected TestLogger $logger;

  /** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();

    $this->fixtures = new RoutingFixtures();
    $this->state = new State(new KeyValueMemoryFactory());
    $this->logger = new TestLogger();
  }

  /** * Confirms that the dumper can be instantiated successfully. */
  public function testCreate() {
    $connection = Database::getConnection();
    $dumper = new MatcherDumper($connection$this->state, $this->logger);

    $class_name = 'Drupal\Core\Routing\MatcherDumper';
    
Home | Imprint | This part of the site doesn't use cookies.