Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Routing / RouteCompilerTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Routing;
4
5 use Drupal\Core\Routing\RouteCompiler;
6 use Symfony\Component\Routing\Route;
7
8 use Drupal\Tests\UnitTestCase;
9
10 /**
11  * @coversDefaultClass \Drupal\Core\Routing\RouteCompiler
12  * @group Routing
13  */
14 class RouteCompilerTest extends UnitTestCase {
15
16   /**
17    * Tests RouteCompiler::getFit().
18    *
19    * @param string $path
20    *   A path whose fit will be calculated in the test.
21    * @param int $expected
22    *   The expected fit returned by RouteCompiler::getFit()
23    *
24    * @dataProvider providerTestGetFit
25    */
26   public function testGetFit($path, $expected) {
27     $route_compiler = new RouteCompiler();
28     $result = $route_compiler->getFit($path);
29     $this->assertSame($expected, $result);
30   }
31
32   /**
33    * Provides data for RouteCompilerTest::testGetFit()
34    *
35    * @return array
36    *   An array of arrays, where each inner array has the path whose fit is to
37    *   be calculated as the first value and the expected fit as the second
38    *   value.
39    */
40   public function providerTestGetFit() {
41     return [
42       ['test', 1],
43       ['/testwithleadingslash', 1],
44       ['testwithtrailingslash/', 1],
45       ['/testwithslashes/', 1],
46       ['test/with/multiple/parts', 15],
47       ['test/with/{some}/slugs', 13],
48       ['test/very/long/path/that/drupal/7/could/not/have/handled', 2047],
49     ];
50   }
51
52   /**
53    * Confirms that a route compiles properly with the necessary data.
54    */
55   public function testCompilation() {
56     $route = new Route('/test/{something}/more');
57     $route->setOption('compiler_class', 'Drupal\Core\Routing\RouteCompiler');
58     $compiled = $route->compile();
59
60     $this->assertEquals($compiled->getFit(), 5 /* That's 101 binary*/, 'The fit was incorrect.');
61     $this->assertEquals($compiled->getPatternOutline(), '/test/%/more', 'The pattern outline was not correct.');
62   }
63
64   /**
65    * Confirms that a compiled route with default values has the correct outline.
66    */
67   public function testCompilationDefaultValue() {
68     // Because "here" has a default value, it should not factor into the outline
69     // or the fitness.
70     $route = new Route('/test/{something}/more/{here}', [
71       'here' => 'there',
72     ]);
73     $route->setOption('compiler_class', 'Drupal\Core\Routing\RouteCompiler');
74     $compiled = $route->compile();
75
76     $this->assertEquals($compiled->getFit(), 5 /* That's 101 binary*/, 'The fit was not correct.');
77     $this->assertEquals($compiled->getPatternOutline(), '/test/%/more', 'The pattern outline was not correct.');
78   }
79
80 }