Completed
Push — add/jetpack-mobile-package ( 96545e...4a9724 )
by
unknown
08:23
created

Test_Device_Detection::ua_provider()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 89

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 89
rs 8.24
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Tests for the Automattic\Jetpack\Mobile package.
4
 */
5
6
use PHPUnit\Framework\TestCase;
7
use Automattic\Jetpack\Device_Detection;
8
9
/**
10
 * Tests for the Device_Detection class.
11
 */
12
class Test_Device_Detection extends TestCase {
13
14
	/**
15
	 * The is_mobile tests.
16
	 *
17
	 * @param string $ua                   User agent string.
18
	 * @param bool   $expected_dumb        Expected value for `dumb` mobile detection.
0 ignored issues
show
Bug introduced by
There is no parameter named $expected_dumb. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
19
	 * @param bool   $expected_smart       Expected value for `smart` mobile detection.
0 ignored issues
show
Bug introduced by
There is no parameter named $expected_smart. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
20
	 * @param bool   $expected_mobile      Expected value for `any` mobile detection.
0 ignored issues
show
Bug introduced by
There is no parameter named $expected_mobile. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
21
	 * @param bool   $expected_ua_returned Expected value for UA returned by the method.
22
	 *
23
	 * @return void
24
	 *
25
	 * @dataProvider ua_provider
26
	 */
27
	public function test_is_mobile( $ua, array $expected_types, $expected_ua_returned ) {
28
		$_SERVER['HTTP_USER_AGENT'] = $ua;
29
30
		$device_info = Device_Detection::get_info();
31
		$all_tested_types = [ 'is_mobile', 'is_smartphone', 'is_handheld', 'is_tablet', 'is_desktop' ];
32
33
		foreach ( $all_tested_types as $type ) {
34
			$is_type_match_expected = in_array( $type, $expected_types, true );
35
			$this->assertEquals( $is_type_match_expected, $device_info[ $type ] );
36
		}
37
		$this->assertEquals( $device_info['is_mobile'] ? $expected_ua_returned : false, $device_info['is_mobile_matched_ua'] );
38
	}
39
40
41
	/**
42
	 * Data provider for test_is_mobile.
43
	 *
44
	 * @return array
45
	 */
46
	public function ua_provider() {
47
		return array(
48
49
			// Nokia 6300, dumb phone.
50
			array(
51
				'Nokia6300/2.0 (05.00) Profile/MIDP-2.0 Configuration/CLDC-1.1',
52
				array(
53
					'is_mobile',
54
					'is_handheld',
55
				),
56
				'nokia',
57
			),
58
59
			// Samsung Galaxy S8 smart phone.
60
			array(
61
				'Mozilla/5.0 (Linux; Android 9; SM-G950F Build/PPR1.180610.011; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/74.0.3729.157 Mobile Safari/537.36',
62
				array(
63
					'is_mobile',
64
					'is_smartphone',
65
					'is_handheld',
66
				),
67
				'android',
68
			),
69
70
			// iPhone X smart phone.
71
			array(
72
				'Mozilla/5.0 (iPhone; CPU iPhone OS 11_4_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.0 Mobile/15E148 Safari/604.1',
73
				array(
74
					'is_mobile',
75
					'is_smartphone',
76
					'is_handheld',
77
				),
78
				'iphone',
79
			),
80
81
			// iPad 2 10.5 tablet.
82
			array(
83
				'Mozilla/5.0 (iPad; CPU OS 11_4_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15G77 [FBAN/FBIOS;FBDV/iPad7,3;FBMD/iPad;FBSN/iOS;FBSV/11.4.1;FBSS/2;FBCR/;FBID/tablet;FBLC/en_US;FBOP/5;FBRV/0]',
84
				array(
85
					'is_tablet',
86
					'is_handheld',
87
				),
88
				false,
89
			),
90
91
			// Kindle 3.
92
			array(
93
				'Mozilla/5.0 (X11; U; Linux armv7l like Android; en-us) AppleWebKit/531.2+ (KHTML, like Gecko) Version/5.0 Safari/533.2+ Kindle/3.0+',
94
				array(
95
					'is_mobile',
96
					'is_smartphone',
97
					'is_tablet',
98
					'is_handheld',
99
				),
100
				'android',
101
			),
102
103
			// Huawei p20 smartphone.
104
			array(
105
				'Mozilla/5.0 (Linux; Android 8.1.0; CLT-L09 Build/HUAWEICLT-L09) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Mobile Safari/537.36',
106
				array(
107
					'is_mobile',
108
					'is_smartphone',
109
					'is_handheld',
110
				),
111
				'android',
112
			),
113
114
			// Googlebot smartphone.
115
			array(
116
				'Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/W.X.Y.Z‡ Mobile Safari/537.36 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
117
				array(
118
					'is_mobile',
119
					'is_smartphone',
120
					'is_handheld',
121
				),
122
				'android',
123
			),
124
125
			// Googlebot desktop.
126
			array(
127
				'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
128
				array(
129
					'is_desktop',
130
				),
131
				false,
132
			),
133
		);
134
	}
135
}
136