class AcceptHeaderMatcherTest in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/system/tests/modules/accept_header_routing_test/tests/Unit/AcceptHeaderMatcherTest.php \Drupal\Tests\accept_header_routing_teste\Unit\Routing\AcceptHeaderMatcherTest
Confirm that the mime types partial matcher is functioning properly.
@group Routing
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \Drupal\Tests\PHPUnit_Framework_TestCase- class \Drupal\Tests\accept_header_routing_teste\Unit\Routing\AcceptHeaderMatcherTest
 
Expanded class hierarchy of AcceptHeaderMatcherTest
File
- core/modules/ system/ tests/ modules/ accept_header_routing_test/ tests/ Unit/ AcceptHeaderMatcherTest.php, line 20 
- Contains \Drupal\Tests\accept_header_routing_teste\Unit\Routing\AcceptHeaderMatcherTest.
Namespace
Drupal\Tests\accept_header_routing_teste\Unit\RoutingView source
class AcceptHeaderMatcherTest extends UnitTestCase {
  /**
   * A collection of shared fixture data for tests.
   *
   * @var \Drupal\Tests\Core\Routing\RoutingFixtures
   */
  protected $fixtures;
  /**
   * The matcher object that is going to be tested.
   *
   * @var \Drupal\accept_header_routing_test\Routing\AcceptHeaderMatcher
   */
  protected $matcher;
  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->fixtures = new RoutingFixtures();
    $this->matcher = new AcceptHeaderMatcher();
  }
  /**
   * Provides data for the Accept header filtering test.
   *
   * @see Drupal\Tests\Core\Routing\AcceptHeaderMatcherTest::testAcceptFiltering()
   */
  public function acceptFilterProvider() {
    return [
      // Check that JSON routes get filtered and prioritized correctly.
      [
        'application/json, text/xml;q=0.9',
        'json',
        'route_c',
        'route_e',
      ],
      // Tests a JSON request with alternative JSON MIME type Accept header.
      [
        'application/x-json, text/xml;q=0.9',
        'json',
        'route_c',
        'route_e',
      ],
      // Tests a standard HTML request.
      [
        'text/html, text/xml;q=0.9',
        'html',
        'route_e',
        'route_c',
      ],
    ];
  }
  /**
   * Tests that requests using Accept headers get filtered correctly.
   *
   * @param string $accept_header
   *   The HTTP Accept header value of the request.
   * @param string $format
   *   The request format.
   * @param string $included_route
   *   The route name that should survive the filter and be ranked first.
   * @param string $excluded_route
   *   The route name that should be filtered out during matching.
   *
   * @dataProvider acceptFilterProvider
   */
  public function testAcceptFiltering($accept_header, $format, $included_route, $excluded_route) {
    $collection = $this->fixtures
      ->sampleRouteCollection();
    $request = Request::create('path/two', 'GET');
    $request->headers
      ->set('Accept', $accept_header);
    $request
      ->setRequestFormat($format);
    $routes = $this->matcher
      ->filter($collection, $request);
    $this
      ->assertEquals(count($routes), 4, 'The correct number of routes was found.');
    $this
      ->assertNotNull($routes
      ->get($included_route), "Route {$included_route} was found when matching {$accept_header}.");
    $this
      ->assertNull($routes
      ->get($excluded_route), "Route {$excluded_route} was not found when matching {$accept_header}.");
    foreach ($routes as $name => $route) {
      $this
        ->assertEquals($name, $included_route, "Route {$included_route} is the first one in the collection when matching {$accept_header}.");
      break;
    }
  }
  /**
   * Confirms that the AcceptHeaderMatcher throws an exception for no-route.
   *
   * @expectedException \Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException
   * @expectedExceptionMessage No route found for the specified formats application/json text/xml.
   */
  public function testNoRouteFound() {
    // Remove the sample routes that would match any method.
    $routes = $this->fixtures
      ->sampleRouteCollection();
    $routes
      ->remove('route_a');
    $routes
      ->remove('route_b');
    $routes
      ->remove('route_c');
    $routes
      ->remove('route_d');
    $request = Request::create('path/two', 'GET');
    $request->headers
      ->set('Accept', 'application/json, text/xml;q=0.9');
    $request
      ->setRequestFormat('json');
    $this->matcher
      ->filter($routes, $request);
    $this->matcher
      ->filter($routes, $request);
    $this
      ->fail('No exception was thrown.');
  }
}Members
| Name   | Modifiers | Type | Description | Overrides | 
|---|---|---|---|---|
| AcceptHeaderMatcherTest:: | protected | property | A collection of shared fixture data for tests. | |
| AcceptHeaderMatcherTest:: | protected | property | The matcher object that is going to be tested. | |
| AcceptHeaderMatcherTest:: | public | function | Provides data for the Accept header filtering test. | |
| AcceptHeaderMatcherTest:: | protected | function | Overrides UnitTestCase:: | |
| AcceptHeaderMatcherTest:: | public | function | Tests that requests using Accept headers get filtered correctly. | |
| AcceptHeaderMatcherTest:: | public | function | Confirms that the AcceptHeaderMatcher throws an exception for no-route. | |
| UnitTestCase:: | protected | property | The random generator. | |
| UnitTestCase:: | protected | property | The app root. | |
| UnitTestCase:: | protected | function | Asserts if two arrays are equal by sorting them first. | |
| UnitTestCase:: | protected | function | Mocks a block with a block plugin. | |
| UnitTestCase:: | protected | function | Returns a stub class resolver. | |
| UnitTestCase:: | public | function | Returns a stub config factory that behaves according to the passed in array. | |
| UnitTestCase:: | public | function | Returns a stub config storage that returns the supplied configuration. | |
| UnitTestCase:: | protected | function | Sets up a container with a cache tags invalidator. | |
| UnitTestCase:: | protected | function | Gets the random generator for the utility methods. | |
| UnitTestCase:: | public | function | Returns a stub translation manager that just returns the passed string. | |
| UnitTestCase:: | public | function | Generates a unique random string containing letters and numbers. | 
