AcceptHeaderTest.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. require 'AcceptHeader.php';
  3. class ContainerTest extends \PHPUnit_Framework_TestCase
  4. {
  5. public function testHeader1()
  6. {
  7. $acceptHeader = new AcceptHeader('audio/*; q=0.2, audio/basic');
  8. $this->assertEquals('audio/basic', $this->_getMedia($acceptHeader[0]));
  9. $this->assertEquals('audio/*; q=0.2', $this->_getMedia($acceptHeader[1]));
  10. }
  11. public function testHeader2()
  12. {
  13. $acceptHeader = new AcceptHeader('text/*;q=0.3, text/html;q=0.7, text/html;level=1, text/html;level=2;q=0.4, */*;q=0.5');
  14. $this->assertEquals('text/html; level=1', $this->_getMedia($acceptHeader[0]));
  15. $this->assertEquals('text/html; q=0.7', $this->_getMedia($acceptHeader[1]));
  16. $this->assertEquals('*/*; q=0.5', $this->_getMedia($acceptHeader[2]));
  17. $this->assertEquals('text/html; level=2; q=0.4', $this->_getMedia($acceptHeader[3]));
  18. $this->assertEquals('text/*; q=0.3', $this->_getMedia($acceptHeader[4]));
  19. }
  20. public function testHeader3()
  21. {
  22. $acceptHeader = new AcceptHeader('text/*, text/html, text/html;level=1, */*');
  23. $this->assertEquals('text/html; level=1', $this->_getMedia($acceptHeader[0]));
  24. $this->assertEquals('text/html', $this->_getMedia($acceptHeader[1]));
  25. $this->assertEquals('text/*', $this->_getMedia($acceptHeader[2]));
  26. $this->assertEquals('*/*', $this->_getMedia($acceptHeader[3]));
  27. }
  28. private function _getMedia(array $mediaType)
  29. {
  30. $str = $mediaType['type'] . '/' . $mediaType['subtype'];
  31. if (!empty($mediaType['params'])) {
  32. foreach ($mediaType['params'] as $k => $v) {
  33. $str .= '; ' . $k . '=' . $v;
  34. }
  35. }
  36. return $str;
  37. }
  38. }