Open Journal Systems  3.3.0
HeaderComparison.php
1 <?php
2 
4 
7 
12 {
24  public function compare($filteredHeaders, $actualHeaders)
25  {
26  $expected = array();
27  $ignore = array();
28  $absent = array();
29 
30  if ($actualHeaders instanceof HeaderCollection) {
31  $actualHeaders = $actualHeaders->toArray();
32  }
33 
34  foreach ($filteredHeaders as $k => $v) {
35  if ($k[0] == '_') {
36  // This header should be ignored
37  $ignore[] = str_replace('_', '', $k);
38  } elseif ($k[0] == '!') {
39  // This header must not be present
40  $absent[] = str_replace('!', '', $k);
41  } else {
42  $expected[$k] = $v;
43  }
44  }
45 
46  return $this->compareArray($expected, $actualHeaders, $ignore, $absent);
47  }
48 
59  public function compareArray(array $expected, $actual, array $ignore = array(), array $absent = array())
60  {
61  $differences = array();
62 
63  // Add information about headers that were present but weren't supposed to be
64  foreach ($absent as $header) {
65  if ($this->hasKey($header, $actual)) {
66  $differences["++ {$header}"] = $actual[$header];
67  unset($actual[$header]);
68  }
69  }
70 
71  // Check if expected headers are missing
72  foreach ($expected as $header => $value) {
73  if (!$this->hasKey($header, $actual)) {
74  $differences["- {$header}"] = $value;
75  }
76  }
77 
78  // Flip the ignore array so it works with the case insensitive helper
79  $ignore = array_flip($ignore);
80  // Allow case-insensitive comparisons in wildcards
81  $expected = array_change_key_case($expected);
82 
83  // Compare the expected and actual HTTP headers in no particular order
84  foreach ($actual as $key => $value) {
85 
86  // If this is to be ignored, the skip it
87  if ($this->hasKey($key, $ignore)) {
88  continue;
89  }
90 
91  // If the header was not expected
92  if (!$this->hasKey($key, $expected)) {
93  $differences["+ {$key}"] = $value;
94  continue;
95  }
96 
97  // Check values and take wildcards into account
98  $lkey = strtolower($key);
99  $pos = is_string($expected[$lkey]) ? strpos($expected[$lkey], '*') : false;
100 
101  foreach ((array) $actual[$key] as $v) {
102  if (($pos === false && $v != $expected[$lkey]) || $pos > 0 && substr($v, 0, $pos) != substr($expected[$lkey], 0, $pos)) {
103  $differences[$key] = "{$value} != {$expected[$lkey]}";
104  }
105  }
106  }
107 
108  return empty($differences) ? false : $differences;
109  }
110 
119  protected function hasKey($key, $array)
120  {
121  if ($array instanceof Collection) {
122  $keys = $array->getKeys();
123  } else {
124  $keys = array_keys($array);
125  }
126 
127  foreach ($keys as $k) {
128  if (!strcasecmp($k, $key)) {
129  return true;
130  }
131  }
132 
133  return false;
134  }
135 }
Guzzle\Tests\Http\Message\HeaderComparison\hasKey
hasKey($key, $array)
Definition: HeaderComparison.php:119
Guzzle\Tests\Http\Message\HeaderComparison\compare
compare($filteredHeaders, $actualHeaders)
Definition: HeaderComparison.php:24
Guzzle\Tests\Http\Message\HeaderComparison\compareArray
compareArray(array $expected, $actual, array $ignore=array(), array $absent=array())
Definition: HeaderComparison.php:59
Guzzle\Tests\Http\Message\HeaderComparison
Definition: HeaderComparison.php:11
Guzzle\Http\Message\Header\HeaderCollection
Definition: HeaderCollection.php:10
Guzzle\Tests\Http\Message
Definition: AbstractMessageTest.php:3
Guzzle\Common\Collection
Definition: paymethod/paypal/lib/vendor/guzzle/guzzle/src/Guzzle/Common/Collection.php:11