AgeTracker.php in Pantheon Advanced Page Cache 8
File
tests/behat/helper_classes/AgeTracker.php
View source
<?php
namespace PantheonSystems\CDNBehatHelpers;
use Behat\Mink\Session;
final class AgeTracker {
public function headersToTrack() {
return [
'age',
'cache-control',
'x-timer',
];
}
public function trackSessionHeaders($path, Session $session) {
$tracked_headers = [];
foreach ($this
->headersToTrack() as $header_name) {
$tracked_headers[$header_name] = $session
->getResponseHeader($header_name);
}
$this->headers[$path][] = $tracked_headers;
}
public function trackHeaders($path, $headers) {
$headers = array_change_key_case($headers, CASE_LOWER);
$this->headers[$path][] = array_filter($headers, function ($v, $k) {
$tracked_headers = [
'age',
'cache-control',
'x-timer',
];
return in_array($k, $tracked_headers);
}, ARRAY_FILTER_USE_BOTH);
}
public function getTrackedHeaders($path) {
return $this->headers[$path];
}
public function wasCacheClearedBetweenLastTwoRequests($path) {
$headers = $this->headers[$path];
$most_recent = array_pop($headers);
$second_most_recent = array_pop($headers);
$return = (int) $most_recent['age'] < (int) $second_most_recent['age'];
return $return;
}
public function ageIncreasedBetweenLastTwoRequests($path) {
$headers = $this->headers[$path];
$most_recent = array_pop($headers);
$second_most_recent = array_pop($headers);
$return = (int) $most_recent['age'] > (int) $second_most_recent['age'];
return $return;
}
}