forked from tobie/ua-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuaparser-test.php
113 lines (97 loc) · 4.57 KB
/
uaparser-test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
/*!
* ua-parser-php Test Suite v1.0.0
*
* Copyright (c) 2012 Dave Olsen, http://dmolsen.com
* Licensed under the MIT license
*
* This is the test suit for ua-parser-php to make sure it matches
* the standards set forth for ua-parser libraries.
*
* IMPORTANT: This test suite skips the Chrome Frame tests because
* this lib doesn't support that feature. It also skips
* the Blackberry Playbook test in test_device.yaml
* because my lib considers it a mobile device. The test
* suite apparently doesn't.
*
*/
// address 5.2 compatibility
if (!defined('__DIR__')) define('__DIR__', dirname(__FILE__));
// include UAParser.php and make sure to turn off the CLI error
require __DIR__."/UAParser.php";
/**
* Take the elements from the test and test the against the results from UAParser.php
*
* @return string the result of the test
*/
function test($tc_ua,$tc_family,$tc_major,$tc_minor,$tc_patch,$type) {
$ua = UA::parse($tc_ua);
if ($type == "b") {
$family_result = ($ua->family == $tc_family) ? true : false;
$major_result = ($ua->major == $tc_major) ? true : false;
$minor_result = ($ua->minor == $tc_minor) ? true : false;
$patch_result = ($ua->patch == $tc_patch) ? true : false;
} else if ($type == "os") {
$family_result = ($ua->os == $tc_family) ? true : false;
$major_result = ($ua->osMajor == $tc_major) ? true : false;
$minor_result = ($ua->osMinor == $tc_minor) ? true : false;
$patch_result = ($ua->osPatch == $tc_patch) ? true : false;
}
if (!$family_result || !$major_result || !$minor_result || !$patch_result) {
print "\n mismatch: got ".$ua->family." ".$ua->major." ".$ua->minor." ".$ua->patch." and expected ".$tc_family." ".$tc_major." ".$tc_minor." ".$tc_patch;
print "\n the mismatched ua: ".$tc_ua;
} else {
print ".";
}
}
/*
* Main logic for the test suite
*/
if (php_sapi_name() == 'cli') {
print "\nrunning UAParser.php against test_user_agent_parser.yaml...\n";
$data = Spyc::YAMLLoad(__DIR__."/../test_resources/test_user_agent_parser.yaml");
foreach($data['test_cases'] as $test_case) {
if (!isset($test_case['js_ua'])) {
test($test_case['user_agent_string'],$test_case['family'],$test_case['major'],$test_case['minor'],$test_case['patch'],"b");
}
}
print "\n\nrunning UAParser.php against test_user_agent_parser_os.yaml...\n";
$data = Spyc::YAMLLoad(__DIR__."/../test_resources/test_user_agent_parser_os.yaml");
foreach ($data['test_cases'] as $test_case) {
test($test_case['user_agent_string'],$test_case['family'],$test_case['major'],$test_case['minor'],$test_case['patch'],"os");
}
print "\n\nrunning UAParser.php against additional_os_tests.yaml...\n";
$data = Spyc::YAMLLoad(__DIR__."/../test_resources/additional_os_tests.yaml");
foreach ($data['test_cases'] as $test_case) {
test($test_case['user_agent_string'],$test_case['family'],$test_case['major'],$test_case['minor'],$test_case['patch'],"os");
}
print "\n\nrunning UAParser.php against test_device.yaml...\n";
$data = Spyc::YAMLLoad(__DIR__."/../test_resources/test_device.yaml");
foreach ($data['test_cases'] as $test_case) {
if ($test_case['family'] != "Blackberry Playbook") {
$ua = UA::parse($test_case['user_agent_string']);
$family_result = ($ua->device == $test_case['family']) ? true : false;
$mobile_result = ($ua->isMobileDevice == $test_case['is_mobile']) ? true : false;
$spider_result = ($ua->isSpider == $test_case['is_spider']) ? true : false;
if (!$family_result || !$mobile_result || !$spider_result) {
print "\n mismatch: got d: ".$ua->device." m: ".$ua->isMobile." s: ".$ua->isSpider." and expected d: ".$test_case['family']." m: ".$test_case['is_mobile']." s: ".$test_case['is_spider'];
print "\n the mismatched ua: ".$test_case['user_agent_string'];
} else {
print ".";
}
}
}
print "\n\nrunning UAParser.php against firefox_user_agent_strings.yaml...\n";
$data = Spyc::YAMLLoad(__DIR__."/../test_resources/firefox_user_agent_strings.yaml");
foreach ($data['test_cases'] as $test_case) {
test($test_case['user_agent_string'],$test_case['family'],$test_case['major'],$test_case['minor'],$test_case['patch'],"b");
}
print "\n\nrunning UAParser.php against pgts_browser_list.yaml... (takes a long time to load)\n";
$data = Spyc::YAMLLoad(__DIR__."/../test_resources/pgts_browser_list.yaml");
foreach ($data['test_cases'] as $test_case) {
test($test_case['user_agent_string'],$test_case['family'],$test_case['major'],$test_case['minor'],$test_case['patch'],"b");
}
print "\ndone testing...\n";
} else {
print "You must run this file from the command line.";
}