Completed
Push — master ( fa7306...f812cd )
by mw
161:35 queued 126:30
created

testGetValueFormatterOnNonRegisteredFormatters()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 17
nc 1
nop 0
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace SMW\Tests\Services;
4
5
use SMW\Services\DataValueServiceFactory;
6
7
/**
8
 * @covers \SMW\Services\DataValueServiceFactory
9
 * @group semantic-mediawiki
10
 *
11
 * @license GNU GPL v2+
12
 * @since 2.5
13
 *
14
 * @author mwjames
15
 */
16
class DataValueServiceFactoryTest extends \PHPUnit_Framework_TestCase {
17
18
	private $containerBuilder;
19
20
	protected function setUp() {
21
		parent::setUp();
22
23
		$this->containerBuilder = $this->getMockBuilder( '\Onoi\CallbackContainer\ContainerBuilder' )
24
			->disableOriginalConstructor()
25
			->getMock();
26
	}
27
28
	public function testCanConstruct() {
29
30
		$this->assertInstanceOf(
31
			DataValueServiceFactory::class,
32
			new DataValueServiceFactory( $this->containerBuilder )
33
		);
34
	}
35
36
	public function testGetServiceFile() {
37
38
		$this->assertInternalType(
39
			'string',
40
			DataValueServiceFactory::SERVICE_FILE
41
		);
42
	}
43
44
	public function testNewDataValueByType() {
45
46
		$dataValue = $this->getMockBuilder( '\SMWDataValue' )
0 ignored issues
show
Unused Code introduced by
$dataValue is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
47
			->disableOriginalConstructor()
48
			->getMockForAbstractClass();
49
50
		$this->containerBuilder->expects( $this->once() )
51
			->method( 'isRegistered' )
52
			->with( $this->stringContains( DataValueServiceFactory::TYPE_INSTANCE . 'foo' ) )
53
			->will( $this->returnValue( true ) );
54
55
		$instance = new DataValueServiceFactory(
56
			$this->containerBuilder
57
		);
58
59
		$instance->newDataValueByType( 'foo', 'bar' );
60
	}
61
62
	public function testGetValueParser() {
63
64
		$dataValue = $this->getMockBuilder( '\SMWDataValue' )
65
			->disableOriginalConstructor()
66
			->getMockForAbstractClass();
67
68
		$this->containerBuilder->expects( $this->once() )
69
			->method( 'singleton' )
70
			->with( $this->stringContains( DataValueServiceFactory::TYPE_PARSER ) );
71
72
		$instance = new DataValueServiceFactory(
73
			$this->containerBuilder
74
		);
75
76
		$instance->getValueParser( $dataValue );
77
	}
78
79
	public function testGetValueFormatterOnRegisteredFormatters() {
80
81
		$dataValueFormatter = $this->getMockBuilder( '\SMW\DataValues\ValueFormatters\DataValueFormatter' )
82
			->disableOriginalConstructor()
83
			->getMockForAbstractClass();
84
85
		$dataValue = $this->getMockBuilder( '\SMWDataValue' )
86
			->disableOriginalConstructor()
87
			->getMockForAbstractClass();
88
89
		$this->containerBuilder->expects( $this->once() )
90
			->method( 'isRegistered' )
91
			->will( $this->returnValue( true ) );
92
93
		$this->containerBuilder->expects( $this->once() )
94
			->method( 'singleton' )
95
			->with( $this->stringContains( DataValueServiceFactory::TYPE_FORMATTER ) )
96
			->will( $this->returnValue( $dataValueFormatter ) );
97
98
		$instance = new DataValueServiceFactory(
99
			$this->containerBuilder
100
		);
101
102
		$instance->getValueFormatter( $dataValue );
103
	}
104
105
	public function testGetValueFormatterOnNonRegisteredFormatters() {
106
107
		$dataValueFormatter = $this->getMockBuilder( '\SMW\DataValues\ValueFormatters\DataValueFormatter' )
108
			->disableOriginalConstructor()
109
			->getMockForAbstractClass();
110
111
		$dataValue = $this->getMockBuilder( '\SMWDataValue' )
112
			->disableOriginalConstructor()
113
			->getMockForAbstractClass();
114
115
		$this->containerBuilder->expects( $this->once() )
116
			->method( 'isRegistered' )
117
			->will( $this->returnValue( false ) );
118
119
		$this->containerBuilder->expects( $this->atLeastOnce() )
120
			->method( 'singleton' )
121
			->with( $this->stringContains( DataValueServiceFactory::TYPE_FORMATTER ) )
122
			->will( $this->returnValue( $dataValueFormatter ) );
123
124
		$instance = new DataValueServiceFactory(
125
			$this->containerBuilder
126
		);
127
128
		$instance->getValueFormatter( $dataValue );
129
	}
130
131
}
132