/vendor/willdurand/negotiation/tests/Negotiation/Tests/NegotiatorTest.php

https://gitlab.com/Snizer/PI-DEV-TUNISIAMALL3A6-WEB · PHP · 334 lines · 276 code · 37 blank · 21 comment · 2 complexity · f45b0a628400915d629736ce7e9af238 MD5 · raw file

  1. <?php
  2. namespace Negotiation\Tests;
  3. use Negotiation\Negotiator;
  4. /**
  5. * @author William Durand <william.durand1@gmail.com>
  6. */
  7. class NegotiatorTest extends TestCase
  8. {
  9. /**
  10. * @var Negotiator
  11. */
  12. private $negotiator;
  13. protected function setUp()
  14. {
  15. $this->negotiator = new Negotiator();
  16. }
  17. public function testGetBestReturnsNullWithNullHeader()
  18. {
  19. $this->assertNull($this->negotiator->getBest(null));
  20. }
  21. public function testGetBestReturnsNullWithEmptyHeader()
  22. {
  23. $this->assertNull($this->negotiator->getBest(''));
  24. }
  25. public function testGetBestReturnsNullWithUnmatchedHeader()
  26. {
  27. $this->assertNull($this->negotiator->getBest('foo, bar, yo', array('baz')));
  28. }
  29. public function testGetBestRespectsPriorities()
  30. {
  31. $acceptHeader = $this->negotiator->getBest('foo, bar, yo', array('yo'));
  32. $this->assertInstanceOf('Negotiation\AcceptHeader', $acceptHeader);
  33. $this->assertEquals('yo', $acceptHeader->getValue());
  34. }
  35. public function testGetBestInCaseInsensitive()
  36. {
  37. $acceptHeader = $this->negotiator->getBest('foo, bar, yo', array('YO'));
  38. $this->assertInstanceOf('Negotiation\AcceptHeader', $acceptHeader);
  39. $this->assertEquals('YO', $acceptHeader->getValue());
  40. }
  41. public function testGetBestWithQualities()
  42. {
  43. $acceptHeader = $this->negotiator->getBest('foo;q=0.1, bar, yo;q=0.9');
  44. $this->assertInstanceOf('Negotiation\AcceptHeader', $acceptHeader);
  45. $this->assertEquals('bar', $acceptHeader->getValue());
  46. $this->assertFalse($acceptHeader->hasParameter('q'));
  47. }
  48. /**
  49. * @dataProvider dataProviderForTestGetBest
  50. */
  51. public function testGetBest($acceptHeader, $priorities, $expected, $parameters = array())
  52. {
  53. $acceptHeader = $this->negotiator->getBest($acceptHeader, $priorities);
  54. if (null === $expected) {
  55. $this->assertNull($acceptHeader);
  56. } else {
  57. $this->assertEquals($expected, $acceptHeader->getValue());
  58. foreach ($parameters as $k => $v) {
  59. $this->assertEquals($v, $acceptHeader->getParameter($k));
  60. }
  61. }
  62. }
  63. /**
  64. * @dataProvider dataProviderForTestParseAcceptHeader
  65. */
  66. public function testParseAcceptHeader($header, $expected)
  67. {
  68. $negotiator = new TestableNegotiator();
  69. $accepts = $negotiator->parseHeader($header);
  70. $this->assertCount(count($expected), $accepts);
  71. $this->assertEquals($expected, array_map(function ($result) {
  72. return $result->getValue();
  73. }, $accepts));
  74. }
  75. /**
  76. * @dataProvider dataProviderForTestParseAcceptHeaderWithQualities
  77. */
  78. public function testParseAcceptHeaderWithQualities($header, $expected)
  79. {
  80. $negotiator = new TestableNegotiator();
  81. $accepts = $negotiator->parseHeader($header);
  82. $this->assertEquals(count($expected), count($accepts));
  83. $i = 0;
  84. foreach ($expected as $value => $quality) {
  85. $this->assertEquals($value, $accepts[$i]->getValue());
  86. $this->assertEquals($quality, $accepts[$i]->getQuality());
  87. $i++;
  88. }
  89. }
  90. /**
  91. * @dataProvider dataProviderForTestParseAcceptHeaderEnsuresPrecedence
  92. */
  93. public function testParseAcceptHeaderEnsuresPrecedence($header, $expected)
  94. {
  95. $negotiator = new TestableNegotiator();
  96. $accepts = $negotiator->parseHeader($header);
  97. $this->assertCount(count($expected), $accepts);
  98. $i = 0;
  99. foreach ($expected as $value => $quality) {
  100. $this->assertEquals($value, $accepts[$i]->getValue());
  101. $this->assertEquals($quality, $accepts[$i]->getQuality());
  102. $i++;
  103. }
  104. }
  105. /**
  106. * @dataProvider dataProviderForParseParameters
  107. */
  108. public function testParseParameters($value, $expected)
  109. {
  110. $negotiator = new TestableNegotiator();
  111. $parameters = $negotiator->parseParameters($value);
  112. $this->assertCount(count($expected), $parameters);
  113. foreach ($expected as $key => $value) {
  114. $this->assertArrayHasKey($key, $parameters);
  115. $this->assertEquals($value, $parameters[$key]);
  116. }
  117. }
  118. public static function dataProviderForTestParseAcceptHeader()
  119. {
  120. return array(
  121. array('gzip,deflate,sdch', array('gzip', 'deflate', 'sdch')),
  122. array("gzip, deflate\t,sdch", array('gzip', 'deflate', 'sdch')),
  123. array('"this;should,not=matter"', array('"this;should,not=matter"')),
  124. array('*;q=0.3,ISO-8859-1,utf-8;q=0.7', array('ISO-8859-1', 'utf-8', '*')),
  125. array('*;q=0.3,ISO-8859-1;q=0.7,utf-8;q=0.7', array('ISO-8859-1', 'utf-8', '*')),
  126. array('*;q=0.3,utf-8;q=0.7,ISO-8859-1;q=0.7', array('utf-8', 'ISO-8859-1', '*')),
  127. );
  128. }
  129. public static function dataProviderForTestParseAcceptHeaderWithQualities()
  130. {
  131. return array(
  132. array('text/html;q=0.8', array('text/html' => 0.8)),
  133. array('text/html;foo=bar;q=0.8 ', array('text/html;foo=bar' => 0.8)),
  134. array('text/html;charset=utf-8; q=0.8', array('text/html;charset=utf-8' => 0.8)),
  135. array('text/html,application/xml;q=0.9,*/*;charset=utf-8; q=0.8', array('text/html' => 1.0, 'application/xml' => 0.9, '*/*;charset=utf-8' => 0.8)),
  136. array('text/html,application/xhtml+xml', array('text/html' => 1, 'application/xhtml+xml' => 1)),
  137. array('text/html, application/json;q=0.8, text/csv;q=0.7', array('text/html' => 1, 'application/json' => 0.8, 'text/csv' => 0.7)),
  138. array('iso-8859-5, unicode-1-1;q=0.8', array('iso-8859-5' => 1, 'unicode-1-1' => 0.8)),
  139. array('gzip;q=1.0, identity; q=0.5, *;q=0', array('gzip' => 1, 'identity' => 0.5, '*' => 0)),
  140. );
  141. }
  142. public static function dataProviderForTestGetBest()
  143. {
  144. $pearCharsetHeader = 'ISO-8859-1, Big5;q=0.6,utf-8;q=0.7, *;q=0.5';
  145. $pearCharsetHeader2 = 'ISO-8859-1, Big5;q=0.6,utf-8;q=0.7';
  146. return array(
  147. array(
  148. $pearCharsetHeader,
  149. array(
  150. 'utf-8',
  151. 'big5',
  152. 'iso-8859-1',
  153. 'shift-jis',
  154. ),
  155. 'iso-8859-1'
  156. ),
  157. array(
  158. $pearCharsetHeader,
  159. array(
  160. 'utf-8',
  161. 'big5',
  162. 'shift-jis',
  163. ),
  164. 'utf-8'
  165. ),
  166. array(
  167. $pearCharsetHeader,
  168. array(
  169. 'Big5',
  170. 'shift-jis',
  171. ),
  172. 'Big5'
  173. ),
  174. array(
  175. $pearCharsetHeader,
  176. array(
  177. 'shift-jis',
  178. ),
  179. 'shift-jis'
  180. ),
  181. array(
  182. $pearCharsetHeader2,
  183. array(
  184. 'utf-8',
  185. 'big5',
  186. 'iso-8859-1',
  187. 'shift-jis',
  188. ),
  189. 'iso-8859-1'
  190. ),
  191. array(
  192. $pearCharsetHeader2,
  193. array(
  194. 'utf-8',
  195. 'big5',
  196. 'shift-jis',
  197. ),
  198. 'utf-8'
  199. ),
  200. array(
  201. $pearCharsetHeader2,
  202. array(
  203. 'Big5',
  204. 'shift-jis',
  205. ),
  206. 'Big5'
  207. ),
  208. array(
  209. 'utf-8;q=0.6,iso-8859-5;q=0.9',
  210. array(
  211. 'iso-8859-5',
  212. 'utf-8',
  213. ),
  214. 'iso-8859-5'
  215. ),
  216. array(
  217. '',
  218. array(
  219. 'iso-8859-5',
  220. 'utf-8',
  221. ),
  222. null
  223. ),
  224. array(
  225. 'audio/*; q=0.2, audio/basic',
  226. array(),
  227. 'audio/basic',
  228. ),
  229. );
  230. }
  231. public static function dataProviderForTestParseAcceptHeaderEnsuresPrecedence()
  232. {
  233. return array(
  234. array(
  235. 'text/*;q=0.3, text/html;q=0.7, text/html;level=1, text/html;level=2;q=0.4, */*;q=0.5',
  236. array(
  237. 'text/html;level=1' => 1,
  238. 'text/html;level=2' => 0.4,
  239. 'text/html' => 0.7,
  240. 'text/*' => 0.3,
  241. '*/*' => 0.5,
  242. )
  243. ),
  244. array(
  245. 'text/html,application/xhtml+xml,application/xml;q=0.9,text/*;q=0.7,*/*,image/gif; q=0.8, image/jpeg; q=0.6, image/*',
  246. array(
  247. 'text/html' => 1,
  248. 'application/xhtml+xml' => 1,
  249. 'application/xml' => 0.9,
  250. 'image/gif' => 0.8,
  251. 'text/*' => 0.7,
  252. 'image/jpeg' => 0.6,
  253. 'image/*' => 0.02,
  254. '*/*' => 0.01,
  255. )
  256. ),
  257. );
  258. }
  259. public static function dataProviderForParseParameters()
  260. {
  261. return array(
  262. array(
  263. 'application/json ;q=1.0; level=2;foo= bar',
  264. array(
  265. 'level' => 2,
  266. 'foo' => 'bar',
  267. ),
  268. ),
  269. array(
  270. 'application/json ;q = 1.0; level = 2; FOO = bAr',
  271. array(
  272. 'level' => 2,
  273. 'foo' => 'bAr',
  274. ),
  275. ),
  276. array(
  277. 'application/json;q=1.0',
  278. array(),
  279. ),
  280. array(
  281. 'application/json;foo',
  282. array(),
  283. ),
  284. );
  285. }
  286. }
  287. class TestableNegotiator extends Negotiator
  288. {
  289. public function parseHeader($acceptHeader)
  290. {
  291. return parent::parseHeader($acceptHeader);
  292. }
  293. public function parseParameters($value)
  294. {
  295. return parent::parseParameters($value);
  296. }
  297. }