UserAgentParserFunctionTest.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. class UserAgentParserFunctionTest extends \PHPUnit_Framework_TestCase {
  3. /**
  4. * @dataProvider userAgentDataProvider
  5. */
  6. public function test_parse_user_agent( $string, $expected ) {
  7. $result = parse_user_agent($string);
  8. $this->assertSame($expected, $result, $string . " test failed!");
  9. }
  10. public function userAgentDataProvider() {
  11. $out = array();
  12. $uas = json_decode(file_get_contents(__DIR__ . '/user_agents.json'), true);
  13. foreach( $uas as $string => $parts ) {
  14. $out[] = array( $string, $parts );
  15. }
  16. return $out;
  17. }
  18. public function test_parse_user_agent_empty() {
  19. $expected = array(
  20. 'platform' => null,
  21. 'browser' => null,
  22. 'version' => null,
  23. );
  24. $result = parse_user_agent('');
  25. $this->assertSame($result, $expected);
  26. $result = parse_user_agent('Mozilla (asdjkakljasdkljasdlkj) BlahBlah');
  27. $this->assertSame($result, $expected);
  28. }
  29. /**
  30. * @expectedException \InvalidArgumentException
  31. */
  32. public function test_no_user_agent_exception() {
  33. unset($_SERVER['HTTP_USER_AGENT']);
  34. parse_user_agent();
  35. }
  36. public function test_global_user_agent() {
  37. $_SERVER['HTTP_USER_AGENT'] = 'Test/1.0';
  38. $this->assertSame(array( 'platform' => null, 'browser' => 'Test', 'version' => '1.0' ), parse_user_agent());
  39. }
  40. }