| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace SMW\ImageCaption\Tests; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use SMW\ImageCaption\Hooks; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @covers \SMW\ImageCaption\Hooks | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @group semantic-image-caption | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @license GNU GPL v2+ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @since 1.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @author mwjames | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | class HooksTest extends \PHPUnit_Framework_TestCase { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 	public function testOnRegisterSchemaTypes() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 		$schemaTypes = $this->getMockBuilder( '\SMW\Schema\SchemaTypes' ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 			->disableOriginalConstructor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 			->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 		$schemaTypes->expects( $this->once() ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 			->method( 'registerSchemaType' ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 			->with( $this->stringContains( 'IMAGECAPTION_RULE_SCHEMA' ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 		$this->assertTrue( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 			Hooks::onRegisterSchemaTypes( $schemaTypes ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  | 	public function testOnImageBeforeProduceHTML() { | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  | 		$title = $this->getMockBuilder( '\Title' ) | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  | 			->disableOriginalConstructor() | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  | 			->getMock(); | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  | 		$title->expects( $this->any() ) | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  | 			->method( 'getNamespace' ) | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  | 			->will( $this->returnValue( NS_MAIN ) ); | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  | 		$file = $this->getMockBuilder( '\File' ) | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  | 			->disableOriginalConstructor() | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  | 			->getMock(); | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  | 		$file->expects( $this->any() ) | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  | 			->method( 'getTitle' ) | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  | 			->will( $this->returnValue( $title ) ); | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  | 		$parserOptions = $this->getMockBuilder( '\ParserOptions' ) | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  | 			->disableOriginalConstructor() | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  | 			->getMock(); | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  | 		$parserOptions->expects( $this->any() ) | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  | 			->method( 'getUserLang' ) | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  | 			->will( $this->returnValue( 'en' ) ); | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  | 		$parser = $this->getMockBuilder( '\Parser' ) | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  | 			->disableOriginalConstructor() | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  | 			->getMock(); | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  | 		$parser->expects( $this->any() ) | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  | 			->method( 'getTitle' ) | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  | 			->will( $this->returnValue( $title ) ); | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  | 		$parser->expects( $this->any() ) | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  | 			->method( 'getOptions' ) | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  | 			->will( $this->returnValue( $parserOptions ) ); | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  | 		$frameParams['caption'] = ''; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  | 		$handlerParams = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  | 		$time = ''; | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  | 		$res = null; | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  | 		$query = ''; | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  | 		$widthOption = ''; | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  | 		$this->assertTrue( | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  | 			Hooks::onImageBeforeProduceHTML( $dummy, $title, $file, $frameParams, $handlerParams, $time, $res, $parser, $query, $widthOption ) | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  | 		); | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 83 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 84 |  |  |  | 
            
                        
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.