Completed
Push — update/enhance-twitter-widget ( 270d91...21f466 )
by
unknown
08:07
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
 * @package automattic/jetpack-device-detection
6
 *
7
 * @phpcs:disable WordPress.Files.FileName
8
 */
9
10
use PHPUnit\Framework\TestCase;
11
use Automattic\Jetpack\Device_Detection;
12
13
/**
14
 * Tests for the Device_Detection class.
15
 */
16
class Test_Device_Detection extends TestCase {
17
18
	/**
19
	 * The is_mobile tests.
20
	 *
21
	 * @param string $ua                   User agent string.
22
	 * @param array  $expected_types       Expected device types to be detected for a user-agent.
23
	 * @param bool   $expected_ua_returned Expected value for UA returned by the method.
24
	 *
25
	 * @return void
26
	 *
27
	 * @dataProvider ua_provider
28
	 */
29
	public function test_is_mobile( $ua, array $expected_types, $expected_ua_returned ) {
30
		$_SERVER['HTTP_USER_AGENT'] = $ua;
31
32
		$device_info      = Device_Detection::get_info();
33
		$all_tested_types = array( 'is_phone', 'is_smartphone', 'is_handheld', 'is_tablet', 'is_desktop' );
34
35
		foreach ( $all_tested_types as $type ) {
36
			$is_type_match_expected = in_array( $type, $expected_types, true );
37
38
			// Test the info returned by `get_info`.
39
			$this->assertEquals( $is_type_match_expected, $device_info[ $type ] );
40
41
			// Make sure the appropriate type method exists on Device_Detection.
42
			$this->assertTrue( method_exists( 'Automattic\Jetpack\Device_Detection', $type ) );
43
44
			// Make sure the direct method (e.g. Device_Detection::is_desktop) returns the correct value.
45
			$this->assertEquals( $is_type_match_expected, call_user_func( array( 'Automattic\Jetpack\Device_Detection', $type ), $ua ) );
46
		}
47
		$this->assertEquals( $device_info['is_phone'] ? $expected_ua_returned : false, $device_info['is_phone_matched_ua'] );
48
	}
49
50
	/**
51
	 * Data provider for test_is_mobile.
52
	 *
53
	 * @return array
54
	 */
55
	public function ua_provider() {
56
		return array(
57
58
			// Nokia 6300, dumb phone.
59
			array(
60
				'Nokia6300/2.0 (05.00) Profile/MIDP-2.0 Configuration/CLDC-1.1',
61
				array(
62
					'is_phone',
63
					'is_handheld',
64
				),
65
				'nokia',
66
			),
67
68
			// Samsung Galaxy S8 smart phone.
69
			array(
70
				'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',
71
				array(
72
					'is_phone',
73
					'is_smartphone',
74
					'is_handheld',
75
				),
76
				'android',
77
			),
78
79
			// iPhone X smart phone.
80
			array(
81
				'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',
82
				array(
83
					'is_phone',
84
					'is_smartphone',
85
					'is_handheld',
86
				),
87
				'iphone',
88
			),
89
90
			// iPad 2 10.5 tablet.
91
			array(
92
				'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]',
93
				array(
94
					'is_tablet',
95
					'is_handheld',
96
				),
97
				false,
98
			),
99
100
			// Kindle 3.
101
			array(
102
				'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+',
103
				array(
104
					'is_phone',
105
					'is_smartphone',
106
					'is_tablet',
107
					'is_handheld',
108
				),
109
				'android',
110
			),
111
112
			// Huawei p20 smartphone.
113
			array(
114
				'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',
115
				array(
116
					'is_phone',
117
					'is_smartphone',
118
					'is_handheld',
119
				),
120
				'android',
121
			),
122
123
			// Googlebot smartphone.
124
			array(
125
				'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)',
126
				array(
127
					'is_phone',
128
					'is_smartphone',
129
					'is_handheld',
130
				),
131
				'android',
132
			),
133
134
			// Googlebot desktop.
135
			array(
136
				'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
137
				array(
138
					'is_desktop',
139
				),
140
				false,
141
			),
142
		);
143
	}
144
}
145