UserAgentTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. use PHPUnit\Framework\TestCase;
  3. /**
  4. * @license MIT License https://github.com/serbanghita/Mobile-Detect/blob/master/LICENSE.txt
  5. * @link http://mobiledetect.net
  6. */
  7. class UserAgentTest extends TestCase
  8. {
  9. protected $detect;
  10. protected static $ualist = array();
  11. protected static $json;
  12. public function setUp()
  13. {
  14. $this->detect = new Mobile_Detect;
  15. }
  16. public static function generateJson()
  17. {
  18. //in case this gets run multiple times
  19. if (isset(self::$json)) {
  20. return self::$json;
  21. }
  22. //the json and PHP formatted files
  23. $jsonFile = dirname(__FILE__) . '/ualist.json';
  24. $phpFile = dirname(__FILE__) . '/UA_List.inc.php';
  25. //currently stored as a PHP array
  26. $list = include $phpFile;
  27. //check recency of the file
  28. if (file_exists($jsonFile) && is_readable($jsonFile)) {
  29. //read the json file
  30. $json = json_decode(file_get_contents($jsonFile), true);
  31. //check that the hash matches
  32. $hash = isset($json['hash']) ? $json['hash'] : null;
  33. if ($hash == sha1(serialize($list))) {
  34. //file is up to date, just read the json file
  35. self::$json = $json['user_agents'];
  36. return self::$json;
  37. }
  38. }
  39. //uses the UA_List.inc.php to generate a json file
  40. if (file_exists($jsonFile) && !is_writable($jsonFile)) {
  41. throw new RuntimeException("Need to be able to create/update $jsonFile from UA_List.inc.php.");
  42. }
  43. if (!is_writable(dirname($jsonFile))) {
  44. throw new RuntimeException("Insufficient permissions to create this file: $jsonFile");
  45. }
  46. //print_r($list['Acer']); exit;
  47. $json = array();
  48. foreach ($list as $vendor => $vendorList) {
  49. foreach ($vendorList as $userAgent => $props) {
  50. if (is_int($userAgent)) {
  51. //this means that the user agent is the props
  52. $userAgent = $props;
  53. $props = array();
  54. }
  55. $tmp = array(
  56. 'vendor' => $vendor,
  57. 'user_agent' => $userAgent
  58. );
  59. if (isset($props['isMobile'])) {
  60. $tmp['mobile'] = $props['isMobile'];
  61. }
  62. if (isset($props['isTablet'])) {
  63. $tmp['tablet'] = $props['isTablet'];
  64. }
  65. if (isset($props['version'])) {
  66. $tmp['version'] = $props['version'];
  67. }
  68. if (isset($props['model'])) {
  69. $tmp['model'] = $props['model'];
  70. }
  71. $json[] = $tmp;
  72. }
  73. }
  74. //save the hash
  75. $hash = sha1(serialize($list));
  76. $json = array(
  77. 'hash' => $hash,
  78. 'user_agents' => $json
  79. );
  80. if (defined('JSON_PRETTY_PRINT')) {
  81. $jsonString = json_encode($json, JSON_PRETTY_PRINT);
  82. } else {
  83. $jsonString = json_encode($json);
  84. }
  85. file_put_contents($jsonFile, $jsonString);
  86. self::$json = $json['user_agents'];
  87. return self::$json;
  88. }
  89. public static function setUpBeforeClass()
  90. {
  91. //generate json file first
  92. self::generateJson();
  93. //get the generated JSON data
  94. $json = self::$json;
  95. //make a list that is usable by functions (THE ORDER OF THE KEYS MATTERS!)
  96. foreach ($json as $userAgent) {
  97. $tmp = array();
  98. $tmp[] = isset($userAgent['user_agent']) ? $userAgent['user_agent'] : null;
  99. $tmp[] = isset($userAgent['mobile']) ? $userAgent['mobile'] : null;
  100. $tmp[] = isset($userAgent['tablet']) ? $userAgent['tablet'] : null;
  101. $tmp[] = isset($userAgent['version']) ? $userAgent['version'] : null;
  102. $tmp[] = isset($userAgent['model']) ? $userAgent['model'] : null;
  103. $tmp[] = isset($userAgent['vendor']) ? $userAgent['vendor'] : null;
  104. self::$ualist[] = $tmp;
  105. }
  106. }
  107. public function userAgentData()
  108. {
  109. if (!count(self::$ualist)) {
  110. self::setUpBeforeClass();
  111. }
  112. return self::$ualist;
  113. }
  114. /**
  115. * @medium
  116. * @dataProvider userAgentData
  117. */
  118. public function testUserAgents($userAgent, $isMobile, $isTablet, $version, $model, $vendor)
  119. {
  120. //make sure we're passed valid data
  121. if (!is_string($userAgent) || !is_bool($isMobile) || !is_bool($isTablet)) {
  122. $this->markTestIncomplete("The User-Agent $userAgent does not have sufficient information for testing.");
  123. return;
  124. }
  125. //setup
  126. $this->detect->setUserAgent($userAgent);
  127. //is mobile?
  128. $this->assertEquals($isMobile, $this->detect->isMobile());
  129. //is tablet?
  130. $this->assertEquals($isTablet, $this->detect->isTablet(), 'FAILED: ' . $userAgent . ' isTablet: ' . $isTablet);
  131. if (isset($version)) {
  132. foreach ($version as $condition => $assertion) {
  133. $this->assertEquals($assertion, $this->detect->version($condition), 'FAILED UA (version("'.$condition.'")): '.$userAgent);
  134. }
  135. }
  136. //version property tests
  137. if (isset($version)) {
  138. foreach ($version as $property => $stringVersion) {
  139. $v = $this->detect->version($property);
  140. $this->assertSame($stringVersion, $v);
  141. }
  142. }
  143. //@todo: model test, not sure how exactly yet
  144. //@todo: vendor test. The below is theoretical, but fails 50% of the tests...
  145. /*if (isset($vendor)) {
  146. $method = "is$vendor";
  147. $this->assertTrue($this->detect->{$method}(), "Expected Mobile_Detect::{$method}() to be true.");
  148. }*/
  149. }
  150. }