Completed
Push — master ( 6dc7d8...407c40 )
by Karsten
15:45
created

JSONScript/JsonTestCaseScriptRunnerTest.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace SRF\Tests\Integration\JSONScript;
3
use SMW\Tests\Integration\JSONScript\JsonTestCaseScriptRunnerTest as SMWJsonTestCaseScriptRunnerTest;
4
/**
5
 * @see https://github.com/SemanticMediaWiki/SemanticMediaWiki/tree/master/tests#write-integration-tests-using-json-script
6
 *
7
 * `JsonTestCaseScriptRunner` provisioned by SMW is a base class allowing to use a JSON
8
 * format to create test definitions with the objective to compose "real" content
9
 * and test integration with MediaWiki, Semantic MediaWiki, and Scribunto.
10
 *
11
 * @group SRF
12
 * @group SMWExtension
13
 *
14
 * @license GNU GPL v2+
15
 * @since 2.5
16
 *
17
 * @author Stephan Gambke
18
 */
19
class JsonTestCaseScriptRunnerTest extends SMWJsonTestCaseScriptRunnerTest {
20
	/**
21
	 * @see \SMW\Tests\JsonTestCaseScriptRunner::getTestCaseLocation
22
	 * @return string
23
	 */
24
	protected function getTestCaseLocation() {
25
		return __DIR__ . '/TestCases';
26
	}
27
	/**
28
	 * @return string[]
29
	 * @since 3.0
30
	 */
31
	protected function getPermittedSettings() {
32
		$settings = parent::getPermittedSettings();
33
		$settings[] = 'srfgMapProvider';
34
		return $settings;
35
	}
36
37
	/**
38
	 * @see JsonTestCaseScriptRunner::getDependencyDefinitions
39
	 */
40
	protected function getDependencyDefinitions() {
41
		return [
0 ignored issues
show
Bug Best Practice introduced by sebastianschmid
The return type of return array('Mermaid' =...ckMermaidDependency')); (array<string,array<SRF\T...riptRunnerTest|string>>) is incompatible with the return type of the parent method SMW\Tests\Integration\JS...etDependencyDefinitions of type array<string,Closure>.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
42
			'Mermaid' => [ $this, 'checkMermaidDependency' ]
43
		];
44
	}
45
	public function checkMermaidDependency( $val, &$reason ) {
46
47
		if ( !defined( 'MERMAID_VERSION' ) ) {
48
			$reason = "Dependency: Mermaid as requirement is not available!";
49
			return false;
50
		}
51
52
		list( $compare, $requiredVersion ) = explode( ' ', $val );
53
		$version = MERMAID_VERSION;
54
55
		if ( !version_compare( $version, $requiredVersion, $compare ) ) {
56
			$reason = "Dependency: Required version of Mermaid($requiredVersion $compare $version) is not available!";
57
			return false;
58
		}
59
60
		return true;
61
	}
62
63
}