I18nJsonFileIntegrityTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 0
dl 0
loc 64
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testI18NJsonDecodeEncode() 0 10 1
A i18nFileProvider() 0 15 2
A findFilesForExtension() 0 16 3
A getDescriptiveJsonError() 0 13 1
1
<?php
2
3
namespace SUC\Tests\Integration;
4
5
/**
6
 * @group summary-cards
7
 * @group medium
8
 *
9
 * @license GNU GPL v2+
10
 * @since 1.0
11
 *
12
 * @author mwjames
13
 */
14
class I18nJsonFileIntegrityTest extends \PHPUnit_Framework_TestCase {
15
16
	/**
17
	 * @dataProvider i18nFileProvider
18
	 */
19
	public function testI18NJsonDecodeEncode( $file ) {
20
21
		$contents = file_get_contents( $file );
22
23
		$this->assertInternalType(
24
			'array',
25
			json_decode( $contents, true ),
26
			'Failed with ' . $this->getDescriptiveJsonError( json_last_error() ) . ' in ' . $file
27
		);
28
	}
29
30
	public function i18nFileProvider() {
31
32
		$provider = array();
33
34
		$files = $this->findFilesForExtension(
35
			$GLOBALS['wgMessagesDirs']['SummaryCards'],
36
			'json'
37
		);
38
39
		foreach ( $files as $file ) {
40
			$provider[] = array( $file );
41
		}
42
43
		return $provider;
44
	}
45
46
	private function findFilesForExtension( $path, $extension ) {
47
48
		$files = array();
49
50
		$directoryIterator = new \RecursiveDirectoryIterator(
51
			str_replace( array( '\\', '/' ), DIRECTORY_SEPARATOR, $path )
52
		);
53
54
		foreach ( new \RecursiveIteratorIterator( $directoryIterator ) as $fileInfo ) {
55
			if ( strtolower( substr( $fileInfo->getFilename(), -( strlen( $extension ) + 1 ) ) ) === ( '.' . $extension ) ) {
56
				$files[$fileInfo->getFilename()] = $fileInfo->getPathname();
57
			}
58
		}
59
60
		return $files;
61
	}
62
63
	private function getDescriptiveJsonError( $errorCode ) {
64
65
		$errorMessages = array(
66
			JSON_ERROR_NONE => '',
67
			JSON_ERROR_STATE_MISMATCH => 'Underflow or the modes mismatch, malformed JSON',
68
			JSON_ERROR_CTRL_CHAR => 'Unexpected control character found, possibly incorrectly encoded',
69
			JSON_ERROR_SYNTAX => 'Syntax error, malformed JSON',
70
			JSON_ERROR_UTF8   => 'Malformed UTF-8 characters, possibly incorrectly encoded',
71
			JSON_ERROR_DEPTH  => 'The maximum stack depth has been exceeded'
72
		);
73
74
		return $errorMessages[$errorCode];
75
	}
76
77
}
78