| 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() { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 |  |  |  | 
            
                        
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: