You are here

class PatternMatchingTest in Config Ignore 8.2

Test the pattern matching.

This is a unit test which tests the protected method, but it is much faster.

@group config_ignore_new

Hierarchy

Expanded class hierarchy of PatternMatchingTest

File

tests/src/Unit/PatternMatchingTest.php, line 16

Namespace

Drupal\Tests\config_ignore\Unit
View source
class PatternMatchingTest extends UnitTestCase {

  /**
   * Test the ignored config matching against names.
   *
   * @param array $patterns
   *   The config of ignored names.
   * @param array $test
   *   An array with config names as keys and the expected outcome as value.
   *
   * @dataProvider patternProvider
   */
  public function testPatternMatching(array $patterns, array $test) {
    $filter = new IgnoreFilter([
      'ignored' => $patterns,
    ], 'config_ignore', [], new MemoryStorage());

    // In order to test much faster we access the protected method.
    $method = new \ReflectionMethod(IgnoreFilter::class, 'matchConfigName');
    $method
      ->setAccessible(TRUE);
    foreach ($test as $name => $expected) {
      static::assertEquals($expected, $method
        ->invoke($filter, $name), $name);
    }
    if (!in_array(TRUE, $test, TRUE) || !in_array(FALSE, $test, TRUE)) {

      // Make sure there is always a positive and negative test.
      $this
        ->markAsRisky();
    }
  }

  /**
   * Get the ignored config and test against the names.
   *
   * @return array
   *   The patterns and what should and shouldn't match.
   */
  public function patternProvider() {

    // For each pattern there needs to be a positive and a negative case.
    return [
      'system.site ignored' => [
        [
          'system.site',
        ],
        [
          'system.site' => TRUE,
          'system.performance' => FALSE,
        ],
      ],
      'system ignored' => [
        [
          'system.*',
        ],
        [
          'system.site' => TRUE,
          'system.performance' => TRUE,
          'node.site' => FALSE,
        ],
      ],
      'site ignored' => [
        [
          '*.site',
        ],
        [
          'system.site' => TRUE,
          'system.performance' => FALSE,
          'other.site' => TRUE,
        ],
      ],
      'node ignored' => [
        [
          'node.*',
        ],
        [
          'system.site' => FALSE,
          'node.settings' => TRUE,
          'node.settings.other' => TRUE,
        ],
      ],
      'middle ignored' => [
        [
          'start.*.end',
        ],
        [
          'start.something' => FALSE,
          'start.something.end' => TRUE,
          'start.something.else.end' => TRUE,
          'start.something.ending' => FALSE,
        ],
      ],
      'enforced excluded' => [
        [
          'system.*',
          '~system.site',
        ],
        [
          'system.site' => FALSE,
          'system.performance' => TRUE,
        ],
      ],
      'system sub-key ignored' => [
        [
          'system.*:foo',
        ],
        [
          'system.site' => TRUE,
          'system.performance' => TRUE,
          'node.foo' => FALSE,
        ],
      ],
    ];
  }

  /**
   * Test the cases that are not allowed in testPatternMatching.
   */
  public function testEverythingAndNothing() {
    $method = new \ReflectionMethod(IgnoreFilter::class, 'matchConfigName');
    $method
      ->setAccessible(TRUE);
    $none = new IgnoreFilter([
      'ignored' => [],
    ], 'config_ignore', [], new MemoryStorage());
    $all = new IgnoreFilter([
      'ignored' => [
        '*',
      ],
    ], 'config_ignore', [], new MemoryStorage());
    static::assertFalse($method
      ->invoke($none, $this
      ->randomMachineName()));
    static::assertTrue($method
      ->invoke($all, $this
      ->randomMachineName()));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PatternMatchingTest::patternProvider public function Get the ignored config and test against the names.
PatternMatchingTest::testEverythingAndNothing public function Test the cases that are not allowed in testPatternMatching.
PatternMatchingTest::testPatternMatching public function Test the ignored config matching against names.
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.
UnitTestCase::setUp protected function 340