ClassLoader example

public function testFindFile() {
    $finder = new ClassFinder();

    // The full path is returned therefore only tests with     // assertStringEndsWith() so the test is portable.     $expected_path = str_replace('/', DIRECTORY_SEPARATOR, 'core/tests/Drupal/Tests/Component/ClassFinder/ClassFinderTest.php');
    $this->assertStringEndsWith($expected_path$finder->findFile(ClassFinderTest::class));
    $class = 'Not\\A\\Class';
    $this->assertNull($finder->findFile($class));

    // Register an autoloader that can find this class.     $loader = new ClassLoader();
    $loader->addClassMap([$class => __FILE__]);
    $loader->register();
    $this->assertEquals(__FILE__, $finder->findFile($class));
    // This shouldn't prevent us from finding the original file.     $this->assertStringEndsWith($expected_path$finder->findFile(ClassFinderTest::class));

    // Clean up the additional autoloader after the test.     $loader->unregister();
  }

}
'\Drupal\search\ViewsSearchQuery',
      ],
    ];
  }

  /** * @covers ::extend * @covers \Drupal\Core\Database\Query\SelectExtender::extend * @dataProvider providerExtend */
  public function testExtend(string $expected, string $namespace, string $extend): void {
    $additional_class_loader = new ClassLoader();
    $additional_class_loader->addPsr4("Drupal\\corefake\\Driver\\Database\\corefake\\", __DIR__ . "/../../../../../tests/fixtures/database_drivers/module/corefake/src/Driver/Database/corefake");
    $additional_class_loader->addPsr4("Drupal\\corefake\\Driver\\Database\\corefakeWithAllCustomClasses\\", __DIR__ . "/../../../../../tests/fixtures/database_drivers/module/corefake/src/Driver/Database/corefakeWithAllCustomClasses");
    $additional_class_loader->register(TRUE);

    $mock_pdo = $this->createMock(StubPDO::class);
    $connection = new StubConnection($mock_pdo['namespace' => $namespace]);

    // Tests the method \Drupal\Core\Database\Query\Select::extend().     $select = $connection->select('test')->extend($extend);
    $this->assertEquals($expectedget_class($select));

    

  protected $root;

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

    $this->additionalClassloader = new ClassLoader();
    $this->additionalClassloader->register();
    // Mock the container so we don't need to mock drupal_valid_test_ua().     // @see \Drupal\Core\Extension\ExtensionDiscovery::scan()     $this->root = dirname(__DIR__, 6);
    $container = $this->createMock(ContainerInterface::class);
    $container->expects($this->any())
      ->method('has')
      ->with('kernel')
      ->willReturn(TRUE);
    $container->expects($this->any())
      ->method('getParameter')
      
    $this->containerNeedsRebuild = TRUE;
    if ($this->booted) {
      // We need to register any new namespaces to a new class loader because       // the current class loader might have stored a negative result for a       // class that is now available.       // @see \Composer\Autoload\ClassLoader::findFile()       $new_namespaces = array_diff_key(
        $this->getModuleNamespacesPsr4($this->getModuleFileNames()),
        $pre_existing_module_namespaces
      );
      if (!empty($new_namespaces)) {
        $additional_class_loader = new ClassLoader();
        $this->classLoaderAddMultiplePsr4($new_namespaces$additional_class_loader);
        $additional_class_loader->register();
      }

      $this->initializeContainer();
    }
  }

  /** * Generate a unique hash for a service object. * * @param object $object * A service object. * * @return string * A unique hash value. * * @deprecated in drupal:9.5.1 and is removed from drupal:11.0.0. Use the * 'Drupal\Component\DependencyInjection\ReverseContainer' service instead. * * @see https://www.drupal.org/node/3327942 */
    if (!isset($query['module']) && in_array($driver['mysql', 'pgsql', 'sqlite'], TRUE)) {
      $query['module'] = $driver;
    }

    if (isset($query['module']) && $query['module']) {
      $module = $query['module'];
      // Set up an additional autoloader. We don't use the main autoloader as       // this method can be called before Drupal is installed and is never       // called during regular runtime.       $namespace = "Drupal\\$module\\Driver\\Database\\$driver";
      $psr4_base_directory = Database::findDriverAutoloadDirectory($namespace$root$include_test_drivers);
      $additional_class_loader = new ClassLoader();
      $additional_class_loader->addPsr4($namespace . '\\', $psr4_base_directory);
      $additional_class_loader->register(TRUE);
      $connection_class = $namespace . '\\Connection';
    }

    if (!$module) {
      // Determine the connection class to use. Discover if the URL has a valid       // driver scheme for a Drupal 8 style custom driver.       // @todo Remove this in Drupal 10.       $connection_class = "Drupal\\Driver\\Database\\{$driver}\\Connection";
    }

    

  public function testDiscoverServiceCustom() {
    new Settings([
      'container_yamls' => [
        __DIR__ . '/fixtures/custom.yml',
      ],
    ]);

    $kernel = new DrupalKernel('prod', new ClassLoader());
    $kernel->discoverServiceProviders();

    $reflected_yamls = (new \ReflectionObject($kernel))->getProperty('serviceYamls');

    $expect = [
      'app' => [
        'core' => 'core/core.services.yml',
      ],
      'site' => [
        __DIR__ . '/fixtures/custom.yml',
      ],
    ];
'Drupal\corefake\Driver\Database\corefakeWithAllCustomClasses',
        '\Drupal\search\SearchQuery',
      ],
    ];
  }

  /** * @covers ::getDriverClass * @dataProvider providerGetDriverClass */
  public function testGetDriverClass($expected$namespace$class) {
    $additional_class_loader = new ClassLoader();
    $additional_class_loader->addPsr4("Drupal\\corefake\\Driver\\Database\\corefake\\", __DIR__ . "/../../../../../tests/fixtures/database_drivers/module/corefake/src/Driver/Database/corefake");
    $additional_class_loader->addPsr4("Drupal\\corefake\\Driver\\Database\\corefakeWithAllCustomClasses\\", __DIR__ . "/../../../../../tests/fixtures/database_drivers/module/corefake/src/Driver/Database/corefakeWithAllCustomClasses");
    $additional_class_loader->register(TRUE);

    $mock_pdo = $this->createMock('Drupal\Tests\Core\Database\Stub\StubPDO');
    $connection = new StubConnection($mock_pdo['namespace' => $namespace]);
    $this->assertEquals($expected$connection->getDriverClass($class));
  }

  /** * Data provider for testSchema(). * * @return array * Array of arrays with the following elements: * - Expected namespaced class of schema object. * - Driver for PDO connection. * - Namespace for connection. */
 '/' . preg_quote('.html.twig') . '$/'
    ));

    foreach ($iterator as $template_file => $contents) {
      $new_template_content = preg_replace("/(attach_library\(['\")])$source_theme_name(\/.*['\"]\))/", "$1$destination_theme$2", $contents);
      if (!file_put_contents($template_file$new_template_content)) {
        $io->getErrorStyle()->error("The template file $template_file could not be written.");
        return 1;
      }
    }

    $loader = new ClassLoader();
    $loader->addPsr4("Drupal\\$source_theme_name\\", "$source/src");
    $loader->register();

    $generator_classname = "Drupal\\$source_theme_name\\StarterKit";
    if (class_exists($generator_classname)) {
      if (is_a($generator_classname, StarterKitInterface::class, TRUE)) {
        $generator_classname::postProcess($tmp_dir$destination_theme$info['name']);
      }
      else {
        $io->getErrorStyle()->error("The $generator_classname does not implement \Drupal\Core\Theme\StarterKitInterface and cannot perform post-processing.");
        return 1;
      }
$module_name.info.yml" => <<<YAML name: CKEditor 5 Test $module_name type: module core_version_requirement: ^9 YAML,
          "$module_name.ckeditor5.yml" => $yaml,
        ] + $additional_files,
      ],
    ]$this->vfsRoot->getChild($site_directory));

    if (!empty($additional_files)) {
      $additional_class_loader = new ClassLoader();
      $additional_class_loader->addPsr4("Drupal\\$module_name\\Plugin\\CKEditor5Plugin\\", vfsStream::url("root/$site_directory/modules/$module_name/src/Plugin/CKEditor5Plugin"));
      $additional_class_loader->register(TRUE);
    }

    $config_sync = \Drupal::service('config.storage');
    $config_data = $this->config('core.extension')->get();
    $config_data['module'][$module_name] = 1;
    $config_sync->write('core.extension', $config_data);

    // Construct a new container for testing a plugin definition in isolation,     // without needing a separate module directory structure for it, and instead
class DebugClassLoaderTest extends TestCase
{
    private string|false $patchTypes;
    private int $errorReporting;
    private array $loader;

    protected function setUp(): void
    {
        $this->patchTypes = getenv('SYMFONY_PATCH_TYPE_DECLARATIONS');
        $this->errorReporting = error_reporting(E_ALL);
        putenv('SYMFONY_PATCH_TYPE_DECLARATIONS=deprecations=1');
        $this->loader = [new DebugClassLoader([new ClassLoader(), 'loadClass']), 'loadClass'];
        spl_autoload_register($this->loader, true, true);
    }

    protected function tearDown(): void
    {
        spl_autoload_unregister($this->loader);
        error_reporting($this->errorReporting);
        putenv('SYMFONY_PATCH_TYPE_DECLARATIONS'.(false !== $this->patchTypes ? '='.$this->patchTypes : ''));
    }

    /** * @runInSeparateProcess */
/** * @return \Composer\Autoload\ClassLoader */
    public static function getLoader()
    {
        if (null !== self::$loader) {
            return self::$loader;
        }

        spl_autoload_register(array('ComposerAutoloaderInit2421ddaf7b9befaa866c073c7c56b72b', 'loadClassLoader'), true, true);
        self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
        spl_autoload_unregister(array('ComposerAutoloaderInit2421ddaf7b9befaa866c073c7c56b72b', 'loadClassLoader'));

        $includePaths = require __DIR__ . '/include_paths.php';
        $includePaths[] = get_include_path();
        set_include_path(implode(PATH_SEPARATOR, $includePaths));

        require __DIR__ . '/autoload_static.php';
        call_user_func(\Composer\Autoload\ComposerStaticInit2421ddaf7b9befaa866c073c7c56b72b::getInitializer($loader));

        $loader->register(true);

        
 $result);
  }

  /** * @covers ::getTestClasses */
  public function testGetTestsInProfiles() {
    $this->setupVfsWithTestClasses();
    $class_loader = $this->prophesize(ClassLoader::class);

    $container = new Container();
    $container->set('kernel', new DrupalKernel('prod', new ClassLoader()));
    $container->setParameter('site.path', 'sites/default');
    \Drupal::setContainer($container);

    $test_discovery = new TestDiscovery('vfs://drupal', $class_loader->reveal());

    $result = $test_discovery->getTestClasses('test_profile_module', ['PHPUnit-Kernel']);
    $expected = [
      'example3' => [
        'Drupal\Tests\test_profile_module\Kernel\KernelExampleTest4' => [
          'name' => 'Drupal\Tests\test_profile_module\Kernel\KernelExampleTest4',
          'description' => 'Test description',
          

class InstallerObjectTest extends UnitTestCase {

  /** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();
    require_once __DIR__ . '/../../../../../includes/install.inc';
    $additional_class_loader = new ClassLoader();
    $additional_class_loader->addPsr4("Drupal\\Driver\\Database\\fake\\", __DIR__ . "/../../../../../tests/fixtures/database_drivers/custom/fake");
    $additional_class_loader->addPsr4("Drupal\\Core\\Database\\Driver\\corefake\\", __DIR__ . "/../../../../../tests/fixtures/database_drivers/core/corefake");
    $additional_class_loader->addPsr4("Drupal\\Driver\\Database\\corefake\\", __DIR__ . "/../../../../../tests/fixtures/database_drivers/custom/corefake");
    $additional_class_loader->addPsr4("Drupal\\driver_test\\Driver\\Database\\DrivertestMysql\\", __DIR__ . "/../../../../../../modules/system/tests/modules/driver_test/src/Driver/Database/DrivertestMysql");
    $additional_class_loader->register(TRUE);
  }

  /** * @dataProvider providerDbInstallerObject */
  public function testDbInstallerObject($driver$namespace$expected_class_name) {
    
Home | Imprint | This part of the site doesn't use cookies.