/vendor/mobiledetect/mobiledetectlib/tests/UserAgentTest.php

https://gitlab.com/vannh/portal_training · PHP · 189 lines · 116 code · 40 blank · 33 comment · 22 complexity · e6415fe22e8f1a4bec3162836b41ad4c MD5 · raw file

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