Completed
Push — master ( f99eb7...4c2f46 )
by mw
50:24 queued 17:14
created

tests/phpunit/includes/NamespaceManagerTest.php (1 issue)

super-globals are not used.

Coding Style Minor

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
use SMW\NamespaceManager;
6
7
/**
8
 * @covers \SMW\NamespaceManager
9
 * @group semantic-mediawiki
10
 *
11
 * @license GNU GPL v2+
12
 * @since 1.9
13
 *
14
 * @author mwjames
15
 */
16
class NamespaceManagerTest extends \PHPUnit_Framework_TestCase {
17
18
	private function newInstance( &$test = array(), $langCode = 'en' ) {
19
20
		$default = array(
21
			'smwgNamespacesWithSemanticLinks' => array(),
22
			'wgNamespacesWithSubpages' => array(),
23
			'wgExtraNamespaces'  => array(),
24
			'wgNamespaceAliases' => array(),
25
			'wgLanguageCode'     => $langCode
26
		);
27
28
		$test = array_merge( $default, $test );
29
30
		$smwBasePath = __DIR__ . '../../../..';
31
32
		return new NamespaceManager( $test, $smwBasePath );
33
	}
34
35
	public function testCanConstruct() {
36
37
		$this->assertInstanceOf(
38
			'\SMW\NamespaceManager',
39
			$this->newInstance()
40
		);
41
	}
42
43
	public function testExecution() {
44
45
		$test = array();
46
47
		$this->newInstance( $test )->init();
48
49
		$this->assertNotEmpty(
50
			$test
51
		);
52
	}
53
54
	public function testExecutionWithIncompleteConfiguration() {
55
56
		$test = array(
57
			'wgExtraNamespaces'  => '',
58
			'wgNamespaceAliases' => ''
59
		);
60
61
		$this->newInstance( $test )->init();
62
63
		$this->assertNotEmpty(
64
			$test
65
		);
66
	}
67
68
	public function testExecutionWithLanguageFallback() {
69
70
		$test = array();
71
72
		$this->newInstance( $test, 'foo' )->init();
73
74
		$this->assertNotEmpty(
75
			$test
76
		);
77
	}
78
79
	public function testGetCanonicalNames() {
80
81
		$result = NamespaceManager::getCanonicalNames();
82
83
		$this->assertInternalType(
84
			'array',
85
			$result
86
		);
87
88
		$this->assertCount(
89
			4,
90
			$result
91
		);
92
	}
93
94
	public function testGetCanonicalNamesWithTypeNamespace() {
0 ignored issues
show
testGetCanonicalNamesWithTypeNamespace uses the super-global variable $GLOBALS which is generally not recommended.

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:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
95
96
		$GLOBALS['smwgHistoricTypeNamespace'] = true;
97
98
		$result = NamespaceManager::getCanonicalNames();
99
100
		$this->assertInternalType(
101
			'array',
102
			$result
103
		);
104
105
		$this->assertCount(
106
			6,
107
			$result
108
		);
109
110
		unset( $GLOBALS['smwgHistoricTypeNamespace'] );
111
	}
112
113
	public function testBuildNamespaceIndex() {
114
		$this->assertInternalType(
115
			'array',
116
			NamespaceManager::buildNamespaceIndex( 100 )
117
		);
118
	}
119
120
	public function testInitCustomNamespace() {
121
122
		$test = array();
123
		NamespaceManager::initCustomNamespace( $test );
124
125
		$this->assertNotEmpty( $test );
126
		$this->assertEquals(
127
			100,
128
			$test['smwgNamespaceIndex'],
129
			'Asserts that smwgNamespaceIndex is being set to a default index'
130
		);
131
	}
132
133
}
134