Completed
Push — master ( 59ef79...0173b4 )
by mw
10s
created

I18nJsonFileIntegrityTest::findFilesForExtension()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
rs 9.4285
cc 3
eloc 8
nc 3
nop 2
1
<?php
2
3
namespace WNBY\Tests\Integration;
4
5
/**
6
 * @group whats-nearby
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() {
0 ignored issues
show
Coding Style introduced by
i18nFileProvider uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
31
32
		$provider = array();
33
34
		$files = $this->findFilesForExtension(
35
			$GLOBALS['wgMessagesDirs']['whats-nearby'],
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