You are here

class FreelinkingManagerTest in Freelinking 8.3

Same name and namespace in other branches
  1. 4.0.x tests/src/Unit/FreelinkingManagerTest.php \Drupal\Tests\freelinking\Unit\FreelinkingManagerTest

Tests the freelinking plugin manager.

@group freelinking

Hierarchy

Expanded class hierarchy of FreelinkingManagerTest

File

tests/src/Unit/FreelinkingManagerTest.php, line 14

Namespace

Drupal\Tests\freelinking\Unit
View source
class FreelinkingManagerTest extends UnitTestCase {

  /**
   * Freelinking Manager object to run tests on.
   *
   * @var \Drupal\freelinking\FreelinkingManagerInterface
   *   The Freelinking Manager.
   */
  protected $pluginManager;

  /**
   * Mock language object.
   *
   * @var \Drupal\Core\Language\LanguageInterface
   *   A language object.
   */
  protected $language;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {

    // Use a null cache backend to prevent caching.
    $cacheBackend = new NullBackend('freelinking');

    // Mock the module handler and language objects.
    $moduleProphet = $this
      ->prophesize('\\Drupal\\Core\\Extension\\ModuleHandlerInterface');
    $moduleHandler = $moduleProphet
      ->reveal();
    $languageProphet = $this
      ->prophesize('\\Drupal\\Core\\Language\\LanguageInterface');
    $this->language = $languageProphet
      ->reveal();
    $languageManagerProphet = $this
      ->prophesize('\\Drupal\\Core\\Language\\LanguageManagerInterface');
    $languageManagerProphet
      ->getLanguage('en')
      ->willReturn($this->language);
    $languageManager = $languageManagerProphet
      ->reveal();
    $namespaces = new \ArrayObject();
    $this->pluginManager = new FreelinkingManager($namespaces, $cacheBackend, $moduleHandler, $languageManager);
  }

  /**
   * Tests parseTarget method.
   *
   * @param array $expected
   *   The expected destination string.
   * @param string $target
   *   The target string.
   *
   * @dataProvider parseTargetProvider
   */
  public function testParseTarget(array $expected, $target) {
    $expected['target'] = $target;
    $expected['language'] = $this->language;
    $this
      ->assertEquals($expected, $this->pluginManager
      ->parseTarget($target, 'en'));
  }

  /**
   * Provide test parameters and expected values for testParseTarget().
   *
   * @return array
   *   An array of test parameters and expected values.
   */
  public function parseTargetProvider() {
    return [
      [
        [
          'dest' => 'nid:2',
          'text' => 'Special title',
          'tooltip' => 'tooltip',
          'other' => [],
        ],
        'nid:2|Special title|tooltip',
      ],
      [
        [
          'dest' => 'nid:2',
          'text' => NULL,
          'tooltip' => NULL,
          'other' => [],
        ],
        'nid:2',
      ],
      [
        [
          'dest' => 'external:http://example.com?id=12345',
          'text' => '1',
          'tooltip' => NULL,
          'other' => [],
        ],
        'external:http://example.com?id=12345|1',
      ],
      [
        [
          'dest' => 'external:http://example.com?id=12345&q=%E2%99%A5#fragment',
          'text' => 'Title',
          'tooltip' => 'Tooltip',
          'arbitrary' => 'value',
          'other' => [
            'Other',
          ],
        ],
        'external:http://example.com?id=12345&q=%E2%99%A5#fragment|Title|Tooltip|arbitrary=value|Other',
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FreelinkingManagerTest::$language protected property Mock language object.
FreelinkingManagerTest::$pluginManager protected property Freelinking Manager object to run tests on.
FreelinkingManagerTest::parseTargetProvider public function Provide test parameters and expected values for testParseTarget().
FreelinkingManagerTest::setUp protected function Overrides UnitTestCase::setUp
FreelinkingManagerTest::testParseTarget public function Tests parseTarget method.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 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::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.