Completed
Pull Request — master (#523)
by
unknown
08:32
created

newMockQueryResultWithLink()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace SRF\Tests\BibTex;
4
5
use PHPUnit\Framework\MockObject\MockObject;
6
use SMWInfolink;
7
use SMWQueryResult;
8
use SRF\BibTex\BibTexFileExportPrinter;
9
use SRF\Tests\ResultPrinterReflector;
10
11
/**
12
 * @covers \SRF\BibTex\BibTexFileExportPrinter
13
 * @group semantic-result-formats
14
 *
15
 * @license GNU GPL v2+
16
 * @since 3.1
17
 *
18
 * @author mwjames
19
 */
20
class BibTexFileExportPrinterTest extends \PHPUnit_Framework_TestCase {
21
22
	public function testCanConstruct() {
23
24
		$this->assertInstanceOf(
25
			BibTexFileExportPrinter::class,
26
			new BibTexFileExportPrinter( 'bibtex' )
27
		);
28
	}
29
30
	/**
31
	 * @dataProvider filenameProvider
32
	 */
33
	public function testGetFileName( $filename, $searchlabel, $expected ) {
34
35
		$parameters = [
36
			'filename' => $filename,
37
			'searchlabel' => $searchlabel
38
		];
39
40
		$bibTexPrinter = new BibTexFileExportPrinter( 'bibtex' );
41
42
		( new ResultPrinterReflector() )->addParameters( $bibTexPrinter, $parameters );
43
44
		$this->assertEquals(
45
			$expected,
46
			$bibTexPrinter->getFileName( $this->newQueryResultDummy() )
47
		);
48
	}
49
50
	public function filenameProvider() {
51
52
		yield[
53
			'',
54
			'',
55
			'BibTeX.bib'
56
		];
57
58
		yield[
59
			'',
60
			'foo bar',
61
			'foo_bar.bib'
62
		];
63
64
		yield[
65
			'foo',
66
			'',
67
			'foo.bib'
68
		];
69
70
		yield[
71
			'foo.bib',
72
			'',
73
			'foo.bib'
74
		];
75
76
		yield[
77
			'foo bar.bib',
78
			'',
79
			'foo_bar.bib'
80
		];
81
	}
82
83
	/**
84
	 * @return MockObject|SMWQueryResult
85
	 */
86
	private function newQueryResultDummy() {
87
		return $this->getMockBuilder( SMWQueryResult::class )
88
			->disableOriginalConstructor()
89
			->getMock();
90
	}
91
92
	public function testGetMimeType() {
93
94
		$instance = new BibTexFileExportPrinter(
95
			'bibtex'
96
		);
97
98
		$this->assertEquals(
99
			'text/bibtex',
100
			$instance->getMimeType( $this->newQueryResultDummy() )
101
		);
102
	}
103
104
	public function testGetResult_LinkOnNonFileOutput() {
105
		$bibTexPrinter = new BibTexFileExportPrinter(
106
			'bibtex'
107
		);
108
109
		$this->assertEquals(
110
			'foo_link',
111
			$bibTexPrinter->getResult(
112
				$this->newMockQueryResultWithLink(),
113
				[],
114
				SMW_OUTPUT_HTML
115
			)
116
		);
117
	}
118
119
	private function newMockQueryResultWithLink() {
120
		$queryResult = $this->newQueryResultDummy();
121
122
		$queryResult->expects( $this->any() )
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<SMWQueryResult>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
123
			->method( 'getErrors' )
124
			->will( $this->returnValue( [] ) );
125
126
		$queryResult->expects( $this->any() )
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<SMWQueryResult>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
127
			->method( 'getCount' )
128
			->will( $this->returnValue( 1 ) );
129
130
		$queryResult->expects( $this->once() )
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<SMWQueryResult>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
131
			->method( 'getQueryLink' )
132
			->will( $this->returnValue( $this->newInfoLinkStub() ) );
133
134
		return $queryResult;
135
	}
136
137
	private function newInfoLinkStub() {
138
		$link = $this->getMockBuilder( SMWInfolink::class )
139
			->disableOriginalConstructor()
140
			->getMock();
141
142
		$link->expects( $this->any() )
143
			->method( 'getText' )
144
			->will( $this->returnValue( 'foo_link' ) );
145
146
		return $link;
147
	}
148
149
}
150