Completed
Push — master ( 3fbb63...83072e )
by mw
39:37 queued 06:51
created

tests/phpunit/includes/DefinesTest.php (2 issues)

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
3
namespace SMW\Test;
4
5
/**
6
 * Tests for global constants being loaded
7
 *
8
 * @since 1.9
9
 *
10
 * @file
11
 *
12
 * @licence GNU GPL v2+
13
 * @author mwjames
14
 */
15
16
/**
17
 * Tests for global constants being loaded
18
 *
19
 *
20
 * @group SMW
21
 * @group SMWExtension
22
 */
23
class DefinesTest extends  SemanticMediaWikiTestCase {
0 ignored issues
show
Expected 1 space before "SemanticMediaWikiTestCase"; 2 found
Loading history...
24
25
	/**
26
	 * Returns the name of the class to be tested
27
	 *
28
	 * @return string|boolean
29
	 */
30
	public function getClass() {
31
		return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type declared by the abstract method SMW\Test\SemanticMediaWikiTestCase::getClass of type string.

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...
32
	}
33
34
	/**
35
	 * Provides sample of constants to be tested
36
	 *
37
	 * @return array
38
	 */
39
	public function getConstantsDataProvider() {
40
		return array(
41
			array( SMW_HEADERS_SHOW, 2 ),
42
			array( SMW_HEADERS_PLAIN, 1 ),
43
			array( SMW_HEADERS_HIDE, 0 ),
44
			array( SMW_OUTPUT_HTML, 1 ),
45
			array( SMW_OUTPUT_WIKI, 2 ),
46
			array( SMW_OUTPUT_FILE, 3 ),
47
			array( SMW_FACTBOX_HIDDEN, 1 ),
48
			array( SMW_FACTBOX_SPECIAL, 2 ),
49
			array( SMW_FACTBOX_NONEMPTY, 3 ),
50
			array( SMW_FACTBOX_SHOWN, 5 ),
51
		);
52
	}
53
54
	/**
55
	 * Test if constants are accessible
56
	 * @dataProvider getConstantsDataProvider
57
	 *
58
	 * @param $constant
59
	 * @param $expected
60
	 */
61
	public function testConstants( $constant, $expected ) {
62
		$this->assertEquals( $expected, $constant );
63
	}
64
}
65