ResponseTest.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpFoundation\Tests;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. class ResponseTest extends ResponseTestCase
  14. {
  15. public function testCreate()
  16. {
  17. $response = Response::create('foo', 301, array('Foo' => 'bar'));
  18. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
  19. $this->assertEquals(301, $response->getStatusCode());
  20. $this->assertEquals('bar', $response->headers->get('foo'));
  21. }
  22. public function testToString()
  23. {
  24. $response = new Response();
  25. $response = explode("\r\n", $response);
  26. $this->assertEquals("HTTP/1.0 200 OK", $response[0]);
  27. $this->assertEquals("Cache-Control: no-cache", $response[1]);
  28. }
  29. public function testClone()
  30. {
  31. $response = new Response();
  32. $responseClone = clone $response;
  33. $this->assertEquals($response, $responseClone);
  34. }
  35. public function testSendHeaders()
  36. {
  37. $response = new Response();
  38. $headers = $response->sendHeaders();
  39. $this->assertObjectHasAttribute('headers', $headers);
  40. $this->assertObjectHasAttribute('content', $headers);
  41. $this->assertObjectHasAttribute('version', $headers);
  42. $this->assertObjectHasAttribute('statusCode', $headers);
  43. $this->assertObjectHasAttribute('statusText', $headers);
  44. $this->assertObjectHasAttribute('charset', $headers);
  45. }
  46. public function testSend()
  47. {
  48. $response = new Response();
  49. $responseSend = $response->send();
  50. $this->assertObjectHasAttribute('headers', $responseSend);
  51. $this->assertObjectHasAttribute('content', $responseSend);
  52. $this->assertObjectHasAttribute('version', $responseSend);
  53. $this->assertObjectHasAttribute('statusCode', $responseSend);
  54. $this->assertObjectHasAttribute('statusText', $responseSend);
  55. $this->assertObjectHasAttribute('charset', $responseSend);
  56. }
  57. public function testGetCharset()
  58. {
  59. $response = new Response();
  60. $charsetOrigin = 'UTF-8';
  61. $response->setCharset($charsetOrigin);
  62. $charset = $response->getCharset();
  63. $this->assertEquals($charsetOrigin, $charset);
  64. }
  65. public function testIsCacheable()
  66. {
  67. $response = new Response();
  68. $this->assertFalse($response->isCacheable());
  69. }
  70. public function testIsCacheableWithSetTtl()
  71. {
  72. $response = new Response();
  73. $response->setTtl(10);
  74. $this->assertTrue($response->isCacheable());
  75. }
  76. public function testMustRevalidate()
  77. {
  78. $response = new Response();
  79. $this->assertFalse($response->mustRevalidate());
  80. }
  81. public function testSetNotModified()
  82. {
  83. $response = new Response();
  84. $modified = $response->setNotModified();
  85. $this->assertObjectHasAttribute('headers', $modified);
  86. $this->assertObjectHasAttribute('content', $modified);
  87. $this->assertObjectHasAttribute('version', $modified);
  88. $this->assertObjectHasAttribute('statusCode', $modified);
  89. $this->assertObjectHasAttribute('statusText', $modified);
  90. $this->assertObjectHasAttribute('charset', $modified);
  91. $this->assertEquals(304, $modified->getStatusCode());
  92. }
  93. public function testIsSuccessful()
  94. {
  95. $response = new Response();
  96. $this->assertTrue($response->isSuccessful());
  97. }
  98. public function testIsNotModified()
  99. {
  100. $response = new Response();
  101. $modified = $response->isNotModified(new Request());
  102. $this->assertFalse($modified);
  103. }
  104. public function testIsValidateable()
  105. {
  106. $response = new Response('', 200, array('Last-Modified' => $this->createDateTimeOneHourAgo()->format(DATE_RFC2822)));
  107. $this->assertTrue($response->isValidateable(), '->isValidateable() returns true if Last-Modified is present');
  108. $response = new Response('', 200, array('ETag' => '"12345"'));
  109. $this->assertTrue($response->isValidateable(), '->isValidateable() returns true if ETag is present');
  110. $response = new Response();
  111. $this->assertFalse($response->isValidateable(), '->isValidateable() returns false when no validator is present');
  112. }
  113. public function testGetDate()
  114. {
  115. $response = new Response('', 200, array('Date' => $this->createDateTimeOneHourAgo()->format(DATE_RFC2822)));
  116. $this->assertEquals(0, $this->createDateTimeOneHourAgo()->diff($response->getDate())->format('%s'), '->getDate() returns the Date header if present');
  117. $response = new Response();
  118. $date = $response->getDate();
  119. $this->assertLessThan(1, $date->diff(new \DateTime(), true)->format('%s'), '->getDate() returns the current Date if no Date header present');
  120. $response = new Response('', 200, array('Date' => $this->createDateTimeOneHourAgo()->format(DATE_RFC2822)));
  121. $now = $this->createDateTimeNow();
  122. $response->headers->set('Date', $now->format(DATE_RFC2822));
  123. $this->assertEquals(0, $now->diff($response->getDate())->format('%s'), '->getDate() returns the date when the header has been modified');
  124. $response = new Response('', 200);
  125. $response->headers->remove('Date');
  126. $this->assertInstanceOf('\DateTime', $response->getDate());
  127. }
  128. public function testGetMaxAge()
  129. {
  130. $response = new Response();
  131. $response->headers->set('Cache-Control', 's-maxage=600, max-age=0');
  132. $this->assertEquals(600, $response->getMaxAge(), '->getMaxAge() uses s-maxage cache control directive when present');
  133. $response = new Response();
  134. $response->headers->set('Cache-Control', 'max-age=600');
  135. $this->assertEquals(600, $response->getMaxAge(), '->getMaxAge() falls back to max-age when no s-maxage directive present');
  136. $response = new Response();
  137. $response->headers->set('Cache-Control', 'must-revalidate');
  138. $response->headers->set('Expires', $this->createDateTimeOneHourLater()->format(DATE_RFC2822));
  139. $this->assertEquals(3600, $response->getMaxAge(), '->getMaxAge() falls back to Expires when no max-age or s-maxage directive present');
  140. $response = new Response();
  141. $response->headers->set('Cache-Control', 'must-revalidate');
  142. $response->headers->set('Expires', -1);
  143. $this->assertEquals('Sat, 01 Jan 00 00:00:00 +0000', $response->getExpires()->format(DATE_RFC822));
  144. $response = new Response();
  145. $this->assertNull($response->getMaxAge(), '->getMaxAge() returns null if no freshness information available');
  146. }
  147. public function testSetSharedMaxAge()
  148. {
  149. $response = new Response();
  150. $response->setSharedMaxAge(20);
  151. $cacheControl = $response->headers->get('Cache-Control');
  152. $this->assertEquals('public, s-maxage=20', $cacheControl);
  153. }
  154. public function testIsPrivate()
  155. {
  156. $response = new Response();
  157. $response->headers->set('Cache-Control', 'max-age=100');
  158. $response->setPrivate();
  159. $this->assertEquals(100, $response->headers->getCacheControlDirective('max-age'), '->isPrivate() adds the private Cache-Control directive when set to true');
  160. $this->assertTrue($response->headers->getCacheControlDirective('private'), '->isPrivate() adds the private Cache-Control directive when set to true');
  161. $response = new Response();
  162. $response->headers->set('Cache-Control', 'public, max-age=100');
  163. $response->setPrivate();
  164. $this->assertEquals(100, $response->headers->getCacheControlDirective('max-age'), '->isPrivate() adds the private Cache-Control directive when set to true');
  165. $this->assertTrue($response->headers->getCacheControlDirective('private'), '->isPrivate() adds the private Cache-Control directive when set to true');
  166. $this->assertFalse($response->headers->hasCacheControlDirective('public'), '->isPrivate() removes the public Cache-Control directive');
  167. }
  168. public function testExpire()
  169. {
  170. $response = new Response();
  171. $response->headers->set('Cache-Control', 'max-age=100');
  172. $response->expire();
  173. $this->assertEquals(100, $response->headers->get('Age'), '->expire() sets the Age to max-age when present');
  174. $response = new Response();
  175. $response->headers->set('Cache-Control', 'max-age=100, s-maxage=500');
  176. $response->expire();
  177. $this->assertEquals(500, $response->headers->get('Age'), '->expire() sets the Age to s-maxage when both max-age and s-maxage are present');
  178. $response = new Response();
  179. $response->headers->set('Cache-Control', 'max-age=5, s-maxage=500');
  180. $response->headers->set('Age', '1000');
  181. $response->expire();
  182. $this->assertEquals(1000, $response->headers->get('Age'), '->expire() does nothing when the response is already stale/expired');
  183. $response = new Response();
  184. $response->expire();
  185. $this->assertFalse($response->headers->has('Age'), '->expire() does nothing when the response does not include freshness information');
  186. $response = new Response();
  187. $response->headers->set('Expires', -1);
  188. $response->expire();
  189. $this->assertNull($response->headers->get('Age'), '->expire() does not set the Age when the response is expired');
  190. }
  191. public function testGetTtl()
  192. {
  193. $response = new Response();
  194. $this->assertNull($response->getTtl(), '->getTtl() returns null when no Expires or Cache-Control headers are present');
  195. $response = new Response();
  196. $response->headers->set('Expires', $this->createDateTimeOneHourLater()->format(DATE_RFC2822));
  197. $this->assertLessThan(1, 3600 - $response->getTtl(), '->getTtl() uses the Expires header when no max-age is present');
  198. $response = new Response();
  199. $response->headers->set('Expires', $this->createDateTimeOneHourAgo()->format(DATE_RFC2822));
  200. $this->assertLessThan(0, $response->getTtl(), '->getTtl() returns negative values when Expires is in past');
  201. $response = new Response();
  202. $response->headers->set('Expires', $response->getDate()->format(DATE_RFC2822));
  203. $response->headers->set('Age', 0);
  204. $this->assertSame(0, $response->getTtl(), '->getTtl() correctly handles zero');
  205. $response = new Response();
  206. $response->headers->set('Cache-Control', 'max-age=60');
  207. $this->assertLessThan(1, 60 - $response->getTtl(), '->getTtl() uses Cache-Control max-age when present');
  208. }
  209. public function testSetClientTtl()
  210. {
  211. $response = new Response();
  212. $response->setClientTtl(10);
  213. $this->assertEquals($response->getMaxAge(), $response->getAge() + 10);
  214. }
  215. public function testGetSetProtocolVersion()
  216. {
  217. $response = new Response();
  218. $this->assertEquals('1.0', $response->getProtocolVersion());
  219. $response->setProtocolVersion('1.1');
  220. $this->assertEquals('1.1', $response->getProtocolVersion());
  221. }
  222. public function testGetVary()
  223. {
  224. $response = new Response();
  225. $this->assertEquals(array(), $response->getVary(), '->getVary() returns an empty array if no Vary header is present');
  226. $response = new Response();
  227. $response->headers->set('Vary', 'Accept-Language');
  228. $this->assertEquals(array('Accept-Language'), $response->getVary(), '->getVary() parses a single header name value');
  229. $response = new Response();
  230. $response->headers->set('Vary', 'Accept-Language User-Agent X-Foo');
  231. $this->assertEquals(array('Accept-Language', 'User-Agent', 'X-Foo'), $response->getVary(), '->getVary() parses multiple header name values separated by spaces');
  232. $response = new Response();
  233. $response->headers->set('Vary', 'Accept-Language,User-Agent, X-Foo');
  234. $this->assertEquals(array('Accept-Language', 'User-Agent', 'X-Foo'), $response->getVary(), '->getVary() parses multiple header name values separated by commas');
  235. }
  236. public function testSetVary()
  237. {
  238. $response = new Response();
  239. $response->setVary('Accept-Language');
  240. $this->assertEquals(array('Accept-Language'), $response->getVary());
  241. $response->setVary('Accept-Language, User-Agent');
  242. $this->assertEquals(array('Accept-Language', 'User-Agent'), $response->getVary(), '->setVary() replace the vary header by default');
  243. $response->setVary('X-Foo', false);
  244. $this->assertEquals(array('Accept-Language', 'User-Agent'), $response->getVary(), '->setVary() doesn\'t change the Vary header if replace is set to false');
  245. }
  246. public function testDefaultContentType()
  247. {
  248. $headerMock = $this->getMock('Symfony\Component\HttpFoundation\ResponseHeaderBag', array('set'));
  249. $headerMock->expects($this->at(0))
  250. ->method('set')
  251. ->with('Content-Type', 'text/html');
  252. $headerMock->expects($this->at(1))
  253. ->method('set')
  254. ->with('Content-Type', 'text/html; charset=UTF-8');
  255. $response = new Response('foo');
  256. $response->headers = $headerMock;
  257. $response->prepare(new Request());
  258. }
  259. public function testContentTypeCharset()
  260. {
  261. $response = new Response();
  262. $response->headers->set('Content-Type', 'text/css');
  263. // force fixContentType() to be called
  264. $response->prepare(new Request());
  265. $this->assertEquals('text/css; charset=UTF-8', $response->headers->get('Content-Type'));
  266. }
  267. public function testPrepareDoesNothingIfContentTypeIsSet()
  268. {
  269. $response = new Response('foo');
  270. $response->headers->set('Content-Type', 'text/plain');
  271. $response->prepare(new Request());
  272. $this->assertEquals('text/plain; charset=UTF-8', $response->headers->get('content-type'));
  273. }
  274. public function testPrepareDoesNothingIfRequestFormatIsNotDefined()
  275. {
  276. $response = new Response('foo');
  277. $response->prepare(new Request());
  278. $this->assertEquals('text/html; charset=UTF-8', $response->headers->get('content-type'));
  279. }
  280. public function testPrepareSetContentType()
  281. {
  282. $response = new Response('foo');
  283. $request = Request::create('/');
  284. $request->setRequestFormat('json');
  285. $response->prepare($request);
  286. $this->assertEquals('application/json', $response->headers->get('content-type'));
  287. }
  288. public function testPrepareRemovesContentForHeadRequests()
  289. {
  290. $response = new Response('foo');
  291. $request = Request::create('/', 'HEAD');
  292. $response->prepare($request);
  293. $this->assertEquals('', $response->getContent());
  294. }
  295. public function testPrepareSetsPragmaOnHttp10Only()
  296. {
  297. $request = Request::create('/', 'GET');
  298. $request->server->set('SERVER_PROTOCOL', 'HTTP/1.0');
  299. $response = new Response('foo');
  300. $response->prepare($request);
  301. $this->assertEquals('no-cache', $response->headers->get('pragma'));
  302. $this->assertEquals('-1', $response->headers->get('expires'));
  303. $request->server->set('SERVER_PROTOCOL', 'HTTP/1.1');
  304. $response = new Response('foo');
  305. $response->prepare($request);
  306. $this->assertFalse($response->headers->has('pragma'));
  307. $this->assertFalse($response->headers->has('expires'));
  308. }
  309. public function testSetCache()
  310. {
  311. $response = new Response();
  312. //array('etag', 'last_modified', 'max_age', 's_maxage', 'private', 'public')
  313. try {
  314. $response->setCache(array("wrong option" => "value"));
  315. $this->fail('->setCache() throws an InvalidArgumentException if an option is not supported');
  316. } catch (\Exception $e) {
  317. $this->assertInstanceOf('InvalidArgumentException', $e, '->setCache() throws an InvalidArgumentException if an option is not supported');
  318. $this->assertContains('"wrong option"', $e->getMessage());
  319. }
  320. $options = array('etag' => '"whatever"');
  321. $response->setCache($options);
  322. $this->assertEquals($response->getEtag(), '"whatever"');
  323. $now = new \DateTime();
  324. $options = array('last_modified' => $now);
  325. $response->setCache($options);
  326. $this->assertEquals($response->getLastModified()->getTimestamp(), $now->getTimestamp());
  327. $options = array('max_age' => 100);
  328. $response->setCache($options);
  329. $this->assertEquals($response->getMaxAge(), 100);
  330. $options = array('s_maxage' => 200);
  331. $response->setCache($options);
  332. $this->assertEquals($response->getMaxAge(), 200);
  333. $this->assertTrue($response->headers->hasCacheControlDirective('public'));
  334. $this->assertFalse($response->headers->hasCacheControlDirective('private'));
  335. $response->setCache(array('public' => true));
  336. $this->assertTrue($response->headers->hasCacheControlDirective('public'));
  337. $this->assertFalse($response->headers->hasCacheControlDirective('private'));
  338. $response->setCache(array('public' => false));
  339. $this->assertFalse($response->headers->hasCacheControlDirective('public'));
  340. $this->assertTrue($response->headers->hasCacheControlDirective('private'));
  341. $response->setCache(array('private' => true));
  342. $this->assertFalse($response->headers->hasCacheControlDirective('public'));
  343. $this->assertTrue($response->headers->hasCacheControlDirective('private'));
  344. $response->setCache(array('private' => false));
  345. $this->assertTrue($response->headers->hasCacheControlDirective('public'));
  346. $this->assertFalse($response->headers->hasCacheControlDirective('private'));
  347. }
  348. public function testSendContent()
  349. {
  350. $response = new Response('test response rendering', 200);
  351. ob_start();
  352. $response->sendContent();
  353. $string = ob_get_clean();
  354. $this->assertContains('test response rendering', $string);
  355. }
  356. public function testSetPublic()
  357. {
  358. $response = new Response();
  359. $response->setPublic();
  360. $this->assertTrue($response->headers->hasCacheControlDirective('public'));
  361. $this->assertFalse($response->headers->hasCacheControlDirective('private'));
  362. }
  363. public function testSetExpires()
  364. {
  365. $response = new Response();
  366. $response->setExpires(null);
  367. $this->assertNull($response->getExpires(), '->setExpires() remove the header when passed null');
  368. $now = new \DateTime();
  369. $response->setExpires($now);
  370. $this->assertEquals($response->getExpires()->getTimestamp(), $now->getTimestamp());
  371. }
  372. public function testSetLastModified()
  373. {
  374. $response = new Response();
  375. $response->setLastModified(new \DateTime());
  376. $this->assertNotNull($response->getLastModified());
  377. $response->setLastModified(null);
  378. $this->assertNull($response->getLastModified());
  379. }
  380. public function testIsInvalid()
  381. {
  382. $response = new Response();
  383. try {
  384. $response->setStatusCode(99);
  385. $this->fail();
  386. } catch (\InvalidArgumentException $e) {
  387. $this->assertTrue($response->isInvalid());
  388. }
  389. try {
  390. $response->setStatusCode(650);
  391. $this->fail();
  392. } catch (\InvalidArgumentException $e) {
  393. $this->assertTrue($response->isInvalid());
  394. }
  395. $response = new Response('', 200);
  396. $this->assertFalse($response->isInvalid());
  397. }
  398. /**
  399. * @dataProvider getStatusCodeFixtures
  400. */
  401. public function testSetStatusCode($code, $text, $expectedText)
  402. {
  403. $response = new Response();
  404. $response->setStatusCode($code, $text);
  405. $statusText = new \ReflectionProperty($response, 'statusText');
  406. $statusText->setAccessible(true);
  407. $this->assertEquals($expectedText, $statusText->getValue($response));
  408. }
  409. public function getStatusCodeFixtures()
  410. {
  411. return array(
  412. array('200', null, 'OK'),
  413. array('200', false, ''),
  414. array('200', 'foo', 'foo'),
  415. array('199', null, ''),
  416. array('199', false, ''),
  417. array('199', 'foo', 'foo')
  418. );
  419. }
  420. public function testIsInformational()
  421. {
  422. $response = new Response('', 100);
  423. $this->assertTrue($response->isInformational());
  424. $response = new Response('', 200);
  425. $this->assertFalse($response->isInformational());
  426. }
  427. public function testIsRedirectRedirection()
  428. {
  429. foreach (array(301, 302, 303, 307) as $code) {
  430. $response = new Response('', $code);
  431. $this->assertTrue($response->isRedirection());
  432. $this->assertTrue($response->isRedirect());
  433. }
  434. $response = new Response('', 304);
  435. $this->assertTrue($response->isRedirection());
  436. $this->assertFalse($response->isRedirect());
  437. $response = new Response('', 200);
  438. $this->assertFalse($response->isRedirection());
  439. $this->assertFalse($response->isRedirect());
  440. $response = new Response('', 404);
  441. $this->assertFalse($response->isRedirection());
  442. $this->assertFalse($response->isRedirect());
  443. $response = new Response('', 301, array('Location' => '/good-uri'));
  444. $this->assertFalse($response->isRedirect('/bad-uri'));
  445. $this->assertTrue($response->isRedirect('/good-uri'));
  446. }
  447. public function testIsNotFound()
  448. {
  449. $response = new Response('', 404);
  450. $this->assertTrue($response->isNotFound());
  451. $response = new Response('', 200);
  452. $this->assertFalse($response->isNotFound());
  453. }
  454. public function testIsEmpty()
  455. {
  456. foreach (array(201, 204, 304) as $code) {
  457. $response = new Response('', $code);
  458. $this->assertTrue($response->isEmpty());
  459. }
  460. $response = new Response('', 200);
  461. $this->assertFalse($response->isEmpty());
  462. }
  463. public function testIsForbidden()
  464. {
  465. $response = new Response('', 403);
  466. $this->assertTrue($response->isForbidden());
  467. $response = new Response('', 200);
  468. $this->assertFalse($response->isForbidden());
  469. }
  470. public function testIsOk()
  471. {
  472. $response = new Response('', 200);
  473. $this->assertTrue($response->isOk());
  474. $response = new Response('', 404);
  475. $this->assertFalse($response->isOk());
  476. }
  477. public function testIsServerOrClientError()
  478. {
  479. $response = new Response('', 404);
  480. $this->assertTrue($response->isClientError());
  481. $this->assertFalse($response->isServerError());
  482. $response = new Response('', 500);
  483. $this->assertFalse($response->isClientError());
  484. $this->assertTrue($response->isServerError());
  485. }
  486. public function testHasVary()
  487. {
  488. $response = new Response();
  489. $this->assertFalse($response->hasVary());
  490. $response->setVary('User-Agent');
  491. $this->assertTrue($response->hasVary());
  492. }
  493. public function testSetEtag()
  494. {
  495. $response = new Response('', 200, array('ETag' => '"12345"'));
  496. $response->setEtag();
  497. $this->assertNull($response->headers->get('Etag'), '->setEtag() removes Etags when call with null');
  498. }
  499. /**
  500. * @dataProvider validContentProvider
  501. */
  502. public function testSetContent($content)
  503. {
  504. $response = new Response();
  505. $response->setContent($content);
  506. $this->assertEquals((string) $content, $response->getContent());
  507. }
  508. /**
  509. * @expectedException UnexpectedValueException
  510. * @dataProvider invalidContentProvider
  511. */
  512. public function testSetContentInvalid($content)
  513. {
  514. $response = new Response();
  515. $response->setContent($content);
  516. }
  517. public function testSettersAreChainable()
  518. {
  519. $response = new Response();
  520. $setters = array(
  521. 'setProtocolVersion' => '1.0',
  522. 'setCharset' => 'UTF-8',
  523. 'setPublic' => null,
  524. 'setPrivate' => null,
  525. 'setDate' => new \DateTime,
  526. 'expire' => null,
  527. 'setMaxAge' => 1,
  528. 'setSharedMaxAge' => 1,
  529. 'setTtl' => 1,
  530. 'setClientTtl' => 1,
  531. );
  532. foreach ($setters as $setter => $arg) {
  533. $this->assertEquals($response, $response->{$setter}($arg));
  534. }
  535. }
  536. public function validContentProvider()
  537. {
  538. return array(
  539. 'obj' => array(new StringableObject),
  540. 'string' => array('Foo'),
  541. 'int' => array(2),
  542. );
  543. }
  544. public function invalidContentProvider()
  545. {
  546. return array(
  547. 'obj' => array(new \stdClass),
  548. 'array' => array(array()),
  549. 'bool' => array(true, '1'),
  550. );
  551. }
  552. protected function createDateTimeOneHourAgo()
  553. {
  554. $date = new \DateTime();
  555. return $date->sub(new \DateInterval('PT1H'));
  556. }
  557. protected function createDateTimeOneHourLater()
  558. {
  559. $date = new \DateTime();
  560. return $date->add(new \DateInterval('PT1H'));
  561. }
  562. protected function createDateTimeNow()
  563. {
  564. return new \DateTime();
  565. }
  566. protected function provideResponse()
  567. {
  568. return new Response();
  569. }
  570. }
  571. class StringableObject
  572. {
  573. public function __toString()
  574. {
  575. return 'Foo';
  576. }
  577. }