ImageCaptionTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 144
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 11 1
A testCanConstruct() 0 7 1
B testModifyCaption_EmptyCaption() 0 62 1
A testModifyCaption_PreventCaptionOverride() 0 54 1
1
<?php
2
3
namespace SMW\ImageCaption\Tests;
4
5
use SMW\ImageCaption\ImageCaption;
6
7
/**
8
 * @covers \SMW\ImageCaption\ImageCaption
9
 * @group semantic-image-caption
10
 *
11
 * @license GNU GPL v2+
12
 * @since 1.0
13
 *
14
 * @author mwjames
15
 */
16
class ImageCaptionTest extends \PHPUnit_Framework_TestCase {
17
18
	private $store;
19
	private $ruleFinder;
20
21
	protected function setUp() : void {
22
23
		$this->store = $this->getMockBuilder( '\SMW\Store' )
24
			->disableOriginalConstructor()
25
			->setMethods( [ 'getPropertyValues' ] )
26
			->getMockForAbstractClass();
27
28
		$this->ruleFinder = $this->getMockBuilder( '\SMW\ImageCaption\RuleFinder' )
29
			->disableOriginalConstructor()
30
			->getMock();
31
	}
32
33
	public function testCanConstruct() {
34
35
		$this->assertInstanceof(
36
			ImageCaption::class,
37
			new ImageCaption( $this->store, $this->ruleFinder )
38
		);
39
	}
40
41
	public function testModifyCaption_EmptyCaption() {
42
43
		$caption = '';
44
45
		$rule = $this->getMockBuilder( '\SMW\Schema\Rule' )
46
			->disableOriginalConstructor()
47
			->getMock();
48
49
		$rule->expects( $this->at( 1 ) )
50
			->method( 'has' )
51
			->with( $this->stringContains( 'then.caption_property' ) )
52
			->will( $this->returnValue( true ) );
53
54
		$rule->expects( $this->at( 2 ) )
55
			->method( 'then' )
56
			->with( $this->stringContains( 'caption_property' ) )
57
			->will( $this->returnValue( 'Foo' ) );
58
59
		$rule->expects( $this->at( 3 ) )
60
			->method( 'has' )
61
			->will( $this->returnValue( true ) );
62
63
		$rule->expects( $this->at( 4 ) )
64
			->method( 'then' )
65
			->with( $this->stringContains( 'max_length' ) );
66
67
		$this->ruleFinder->expects( $this->any() )
68
			->method( 'findRule' )
69
			->will( $this->returnValue( $rule ) );
70
71
		$this->store->expects( $this->any() )
72
			->method( 'getPropertyValues' )
73
			->will( $this->returnValue( [] ) );
74
75
		$title = $this->getMockBuilder( '\Title' )
76
			->disableOriginalConstructor()
77
			->getMock();
78
79
		$title->expects( $this->any() )
80
			->method( 'getNamespace' )
81
			->will( $this->returnValue( NS_MAIN ) );
82
83
		$file = $this->getMockBuilder( '\File' )
84
			->disableOriginalConstructor()
85
			->getMock();
86
87
		$file->expects( $this->any() )
88
			->method( 'getTitle' )
89
			->will( $this->returnValue( $title ) );
90
91
		$instance = new ImageCaption(
92
			$this->store,
93
			$this->ruleFinder
94
		);
95
96
		$instance->modifyCaption( $title, $file, $caption, 'en' );
97
98
		$this->assertEquals(
99
			'',
100
			$caption
101
		);
102
	}
103
104
	public function testModifyCaption_PreventCaptionOverride() {
105
106
		$caption = 'Foo';
107
108
		$rule = $this->getMockBuilder( '\SMW\Schema\Rule' )
109
			->disableOriginalConstructor()
110
			->getMock();
111
112
		$rule->expects( $this->at( 1 ) )
113
			->method( 'has' )
114
			->with( $this->stringContains( 'then.allow_caption_override' ) )
115
			->will( $this->returnValue( true ) );
116
117
		$rule->expects( $this->at( 2 ) )
118
			->method( 'then' )
119
			->with( $this->stringContains( 'allow_caption_override' ) )
120
			->will( $this->returnValue( false ) );
121
122
		$this->ruleFinder->expects( $this->any() )
123
			->method( 'findRule' )
124
			->will( $this->returnValue( $rule ) );
125
126
		$this->store->expects( $this->any() )
127
			->method( 'getPropertyValues' )
128
			->will( $this->returnValue( [] ) );
129
130
		$title = $this->getMockBuilder( '\Title' )
131
			->disableOriginalConstructor()
132
			->getMock();
133
134
		$title->expects( $this->any() )
135
			->method( 'getNamespace' )
136
			->will( $this->returnValue( NS_MAIN ) );
137
138
		$file = $this->getMockBuilder( '\File' )
139
			->disableOriginalConstructor()
140
			->getMock();
141
142
		$file->expects( $this->any() )
143
			->method( 'getTitle' )
144
			->will( $this->returnValue( $title ) );
145
146
		$instance = new ImageCaption(
147
			$this->store,
148
			$this->ruleFinder
149
		);
150
151
		$instance->modifyCaption( $title, $file, $caption, 'en' );
152
153
		$this->assertEquals(
154
			'Foo',
155
			$caption
156
		);
157
	}
158
159
}
160