class RoutePathTest

Same name in other branches
  1. 4.0.x tests/src/Unit/RoutePathTest.php \Drupal\Tests\examples\Unit\RoutePathTest

Validate paths for routes.

Paths in routes should start with a /.

@group examples

Hierarchy

  • class \Drupal\Tests\examples\Unit\RoutePathTest extends \PHPUnit\Framework\TestCase

Expanded class hierarchy of RoutePathTest

File

tests/src/Unit/RoutePathTest.php, line 15

Namespace

Drupal\Tests\examples\Unit
View source
class RoutePathTest extends TestCase {
    
    /**
     * Find all the routing YAML files and provide them to the test.
     *
     * @return array[]
     *   An array of arrays of strings, suitable as a data provider. Strings are
     *   paths to routing YAML files.
     */
    public function provideYamls() {
        $yaml_paths = [];
        $examples_project_path = realpath(__DIR__ . '/../../..');
        $paths = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($examples_project_path, \RecursiveDirectoryIterator::FOLLOW_SYMLINKS));
        foreach ($paths as $path) {
            $pathname = $path->getPathname();
            if (strpos($pathname, 'routing.yml') !== FALSE) {
                $yaml_paths[] = [
                    $pathname,
                ];
            }
        }
        return $yaml_paths;
    }
    
    /**
     * @dataProvider provideYamls
     */
    public function testPathsStartWithSlash($yaml_path) {
        $routes = Yaml::parse(file_get_contents($yaml_path));
        foreach ($routes as $name => $route) {
            if (isset($route['path'])) {
                $this->assertEquals('/', $route['path'][0], "Route {$name} does not start with a slash '/'.");
            }
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary
RoutePathTest::provideYamls public function Find all the routing YAML files and provide them to the test.
RoutePathTest::testPathsStartWithSlash public function @dataProvider provideYamls