Completed
Push — master ( 3c62d6...8f3f49 )
by mw
14s
created

LocalizerTest::testConvertDoubleWidth()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
3
namespace SMW\Tests;
4
5
use Language;
6
use SMW\Localizer;
7
8
/**
9
 * @covers \SMW\Localizer
10
 * @group semantic-mediawiki
11
 *
12
 * @license GNU GPL v2+
13
 * @since 2.1
14
 *
15
 * @author mwjames
16
 */
17
class LocalizerTest extends \PHPUnit_Framework_TestCase {
18
19
	public function testCanConstruct() {
20
21
		$language = $this->getMockBuilder( '\Language' )
22
			->disableOriginalConstructor()
23
			->getMock();
24
25
		$this->assertInstanceOf(
26
			'\SMW\Localizer',
27
			new Localizer( $language )
28
		);
29
30
		$this->assertInstanceOf(
31
			'\SMW\Localizer',
32
			Localizer::getInstance()
33
		);
34
	}
35
36
	public function testGetContentLanguage() {
0 ignored issues
show
Coding Style introduced by
testGetContentLanguage 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...
37
38
		$language = $this->getMockBuilder( '\Language' )
39
			->disableOriginalConstructor()
40
			->getMock();
41
42
		$instance = new Localizer( $language );
43
44
		$this->assertSame(
45
			$language,
46
			$instance->getContentLanguage()
47
		);
48
49
		$this->assertSame(
50
			$GLOBALS['wgContLang'],
51
			Localizer::getInstance()->getContentLanguage()
52
		);
53
54
		Localizer::clear();
55
	}
56
57
	public function testNamespaceTextById() {
58
59
		$instance = new Localizer( Language::factory( 'en') );
60
61
		$this->assertEquals(
62
			'Property',
63
			$instance->getNamespaceTextById( SMW_NS_PROPERTY )
64
		);
65
	}
66
67
	public function testNamespaceIndexByName() {
68
69
		$instance = new Localizer( Language::factory( 'en') );
70
71
		$this->assertEquals(
72
			SMW_NS_PROPERTY,
73
			$instance->getNamespaceIndexByName( 'property' )
74
		);
75
	}
76
77
	public function testSupportedLanguageForLowerCaseLetter() {
0 ignored issues
show
Coding Style introduced by
testSupportedLanguageForLowerCaseLetter 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...
78
79
		if ( version_compare( $GLOBALS['wgVersion'], '1.20', '<' ) ) {
80
			$this->markTestSkipped( 'Skipping because `Language::isSupportedLanguage` is not supported on 1.19' );
81
		}
82
83
		$this->assertTrue(
84
			Localizer::isSupportedLanguage( 'en' )
85
		);
86
	}
87
88
	public function testSupportedLanguageForUpperCaseLetter() {
0 ignored issues
show
Coding Style introduced by
testSupportedLanguageForUpperCaseLetter 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...
89
90
		if ( version_compare( $GLOBALS['wgVersion'], '1.20', '<' ) ) {
91
			$this->markTestSkipped( 'Skipping because `Language::isSupportedLanguage` is not supported on 1.19' );
92
		}
93
94
		$this->assertTrue(
95
			Localizer::isSupportedLanguage( 'ZH-HANS' )
96
		);
97
	}
98
99
	public function testAsBCP47FormattedLanguageCode() {
100
		$this->assertEquals(
101
			'zh-Hans',
102
			Localizer::asBCP47FormattedLanguageCode( 'zh-hans' )
103
		);
104
	}
105
106
	public function testCanGetLanguageCodeOnValidMarkedValue() {
107
108
		$value = 'Foo@en';
109
110
		$this->assertEquals(
111
			'en',
112
			Localizer::getLanguageCodeFrom( $value )
113
		);
114
115
		$this->assertEquals(
116
			'Foo',
117
			$value
118
		);
119
	}
120
121
	public function testCanGetLanguageCodeOnDoubledMarker() {
122
123
		$value = 'Foo@@en';
124
125
		$this->assertEquals(
126
			'en',
127
			Localizer::getLanguageCodeFrom( $value )
128
		);
129
130
		$this->assertEquals(
131
			'Foo@',
132
			$value
133
		);
134
	}
135
136
	public function testCanNotGetLanguageCodeOnNonMarkedValue() {
137
138
		$value = 'Fooen';
139
140
		$this->assertFalse(
141
			Localizer::getLanguageCodeFrom( $value )
142
		);
143
144
		$this->assertEquals(
145
			'Fooen',
146
			$value
147
		);
148
	}
149
150
	public function testCanNotGetLanguageCodeOnMissingLanguageCode() {
151
152
		$value = 'Foo@';
153
154
		$this->assertFalse(
155
			Localizer::getLanguageCodeFrom( $value )
156
		);
157
158
		$this->assertEquals(
159
			'Foo@',
160
			$value
161
		);
162
	}
163
164
	public function testExtraneousLanguage() {
165
166
		$this->assertInstanceOf(
167
			'\SMW\ExtraneousLanguage',
168
			Localizer::getInstance()->getExtraneousLanguage()
169
		);
170
171
		$this->assertInstanceOf(
172
			'\SMW\ExtraneousLanguage',
173
			Localizer::getInstance()->getExtraneousLanguage( 'en' )
174
		);
175
176
		Localizer::clear();
177
	}
178
179
	public function testConvertDoubleWidth() {
180
181
		$this->assertEquals(
182
			'2000',
183
			Localizer::convertDoubleWidth( '2000' )
184
		);
185
186
		$this->assertEquals(
187
			'aBc',
188
			Localizer::getInstance()->convertDoubleWidth( 'aBc' )
189
		);
190
	}
191
192
}
193