public function ResponseTest::testSetCache

File

drupal/core/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/Tests/ResponseTest.php, line 386

Class

ResponseTest

Namespace

Symfony\Component\HttpFoundation\Tests

Code

public function testSetCache() {
  $response = new Response();

  //array('etag', 'last_modified', 'max_age', 's_maxage', 'private', 'public')
  try {
    $response
      ->setCache(array(
      "wrong option" => "value",
    ));
    $this
      ->fail('->setCache() throws an InvalidArgumentException if an option is not supported');
  } catch (\Exception $e) {
    $this
      ->assertInstanceOf('InvalidArgumentException', $e, '->setCache() throws an InvalidArgumentException if an option is not supported');
    $this
      ->assertContains('"wrong option"', $e
      ->getMessage());
  }
  $options = array(
    'etag' => '"whatever"',
  );
  $response
    ->setCache($options);
  $this
    ->assertEquals($response
    ->getEtag(), '"whatever"');
  $now = new \DateTime();
  $options = array(
    'last_modified' => $now,
  );
  $response
    ->setCache($options);
  $this
    ->assertEquals($response
    ->getLastModified()
    ->getTimestamp(), $now
    ->getTimestamp());
  $options = array(
    'max_age' => 100,
  );
  $response
    ->setCache($options);
  $this
    ->assertEquals($response
    ->getMaxAge(), 100);
  $options = array(
    's_maxage' => 200,
  );
  $response
    ->setCache($options);
  $this
    ->assertEquals($response
    ->getMaxAge(), 200);
  $this
    ->assertTrue($response->headers
    ->hasCacheControlDirective('public'));
  $this
    ->assertFalse($response->headers
    ->hasCacheControlDirective('private'));
  $response
    ->setCache(array(
    'public' => true,
  ));
  $this
    ->assertTrue($response->headers
    ->hasCacheControlDirective('public'));
  $this
    ->assertFalse($response->headers
    ->hasCacheControlDirective('private'));
  $response
    ->setCache(array(
    'public' => false,
  ));
  $this
    ->assertFalse($response->headers
    ->hasCacheControlDirective('public'));
  $this
    ->assertTrue($response->headers
    ->hasCacheControlDirective('private'));
  $response
    ->setCache(array(
    'private' => true,
  ));
  $this
    ->assertFalse($response->headers
    ->hasCacheControlDirective('public'));
  $this
    ->assertTrue($response->headers
    ->hasCacheControlDirective('private'));
  $response
    ->setCache(array(
    'private' => false,
  ));
  $this
    ->assertTrue($response->headers
    ->hasCacheControlDirective('public'));
  $this
    ->assertFalse($response->headers
    ->hasCacheControlDirective('private'));
}