You are here

DynamicRoutingTest.php in Bakery Single Sign-On System 8.2

File

tests/src/Functional/DynamicRoutingTest.php
View source
<?php

namespace Drupal\Tests\bakery\Functional;

use Symfony\Component\Routing\Exception\RouteNotFoundException;
use Symfony\Component\Routing\Route;
class DynamicRoutingTest extends BakerySiteTestBase {
  protected $defaultTheme = 'stark';

  /**
   * @var \Drupal\Core\Routing\RouteProvider|object|null
   */
  private $routeProvider;
  public function setUp() {
    parent::setUp();
    $this->routeProvider = $this->container
      ->get('router.route_provider');
  }

  /**
   * Test that some routes are correctly registered.
   *
   * This is sort of a lower gut check on things. Other tests need to validate
   * the routes actually function.
   */
  public function testDynamicRoutes() {
    $router = $this->container
      ->get('router.route_provider');
    $this
      ->config('bakery.settings')
      ->set('bakery_is_master', 1)
      ->set('subsite_login', TRUE)
      ->save();
    $this->container
      ->get('router.builder')
      ->rebuild();
    $this->routeProvider
      ->reset();
    $this
      ->assertRouteExists('bakery.register');
    $this
      ->assertRouteExists('bakery.login');
    $this
      ->assertRouteExists('bakery.create');
    $this
      ->config('bakery.settings')
      ->set('bakery_is_master', 1)
      ->set('subsite_login', FALSE)
      ->save();
    $this->container
      ->get('router.builder')
      ->rebuild();
    $this->routeProvider
      ->reset();
    $this
      ->assertRouteNotExists('bakery.register');
    $this
      ->assertRouteNotExists('bakery.login');
    $this
      ->assertRouteExists('bakery.create');
    $this
      ->config('bakery.settings')
      ->set('bakery_is_master', 0)
      ->set('subsite_login', TRUE)
      ->save();
    $this->container
      ->get('router.builder')
      ->rebuild();
    $this->routeProvider
      ->reset();
    $this
      ->assertRouteExists('bakery.register');
    $this
      ->assertRouteExists('bakery.create');
    $this
      ->assertRouteExists('bakery.update');
    $this
      ->assertRouteExists('bakery.repair');
    $this
      ->assertRouteExists('bakery.pull');
    $this
      ->config('bakery.settings')
      ->set('bakery_is_master', 0)
      ->set('subsite_login', FALSE)
      ->save();
    $this->container
      ->get('router.builder')
      ->rebuild();
    $router
      ->reset();
    $this->routeProvider
      ->reset();
    $this
      ->assertRouteNotExists('bakery.register');
    $this
      ->assertRouteNotExists('bakery.create');
    $this
      ->assertRouteExists('bakery.update');
    $this
      ->assertRouteExists('bakery.repair');
    $this
      ->assertRouteExists('bakery.pull');
  }
  protected function assertRouteExists(string $name) {
    $this
      ->assertInstanceOf(Route::class, $this->routeProvider
      ->getRouteByName($name));
  }
  protected function assertRouteNotExists(string $name) {
    try {
      $this
        ->assertNull($this->routeProvider
        ->getRouteByName($name));
    } catch (RouteNotFoundException $e) {
      $this
        ->assertInstanceOf(RouteNotFoundException::class, $e);
    }
  }

}

Classes

Namesort descending Description
DynamicRoutingTest