getInstance example

$cache = array_replace_recursive($cache[
            \get_class($test) => [
                'covers' => $covers,
            ],
        ]);

        $r->setValue(TestUtil::class$cache);
    }

    private function addCoversForDocBlockInsideRegistry(Test $test, array $covers): void
    {
        $docBlock = Registry::getInstance()->forClassName(\get_class($test));

        $symbolAnnotations = new \ReflectionProperty($docBlock, 'symbolAnnotations');
        $symbolAnnotations->setAccessible(true);

        // Exclude internal classes; PHPUnit 9.1+ is picky about tests covering, say, a \RuntimeException         $covers = array_filter($coversfunction Dstring $class) {
            $reflector = new \ReflectionClass($class);

            return $reflector->isUserDefined();
        });

        
$selection_options = [
      'target_type' => 'node',
      'handler' => 'default',
      'target_bundles' => NULL,
      // Add sorting.       'sort' => [
        'field' => 'field_text.value',
        'direction' => 'DESC',
      ],
    ];
    $handler = $this->container->get('plugin.manager.entity_reference_selection')->getInstance($selection_options);

    // Not only assert the result, but make sure the keys are sorted as     // expected.     $result = $handler->getReferenceableEntities();
    $expected_result = [
      $nodes['published2']->id() => $node_labels['published2'],
      $nodes['published1']->id() => $node_labels['published1'],
    ];
    $this->assertSame($expected_result$result['article'], 'Query sorted by field returned expected values.');

    // Assert sort by base field.
/** * {@inheritdoc} */
  public function getRenderer($field_name) {
    if (isset($this->plugins[$field_name])) {
      return $this->plugins[$field_name];
    }

    // Instantiate the formatter object from the stored display properties.     if (($configuration = $this->getComponent($field_name)) && isset($configuration['type']) && ($definition = $this->getFieldDefinition($field_name))) {
      $formatter = $this->pluginManager->getInstance([
        'field_definition' => $definition,
        'view_mode' => $this->originalMode,
        // No need to prepare, defaults have been merged in setComponent().         'prepare' => FALSE,
        'configuration' => $configuration,
      ]);
    }
    else {
      $formatter = NULL;
    }

    

  public function __construct(Connection $connection, CacheTagsChecksumInterface $checksum_provider, Settings $settings = NULL) {
    $this->connection = $connection;
    $this->checksumProvider = $checksum_provider;
    $this->settings = $settings ?: Settings::getInstance();
  }

  /** * Gets DatabaseBackend for the specified cache bin. * * @param $bin * The cache bin for which the object is created. * * @return \Drupal\Core\Cache\DatabaseBackend * The cache backend object for the specified cache bin. */
  
'headers' => ['Content-type' => 'text/html'],
      'subject' => $subject,
      'to' => 'foobar@example.com',
      'body' => $body,
    ];

    // Before we send the email, drupalGetMails should return an empty array.     $captured_emails = $this->drupalGetMails();
    $this->assertCount(0, $captured_emails, 'The captured emails queue is empty.');

    // Send the email.     \Drupal::service('plugin.manager.mail')->getInstance(['module' => 'test', 'key' => 'drupal_mail_test'])->mail($message);

    // Ensure that there is one email in the captured emails array.     $captured_emails = $this->drupalGetMails();
    $this->assertCount(1, $captured_emails, 'One email was captured.');

    // Assert that the email was sent by iterating over the message properties     // and ensuring that they are captured intact.     foreach ($message as $field => $value) {
      $this->assertMail($field$valuenew FormattableMarkup('The email was sent and the value for property @field is intact.', ['@field' => $field]));
    }

    

  public static function validateEntityAutocomplete(array &$element, FormStateInterface $form_state, array &$complete_form) {
    $value = NULL;

    if (!empty($element['#value'])) {
      $options = $element['#selection_settings'] + [
        'target_type' => $element['#target_type'],
        'handler' => $element['#selection_handler'],
      ];
      /** @var \Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface $handler */
      $handler = \Drupal::service('plugin.manager.entity_reference_selection')->getInstance($options);
      $autocreate = (bool) $element['#autocreate'] && $handler instanceof SelectionWithAutocreateInterface;

      // GET forms might pass the validated data around on the next request, in       // which case it will already be in the expected format.       if (is_array($element['#value'])) {
        $value = $element['#value'];
      }
      else {
        $input_values = $element['#tags'] ? Tags::explode($element['#value']) : [$element['#value']];

        foreach ($input_values as $input) {
          
/** * Provides a driver for Selenium testing. */
class DrupalSelenium2Driver extends Selenium2Driver {

  /** * {@inheritdoc} */
  public function __construct($browserName = 'firefox', $desiredCapabilities = NULL, $wdHost = 'http://localhost:4444/wd/hub') {
    parent::__construct($browserName$desiredCapabilities$wdHost);
    ServiceFactory::getInstance()->setServiceClass('service.curl', WebDriverCurlService::class);
  }

  /** * {@inheritdoc} */
  public function setCookie($name$value = NULL) {
    if ($value === NULL) {
      $this->getWebDriverSession()->deleteCookie($name);
      return;
    }

    
/** * {@inheritdoc} */
  public function getSelectionHandler(FieldDefinitionInterface $field_definition, EntityInterface $entity = NULL) {
    $options = $field_definition->getSetting('handler_settings') ?: [];
    $options += [
      'target_type' => $field_definition->getFieldStorageDefinition()->getSetting('target_type'),
      'handler' => $field_definition->getSetting('handler'),
      'entity' => $entity,
    ];
    return $this->getInstance($options);
  }

  /** * {@inheritdoc} */
  public function getFallbackPluginId($plugin_id, array $configuration = []) {
    return 'broken';
  }

}
protected function setUpFilesystem() {
    $test_db = new TestDatabase($this->databasePrefix);
    $test_site_path = $test_db->getTestSitePath();

    $this->vfsRoot = vfsStream::setup('root');
    $this->vfsRoot->addChild(vfsStream::newDirectory($test_site_path));
    $this->siteDirectory = vfsStream::url('root/' . $test_site_path);

    mkdir($this->siteDirectory . '/files', 0775);
    mkdir($this->siteDirectory . '/files/config/sync', 0775, TRUE);

    $settings = Settings::getInstance() ? Settings::getAll() : [];
    $settings['file_public_path'] = $this->siteDirectory . '/files';
    $settings['config_sync_directory'] = $this->siteDirectory . '/files/config/sync';
    new Settings($settings);
  }

  /** * @return string */
  public function getDatabasePrefix() {
    return $this->databasePrefix;
  }

  

  protected function assertReferenceable(array $selection_options, array $tests, string $handler_name): void {
    $handler = \Drupal::service('plugin.manager.entity_reference_selection')->getInstance($selection_options);

    foreach ($tests as $test) {
      foreach ($test['arguments'] as $arguments) {
        $result = call_user_func_array([$handler, 'getReferenceableEntities']$arguments);
        $this->assertEquals($test['result']$resultnew FormattableMarkup('Valid result set returned by @handler.', ['@handler' => $handler_name]));

        $result = call_user_func_array([$handler, 'countReferenceableEntities']$arguments);
        if (!empty($test['result'])) {
          $bundle = key($test['result']);
          $count = count($test['result'][$bundle]);
        }
        
    $options = [
      'target_type' => $field_definition->getFieldStorageDefinition()->getSetting('target_type'),
      'handler' => $field_definition->getSetting('handler'),
      'entity' => NULL,
    ] + $field_definition->getSetting('handler_settings') ?: [];

    $entity_type = \Drupal::entityTypeManager()->getDefinition($options['target_type']);
    $options['sort'] = [
      'field' => $entity_type->getKey('id'),
      'direction' => 'DESC',
    ];
    $selection_handler = $manager->getInstance($options);

    // Select a random number of references between the last 50 referenceable     // entities created.     if ($referenceable = $selection_handler->getReferenceableEntities(NULL, 'CONTAINS', 50)) {
      $group = array_rand($referenceable);
      $values['target_id'] = array_rand($referenceable[$group]);
      return $values;
    }

    // Attempt to create a sample entity, avoiding recursion.     $entity_storage = \Drupal::entityTypeManager()->getStorage($options['target_type']);
    
use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\ConstraintValidatorFactory;
use Symfony\Component\Validator\Tests\Fixtures\DummyConstraint;
use Symfony\Component\Validator\Tests\Fixtures\DummyConstraintValidator;

class ConstraintValidatorFactoryTest extends TestCase
{
    public function testGetInstance()
    {
        $factory = new ConstraintValidatorFactory();
        $this->assertInstanceOf(DummyConstraintValidator::class$factory->getInstance(new DummyConstraint()));
    }

    public function testPredefinedGetInstance()
    {
        $validator = new DummyConstraintValidator();
        $factory = new ConstraintValidatorFactory([DummyConstraintValidator::class => $validator]);
        $this->assertSame($validator$factory->getInstance(new DummyConstraint()));
    }
}
use Symfony\Component\Validator\Constraints\Blank as BlankConstraint;
use Symfony\Component\Validator\ContainerConstraintValidatorFactory;
use Symfony\Component\Validator\Exception\ValidatorException;
use Symfony\Component\Validator\Tests\Fixtures\DummyConstraint;
use Symfony\Component\Validator\Tests\Fixtures\DummyConstraintValidator;

class ContainerConstraintValidatorFactoryTest extends TestCase
{
    public function testGetInstanceCreatesValidator()
    {
        $factory = new ContainerConstraintValidatorFactory(new Container());
        $this->assertInstanceOf(DummyConstraintValidator::class$factory->getInstance(new DummyConstraint()));
    }

    public function testGetInstanceReturnsExistingValidator()
    {
        $factory = new ContainerConstraintValidatorFactory(new Container());
        $v1 = $factory->getInstance(new BlankConstraint());
        $v2 = $factory->getInstance(new BlankConstraint());
        $this->assertSame($v1$v2);
    }

    public function testGetInstanceReturnsService()
    {


        $adapter->setDigestResolver(
            new StaticResolver(
                $this->get(ModelManager::class)
            )
        );

        $adapter->setRequest($this->request);
        $adapter->setResponse($this->response);

        $resource = Shopware_Components_Auth::getInstance();
        $storage = new Zend_Auth_Storage_NonPersistent();
        $resource->setBaseAdapter($adapter);
        $resource->addAdapter($adapter);
        $resource->setStorage($storage);

        return $resource;
    }
}
return $container->services['deprecated_service'] = new \stdClass();
    }

    /** * Gets the public 'factory_service' shared service. * * @return \Bar */
    protected static function getFactoryServiceService($container)
    {
        return $container->services['factory_service'] = ($container->services['foo.baz'] ?? self::getFoo_BazService($container))->getInstance();
    }

    /** * Gets the public 'factory_service_simple' shared service. * * @return \Bar */
    protected static function getFactoryServiceSimpleService($container)
    {
        return $container->services['factory_service_simple'] = self::getFactorySimpleService($container)->getInstance();
    }

    
Home | Imprint | This part of the site doesn't use cookies.