| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace SRF\Test; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use SMW\Test\QueryPrinterRegistryTestCase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use SMW\Tests\Utils\Mock\CoreMockObjectRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use SMW\Tests\Utils\Mock\MockObjectBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use SMWQueryProcessor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use SRF\Formats\Tree\TreeResultPrinter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * Class TreeTest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @since 2.5 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * @ingroup SemanticResultFormats | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @ingroup Test | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * @group SRF | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * @group SMWExtension | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * @group ResultPrinters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * @author Stephan Gambke | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | class TreeTest extends QueryPrinterRegistryTestCase { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 	private $parser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 	private $title; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 	 * Returns the names of the formats being tested. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 	 * @return string[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 	public function getFormats() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 		return [ 'tree' ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 	 * Returns the name of the class being tested. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 	 * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 	public function getClass() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 		return '\SRF\Formats\Tree\TreeResultPrinter'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	 * @covers \SRF\Formats\Tree\TreeResultPrinter::getResult() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 	public function testGetResult_NoParentProperty() { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 		$this->prepareGlobalState(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 		$mockBuilder = new MockObjectBuilder(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 		$mockBuilder->registerRepository( new CoreMockObjectRepository() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 		/** @var \PHPUnit_Framework_MockObject_MockObject $queryResult */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 		$queryResult = $mockBuilder->newObject( 'QueryResult', [ 'getCount' => 1 ] ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 		$queryResult->expects( $this->once() ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 			->method( 'addErrors' ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 			->will( $this->returnValue( null ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 		$params = SMWQueryProcessor::getProcessedParams( [ 'format' => 'tree' ], [] ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 		$testObject = new TreeResultPrinter( 'tree' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 		$this->assertEquals( '', $testObject->getResult( $queryResult, $params, SMW_OUTPUT_HTML ), 'Result should be empty.' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 		// Restore GLOBAL state to ensure that preceding tests do not use a | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 		// mocked instance | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 		$GLOBALS[ 'wgParser' ] = $this->parser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 		$GLOBALS[ 'wgTitle' ] = $this->title; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  | 	protected function prepareGlobalState() { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  | 		// Store current state | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  | 		$this->parser = $GLOBALS[ 'wgParser' ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  | 		$this->title = $GLOBALS[ 'wgTitle' ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  | 		$parserOutput = $this->getMockBuilder( '\ParserOutput' ) | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  | 			->disableOriginalConstructor() | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  | 			->getMock(); | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  | 		$parserOutput->expects( $this->any() ) | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  | 			->method( 'getHeadItems' ) | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  | 			->will( $this->returnValue( [] ) ); | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  | 		$parser = $this->getMockBuilder( '\Parser' ) | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  | 			->disableOriginalConstructor() | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  | 			->getMock(); | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  | 		$parser->expects( $this->any() ) | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  | 			->method( 'parse' ) | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  | 			->will( $this->returnValue( $parserOutput ) ); | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  | 		$title = $this->getMockBuilder( '\Title' ) | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  | 			->disableOriginalConstructor() | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  | 			->getMock(); | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  | 		// Careful!! | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  | 		$GLOBALS[ 'wgParser' ] = $parser; | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  | 		$GLOBALS[ 'wgTitle' ] = $title; | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 	 * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 	protected function provideQueryParamsAndResults() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 		$mockBuilder = new MockObjectBuilder(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 		$mockBuilder->registerRepository( new CoreMockObjectRepository() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 		/** @var \SMWResultArray[]|false $resultRow */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 		$resultRow = $mockBuilder->newObject( 'ResultArray' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 		//$resultRow->add( $resultCell ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 		$resultSet[] = []; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 		$resultSet[] = $resultRow; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 		/** @var array(SMWResultArray[]|false) $resultSet */ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 		$resultSet[] = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 		$queryResult = $mockBuilder->newObject( 'QueryResult', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 			'getCount' => 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 		] ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 		$queryResult->expects( $this->any() ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 			->method( 'getNext' ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 			->will( call_user_func( [ $this, 'onConsecutiveCalls' ], $resultSet ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 		$queryResult = $mockBuilder->newObject( 'QueryResult', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 			'getCount' => 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 		] ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 		$params = SMWQueryProcessor::getProcessedParams( [ 'format' => 'tree' ], [] ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 		$expected = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 		return [ $queryResult, $params, $expected ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 145 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 146 |  |  |  | 
            
                        
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: