You are here

class ModuleRouteSubscriberTest in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/Core/EventSubscriber/ModuleRouteSubscriberTest.php \Drupal\Tests\Core\EventSubscriber\ModuleRouteSubscriberTest

@coversDefaultClass \Drupal\Core\EventSubscriber\ModuleRouteSubscriber @group EventSubscriber

Hierarchy

Expanded class hierarchy of ModuleRouteSubscriberTest

File

core/tests/Drupal/Tests/Core/EventSubscriber/ModuleRouteSubscriberTest.php, line 15

Namespace

Drupal\Tests\Core\EventSubscriber
View source
class ModuleRouteSubscriberTest extends UnitTestCase {

  /**
   * The mock module handler.
   *
   * @var Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $moduleHandler;
  protected function setUp() : void {
    $this->moduleHandler = $this
      ->createMock('Drupal\\Core\\Extension\\ModuleHandlerInterface');
    $value_map = [
      [
        'enabled',
        TRUE,
      ],
      [
        'disabled',
        FALSE,
      ],
    ];
    $this->moduleHandler
      ->expects($this
      ->any())
      ->method('moduleExists')
      ->willReturnMap($value_map);
  }

  /**
   * Tests that removeRoute() removes routes when the module is not enabled.
   *
   * @dataProvider providerTestRemoveRoute
   * @covers ::onAlterRoutes
   *
   * @param string $route_name
   *   The machine name for the route.
   * @param array $requirements
   *   An array of requirements to use for the route.
   * @param bool $removed
   *   Whether or not the route is expected to be removed from the collection.
   */
  public function testRemoveRoute($route_name, array $requirements, $removed) {
    $collection = new RouteCollection();
    $route = new Route('', [], $requirements);
    $collection
      ->add($route_name, $route);
    $event = new RouteBuildEvent($collection, 'test');
    $route_subscriber = new ModuleRouteSubscriber($this->moduleHandler);
    $route_subscriber
      ->onAlterRoutes($event);
    if ($removed) {
      $this
        ->assertNull($collection
        ->get($route_name));
    }
    else {
      $this
        ->assertInstanceOf('Symfony\\Component\\Routing\\Route', $collection
        ->get($route_name));
    }
  }

  /**
   * Data provider for testRemoveRoute().
   */
  public function providerTestRemoveRoute() {
    return [
      [
        'enabled',
        [
          '_module_dependencies' => 'enabled',
        ],
        FALSE,
      ],
      [
        'disabled',
        [
          '_module_dependencies' => 'disabled',
        ],
        TRUE,
      ],
      [
        'enabled_or',
        [
          '_module_dependencies' => 'disabled,enabled',
        ],
        FALSE,
      ],
      [
        'enabled_or',
        [
          '_module_dependencies' => 'enabled,disabled',
        ],
        FALSE,
      ],
      [
        'disabled_or',
        [
          '_module_dependencies' => 'disabled,disabled',
        ],
        TRUE,
      ],
      [
        'enabled_and',
        [
          '_module_dependencies' => 'enabled+enabled',
        ],
        FALSE,
      ],
      [
        'enabled_and',
        [
          '_module_dependencies' => 'enabled+disabled',
        ],
        TRUE,
      ],
      [
        'enabled_and',
        [
          '_module_dependencies' => 'disabled+enabled',
        ],
        TRUE,
      ],
      [
        'disabled_and',
        [
          '_module_dependencies' => 'disabled+disabled',
        ],
        TRUE,
      ],
      [
        'no_dependencies',
        [],
        FALSE,
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ModuleRouteSubscriberTest::$moduleHandler protected property The mock module handler.
ModuleRouteSubscriberTest::providerTestRemoveRoute public function Data provider for testRemoveRoute().
ModuleRouteSubscriberTest::setUp protected function Overrides UnitTestCase::setUp
ModuleRouteSubscriberTest::testRemoveRoute public function Tests that removeRoute() removes routes when the module is not enabled.
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals Deprecated protected function Asserts if two arrays are equal by sorting them first.
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::setUpBeforeClass public static function