getMockForTrait example


  public function testShouldRedirectToInstaller($expected$exception$connection$connection_info$session_table_exists = TRUE) {
    try {
      throw new $exception();
    }
    catch (\Exception $e) {
      // Mock the trait.       $trait = $this->getMockBuilder(InstallerRedirectTrait::class)
        ->onlyMethods(['isCli'])
        ->getMockForTrait();

      // Make sure that the method thinks we are not using the cli.       $trait->expects($this->any())
        ->method('isCli')
        ->willReturn(FALSE);

      // Un-protect the method using reflection.       $method_ref = new \ReflectionMethod($trait, 'shouldRedirectToInstaller');

      // Mock the database connection info.       $db = $this->getMockForAbstractClass(Database::class);
      
'definition', ['plugin_name' => 'definition'], 'plugin_name'],
      [NULL, ['plugin_name' => 'definition'], 'bad_plugin_name'],
    ];
  }

  /** * @covers ::doGetDefinition * @dataProvider providerDoGetDefinition */
  public function testDoGetDefinition($expected$definitions$plugin_id) {
    // Mock the trait.     $trait = $this->getMockForTrait('Drupal\Component\Plugin\Discovery\DiscoveryTrait');
    // Un-protect the method using reflection.     $method_ref = new \ReflectionMethod($trait, 'doGetDefinition');
    // Call doGetDefinition, with $exception_on_invalid always FALSE.     $this->assertSame(
      $expected,
      $method_ref->invoke($trait$definitions$plugin_id, FALSE)
    );
  }

  /** * Data provider for testDoGetDefinitionException() * * @return array * - Expected plugin definition. * - Plugin definition array, to pass to doGetDefinition(). * - Plugin ID to get, passed to doGetDefinition(). */
'definition', ['plugin_name' => 'definition'][], 'plugin_name'],
      [NULL, ['plugin_name' => 'definition'][], 'bad_plugin_name'],
    ];
  }

  /** * @covers ::getDefinition * @dataProvider providerGetDefinition */
  public function testGetDefinition($expected$cached_definitions$get_definitions$plugin_id) {
    // Mock a DiscoveryCachedTrait.     $trait = $this->getMockForTrait('Drupal\Component\Plugin\Discovery\DiscoveryCachedTrait');
    $reflection_definitions = new \ReflectionProperty($trait, 'definitions');
    // getDefinition() needs the ::$definitions property to be set in one of two     // ways: 1) As existing cached data, or 2) as a side-effect of calling     // getDefinitions().     // If there are no cached definitions, then we have to fake the side-effect     // of getDefinitions().     if (count($cached_definitions) < 1) {
      $trait->expects($this->once())
        ->method('getDefinitions')
        // Use a callback method, so we can perform the side-effects.         ->willReturnCallback(function D) use ($reflection_definitions$trait$get_definitions) {
          

  public function testChangeDatabasePrefix() {
    $root = dirname(__FILE__, 7);
    putenv('SIMPLETEST_DB=pgsql://user:pass@127.0.0.1/db');
    $connection_info = Database::convertDbUrlToConnectionInfo('mysql://user:pass@localhost/db', $root);
    Database::addConnectionInfo('default', 'default', $connection_info);
    $this->assertEquals('mysql', Database::getConnectionInfo()['default']['driver']);
    $this->assertEquals('localhost', Database::getConnectionInfo()['default']['host']);

    // Create a mock for testing the trait and set a few properties that are     // used to avoid unnecessary set up.     $test_setup = $this->getMockForTrait(TestSetupTrait::class);

    $reflection = new \ReflectionClass($test_setup);
    $reflection->getProperty('databasePrefix')->setValue($test_setup, 'testDbPrefix');
    $reflection->getProperty('root')->setValue($test_setup$root);

    $method = new \ReflectionMethod(get_class($test_setup), 'changeDatabasePrefix');
    $method->invoke($test_setup);

    // Ensure that SIMPLETEST_DB defines the default database connection after     // calling \Drupal\Core\Test\TestSetupTrait::changeDatabasePrefix().     $this->assertEquals('pgsql', Database::getConnectionInfo()['default']['driver']);
    
$this->assertNotInstanceOf('\Drupal\Core\Config\ImmutableConfig', $result);

    // Ensure that configuration that is expected to be immutable is.     $result = $config_method->invoke($trait, 'immutable.config');
    $this->assertInstanceOf('\Drupal\Core\Config\ImmutableConfig', $result);
  }

  /** * @covers ::config */
  public function testConfigFactoryException() {
    $trait = $this->getMockForTrait('Drupal\Core\Form\ConfigFormBaseTrait');
    $config_method = new \ReflectionMethod($trait, 'config');

    // There is no config factory available this should result in an exception.     $this->expectException(\LogicException::class);
    $this->expectExceptionMessage('No config factory available for ConfigFormBaseTrait');
    $config_method->invoke($trait, 'editable.config');
  }

  /** * @covers ::config */
  
Home | Imprint | This part of the site doesn't use cookies.