Completed
Push — master ( 52b7dd...834530 )
by Enrico
02:14
created

LocalBusinessTest::testGetDefaultOptions()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 109
Code Lines 82

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 109
rs 8.2857
c 0
b 0
f 0
cc 1
eloc 82
nc 1
nop 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
class LocalBusinessTest extends PHPUnit_Framework_TestCase
4
{	
5
    /**
6
     * @dataProvider goodLocalBusiness
7
     */	
8
	public function testConstructor($data, $expectedData)
9
	{
10
		$localBusiness = new BOTK\Model\LocalBusiness($data);		
11
		$this->assertEquals($expectedData, $localBusiness->asArray());
12
	}
13
	public function goodLocalBusiness()
14
    {
15
    	return array( 
16
    		array(
17
	    		array(),
18
	    		array(
19
					'base'				=> 'http://linkeddata.center/botk/resource/',
20
					'addressCountry'	=> 'IT',
21
				),
22
			),
23
			
24
    		array(
25
	    		array(
26
					'base'				=> 'urn:a:',
27
					'addressCountry'	=> 'US',
28
				),
29
	    		array(
30
					'base'				=> 'urn:a:',
31
					'addressCountry'	=> 'US',
32
				),
33
			),
34
			
35
    		array(
36
	    		array(
37
	    			'id'				=> '1234567890',
38
					'taxID'				=> 'fgn nrc 63S0 6F205 A',
39
					'vatID'				=> '01234567890',
40
					'legalName'			=> 'Example srl',
41
					'businessName'		=> 'Example',
42
					'businessType'		=> 'schema:MedicalOrganization',
43
					'addressCountry'	=> 'IT',
44
					'addressLocality'	=> 'LECCO',
45
					'addressRegion'		=> 'LC',
46
					'streetAddress'		=> 'Via  Fausto Valsecchi,124',
47
					'postalCode'		=> '23900',
48
					'page'				=> 'http://linkeddata.center/',
49
					'telephone'			=> '+39 3356382949',
50
					'faxNumber'			=> '+39 335 63 82 949',
51
					'email'				=> array('[email protected]'),
52
					'addressDescription'=> 'Via  F. Valsecchi,124-23900 Lecco (LC)',
53
					'lat'				=> '1.12345',
54
					'long'				=> '2.123456',
55
				),
56
	    		array(
57
					'base'				=> 'http://linkeddata.center/botk/resource/',
58
	    			'id'				=> '1234567890',
59
					'businessType'		=> array('schema:MedicalOrganization'),
60
					'taxID'				=> 'FGNNRC63S06F205A',
61
					'vatID'				=> '01234567890',
62
					'legalName'			=> 'EXAMPLE SRL',
63
					'businessName'		=> array('Example'),
64
					'addressCountry'	=> 'IT',
65
					'addressLocality'	=> 'LECCO',
66
					'addressRegion'		=> 'LC',
67
					'streetAddress'		=> 'VIA FAUSTO VALSECCHI, 124',
68
					'postalCode'		=> '23900',
69
					'page'				=> array('http://linkeddata.center/'),
70
					'telephone'			=> '3356382949',
71
					'faxNumber'			=> '3356382949',
72
					'email'				=> array('[email protected]'),
73
					'addressDescription'=> 'VIA F.VALSECCHI, 124 - 23900 LECCO (LC)',
74
					'lat'				=> '1.12345',
75
					'long'				=> '2.123456',
76
				),
77
			),
78
		);
79
   	}
80
81
82
	public function testGetDefaultOptions()
83
	{	
84
		$expectedOptions =  array (
85
			'uri'				=> array(
86
									'filter'    => FILTER_SANITIZE_URL,
87
	                            	'flags'  	=> FILTER_REQUIRE_SCALAR,
88
				                   ),
89
			'base'				=> array(
90
									'default'	=> 'http://linkeddata.center/botk/resource/',
91
									'filter'    => FILTER_SANITIZE_URL,
92
	                            	'flags'  	=> FILTER_REQUIRE_SCALAR,
93
				                   ),
94
			'id'				=> array(		
95
									'filter'    => FILTER_CALLBACK,
96
			                        'options' 	=> '\BOTK\Filters::FILTER_SANITIZE_ID',
97
	                            	'flags'  	=> FILTER_REQUIRE_SCALAR,
98
				                   ),
99
			'page'				=> array(	
100
									'filter'    => FILTER_SANITIZE_URL,
101
	                            	'flags'  	=> FILTER_FORCE_ARRAY,
102
				                   ),
103
			'homepage'			=> array(	
104
									'filter'    => FILTER_SANITIZE_URL,
105
	                            	'flags'  	=> FILTER_FORCE_ARRAY,
106
				                   ),
107
			'businessType'		=> array(		
108
									// additional types  as extension of schema:LocalBusiness
109
									'filter'    => FILTER_DEFAULT,
110
	                            	'flags'  	=> FILTER_FORCE_ARRAY,
111
				                   ),
112
			'taxID'				=> array(	
113
									'filter'    => FILTER_CALLBACK,
114
			                        'options' 	=> '\BOTK\Filters::FILTER_SANITIZE_TOKEN',
115
	                            	'flags'  	=> FILTER_REQUIRE_SCALAR,
116
				                   ),
117
			'vatID'				=> array(	// italian rules
118
									'filter'    => FILTER_VALIDATE_REGEXP,
119
			                        'options' 	=> array('regexp'=>'/^[0-9]{11}$/'),
120
	                            	'flags'  	=> FILTER_REQUIRE_SCALAR,
121
				                   ),
122
			'legalName'			=> array(
123
									'filter'    => FILTER_CALLBACK,
124
			                        'options' 	=> '\BOTK\Filters::FILTER_SANITIZE_ADDRESS',
125
	                            	'flags'  	=> FILTER_REQUIRE_SCALAR,
126
				                   ),
127
			'businessName'		=> array(
128
									// a schema:alternateName for schema:PostalAddress
129
									'filter'    => FILTER_DEFAULT,
130
	                            	'flags'  	=> FILTER_FORCE_ARRAY,
131
								   ),
132
			'addressDescription'=> array(	//	
133
									'filter'    => FILTER_CALLBACK,
134
			                        'options' 	=> '\BOTK\Filters::FILTER_SANITIZE_ADDRESS',
135
	                            	'flags'  	=> FILTER_REQUIRE_SCALAR,
136
				                   ),
137
			'addressCountry'	=> array(
138
									'default'	=> 'IT',		
139
									'filter'    => FILTER_VALIDATE_REGEXP,
140
			                        'options' 	=> array('regexp'=>'/^[A-Z]{2}$/'),
141
	                            	'flags'  	=> FILTER_REQUIRE_SCALAR,
142
				                   ),
143
			'addressLocality'	=> array(	
144
									'filter'    => FILTER_CALLBACK,
145
			                        'options' 	=> '\BOTK\Filters::FILTER_SANITIZE_ADDRESS',
146
	                            	'flags'  	=> FILTER_REQUIRE_SCALAR,
147
				                   ),
148
			'addressRegion'		=> array(	
149
									'filter'    => FILTER_CALLBACK,
150
			                        'options' 	=> '\BOTK\Filters::FILTER_SANITIZE_ADDRESS',
151
	                            	'flags'  	=> FILTER_REQUIRE_SCALAR,
152
				                   ),
153
			'streetAddress'		=> array(	
154
									'filter'    => FILTER_CALLBACK,
155
			                        'options' 	=> '\BOTK\Filters::FILTER_SANITIZE_ADDRESS',
156
	                            	'flags'  	=> FILTER_REQUIRE_SCALAR,
157
				                   ),
158
			'postalCode'		=> array(	// italian rules
159
									'filter'    => FILTER_VALIDATE_REGEXP,
160
			                        'options' 	=> array('regexp'=>'/^[0-9]{5}$/'),
161
	                            	'flags'  	=> FILTER_REQUIRE_SCALAR,
162
				                   ),
163
			'telephone'			=> array(	
164
									'filter'    => FILTER_CALLBACK,	
165
			                        'options' 	=> '\BOTK\Filters::FILTER_SANITIZE_TELEPHONE',
166
	                            	'flags'  	=> FILTER_FORCE_ARRAY,
167
				                   ),
168
			'faxNumber'			=> array(	
169
									'filter'    => FILTER_CALLBACK,
170
			                        'options' 	=> '\BOTK\Filters::FILTER_SANITIZE_TELEPHONE',
171
	                            	'flags'  	=> FILTER_FORCE_ARRAY,
172
				                   ),
173
			'email'				=> array(	
174
									'filter'    => FILTER_CALLBACK,
175
			                        'options' 	=> '\BOTK\Filters::FILTER_SANITIZE_EMAIL',
176
	                            	'flags'  	=> FILTER_FORCE_ARRAY,
177
				                   ),
178
			'lat'				=> array( 
179
									'filter'    => FILTER_CALLBACK,
180
			                        'options' 	=> '\BOTK\Filters::FILTER_SANITIZE_GEO',
181
				                   ),
182
			'long'				=> array( 
183
									'filter'    => FILTER_CALLBACK,
184
			                        'options' 	=> '\BOTK\Filters::FILTER_SANITIZE_GEO',
185
				                   ),
186
		);
187
		
188
		$localBusiness = new BOTK\Model\LocalBusiness(array());
189
		$this->assertEquals($expectedOptions, $localBusiness->getOptions());
190
	}
191
192
193
194
195
    /**
196
     * @dataProvider goodRdf
197
     */	
198
	public function testRdfGeneration($data, $rdf, $tripleCount)
199
	{
200
		$localBusiness = new BOTK\Model\LocalBusiness($data);		
201
		$this->assertEquals($rdf, $localBusiness->asTurtle());
202
		$this->assertEquals($tripleCount, $localBusiness->getTripleCount());
203
	}
204
	public function goodRdf()
205
    {
206
    	return array(
207
    		array( 
208
    			array('uri'=>'urn:test:a'),
209
    			'<urn:test:a> a schema:LocalBusiness;schema:address <urn:test:a_address>. <urn:test:a_address> a schema:PostalAddress;schema:addressCountry "IT". ',
210
    			4,
211
			),
212
    		array(
213
    			array(
214
    				'base'				=> 'urn:test:',
215
    				'id'				=> 'b',
216
					'vatID'				=> '01234567890',
217
					'legalName'			=> 'Calenda chiodi snc',
218
				),
219
    			'<urn:test:b> a schema:LocalBusiness;dct:identifier "b";schema:vatID "01234567890";schema:legalName "CALENDA CHIODI SNC";schema:address <urn:test:b_address>. <urn:test:b_address> a schema:PostalAddress;schema:addressCountry "IT". ',
220
    			7,
221
			),
222
			
223
    		array(
224
    			array(
225
	    			'id'				=> '1234567890',
226
					'taxID'				=> 'fgn nrc 63S0 6F205 A',
227
					'vatID'				=> '01234567890',
228
					'legalName'			=> 'Example srl',
229
					'businessName'		=> 'Example',
230
					'businessType'		=> 'schema:MedicalOrganization',
231
					'addressCountry'	=> 'IT',
232
					'addressLocality'	=> 'LECCO',
233
					'addressRegion'		=> 'LC',
234
					'streetAddress'		=> 'Via  Fausto Valsecchi,124',
235
					'postalCode'		=> '23900',
236
					'page'				=> 'http://linkeddata.center/',
237
					'telephone'			=> '+39 3356382949',
238
					'faxNumber'			=> '+39 335 63 82 949',
239
					'email'				=> array('[email protected]'),
240
					'mailbox'			=> '[email protected]',
241
					'addressDescription'=> 'Via  F. Valsecchi,124-23900 Lecco (LC)',
242
					'lat'				=> '1.12345',
243
					'long'				=> '2.123456',
244
				),
245
    			'<http://linkeddata.center/botk/resource/1234567890> a schema:LocalBusiness;a schema:MedicalOrganization;dct:identifier "1234567890";schema:vatID "01234567890";schema:legalName "EXAMPLE SRL";schema:alternateName "Example";schema:telephone "3356382949";schema:faxNumber "3356382949";schema:page <http://linkeddata.center/>;schema:email "[email protected]";schema:geo <geo:1.12345,2.123456>;schema:address <http://linkeddata.center/botk/resource/1234567890_address>. <http://linkeddata.center/botk/resource/1234567890_address> a schema:PostalAddress;schema:description "VIA F.VALSECCHI, 124 - 23900 LECCO (LC)";schema:streetAddress "VIA FAUSTO VALSECCHI, 124";schema:postalCode "23900";schema:addressLocality "LECCO";schema:addressRegion "LC";schema:addressCountry "IT". <geo:1.12345,2.123456> a schema:GeoCoordinates;wgs:lat "1.12345"^^xsd:float;wgs:long "2.123456"^^xsd:float . ',
246
    			22,
247
			),
248
		);
249
	}
250
251
252
253
    /**
254
     * @dataProvider structuredAdresses
255
     */	
256
	public function testBuildNormalizedAddress($rawdata, $expectedData)
257
	{
258
		$localBusiness = new BOTK\Model\LocalBusiness($rawdata);
259
		$data=$localBusiness->asArray();
260
		$this->assertEquals($expectedData, $data['addressDescription']);
261
	}
262
	public function structuredAdresses()
263
    {
264
    	return array( 
265
    		array( 
266
    			array(
267
    				'streetAddress'		=> 'Lungolario Luigi Cadorna, 1',
268
    				'addressLocality'	=> 'Lecco',
269
    				'addressRegion'		=> 'LC',
270
    				'addressCountry'	=> 'IT',
271
    				'postalCode'		=> '23900',	
272
				),	
273
				'LUNGOLARIO LUIGI CADORNA, 1, 23900 LECCO (LC)'
274
			),
275
    		array( 
276
    			array(
277
    				'streetAddress'		=> 'Lungolario Luigi Cadorna, 1',
278
    				'addressLocality'	=> 'Lecco',
279
    				'addressCountry'	=> 'IT',	
280
				),	
281
				'LUNGOLARIO LUIGI CADORNA, 1, LECCO'
282
			),
283
    		array( 
284
    			array(
285
    				'streetAddress'		=> 'Lungolario Luigi Cadorna, 1',
286
    				'addressCountry'	=> 'IT',
287
    				'postalCode'		=> '23900',	
288
				),	
289
				'LUNGOLARIO LUIGI CADORNA, 1, 23900'
290
			),
291
    		array( 
292
    			array(
293
    				'addressDescription'	=> 'test address',
294
				),	
295
				'TEST ADDRESS'
296
			),
297
		);
298
   	}
299
300
301
    public  function testBadLocalBusiness()
302
	{
303
		$badData = array(
304
			'vatID'				=>	'not a vat',
305
			'addressCountry'	=>  'ITALY', 	// should be a two character ISO country code
306
			'postalCode'		=>  '234992',	// too long
307
			'email'				=>  'not an e mail',
308
		);
309
		$localBusiness = new BOTK\Model\LocalBusiness($badData);
310
		$this->assertEquals(array_keys($badData), $localBusiness->getDroppedFields());
311
	}
312
313
}
314
315