You are here

class ProviderManagerTest in Video Embed Field 8

Same name and namespace in other branches
  1. 8.2 tests/src/Unit/ProviderManagerTest.php \Drupal\Tests\video_embed_field\Unit\ProviderManagerTest

Tests the provider manager is working.

@group video_embed_field

Hierarchy

Expanded class hierarchy of ProviderManagerTest

File

tests/src/Unit/ProviderManagerTest.php, line 12

Namespace

Drupal\Tests\video_embed_field\Unit
View source
class ProviderManagerTest extends UnitTestCase {

  /**
   * Mock providers to use for the test.
   *
   * @var array
   */
  protected $mockProviders = [
    'provider_a' => [
      'id' => 'provider_a',
      'title' => 'Provider A',
    ],
    'provider_b' => [
      'id' => 'provider_b',
      'title' => 'Provider B',
    ],
    'provider_c' => [
      'id' => 'provider_c',
      'title' => 'Provider C',
    ],
  ];

  /**
   * Test URL parsing works as expected.
   */
  public function testOptionsList() {
    $options = $this
      ->getManagerMock()
      ->getProvidersOptionList();
    $this
      ->assertEquals($options, [
      'provider_a' => 'Provider A',
      'provider_b' => 'Provider B',
      'provider_c' => 'Provider C',
    ]);
  }

  /**
   * Test filtering the definition list from user input via checkboxes.
   *
   * @dataProvider optionsWithExpectedProviders
   */
  public function testDefinitionListFromOptionsList($user_input, $expected_providers) {
    $this
      ->assertEquals($expected_providers, $this
      ->getManagerMock()
      ->loadDefinitionsFromOptionList($user_input));
  }

  /**
   * A data provider for user input with expected filtered providers.
   *
   * @return array
   *   An array of test cases.
   */
  public function optionsWithExpectedProviders() {
    return [
      'Empty input: all providers' => [
        [],
        $this->mockProviders,
      ],
      'Empty checkbox input: all providers' => [
        [
          'provider_a' => '0',
          'provider_b' => '0',
          'provider_c' => '0',
        ],
        $this->mockProviders,
      ],
      'Some providers' => [
        [
          'provider_a' => '0',
          'provider_b' => 'provider_b',
          'provider_c' => 'provider_c',
        ],
        [
          'provider_b' => $this->mockProviders['provider_b'],
          'provider_c' => $this->mockProviders['provider_c'],
        ],
      ],
      'One provider' => [
        [
          'provider_a' => 'provider_a',
          'provider_b' => '0',
          'provider_c' => '0',
        ],
        [
          'provider_a' => $this->mockProviders['provider_a'],
        ],
      ],
    ];
  }

  /**
   * Get a mock provider manager.
   */
  protected function getManagerMock() {
    $definitions = $this->mockProviders;
    $manager = $this
      ->getMockBuilder('Drupal\\video_embed_field\\ProviderManager')
      ->disableOriginalConstructor()
      ->setMethods([
      'getDefinitions',
      'getDefinition',
      'createInstance',
    ])
      ->getMock();
    $manager
      ->method('getDefinitions')
      ->willReturn($definitions);
    $manager
      ->method('getDefinition')
      ->willReturnCallback(function ($value) use ($definitions) {
      return $definitions[$value];
    });
    $manager
      ->method('createInstance')
      ->willReturnCallback(function ($name) {
      return (object) [
        'id' => $name,
      ];
    });
    return $manager;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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.
ProviderManagerTest::$mockProviders protected property Mock providers to use for the test.
ProviderManagerTest::getManagerMock protected function Get a mock provider manager.
ProviderManagerTest::optionsWithExpectedProviders public function A data provider for user input with expected filtered providers.
ProviderManagerTest::testDefinitionListFromOptionsList public function Test filtering the definition list from user input via checkboxes.
ProviderManagerTest::testOptionsList public function Test URL parsing works as expected.
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.
UnitTestCase::setUp protected function 340