Completed
Push — bibtex-refactor ( 88494c...f5535a )
by mw
63:25 queued 43:27
created

ItemTest::testText()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
namespace SRF\Tests\BibTex;
4
5
use SRF\BibTex\Item;
6
7
/**
8
 * @covers \SRF\BibTex\Item
9
 * @group semantic-result-formats
10
 *
11
 * @license GNU GPL v2+
12
 * @since 3.1
13
 *
14
 * @author mwjames
15
 */
16
class ItemTest extends \PHPUnit_Framework_TestCase {
17
18
	public function testCanConstruct() {
19
20
		$this->assertInstanceOf(
21
			Item::class,
22
			new Item()
23
		);
24
	}
25
26
	/**
27
	 * @dataProvider fieldsProvider
28
	 */
29
	public function testText( $fields, $expected ) {
30
31
		$instance = new Item();
32
33
		foreach ( $fields as $key => $value ) {
34
			$instance->set( $key, $value );
35
		}
36
37
		$this->assertEquals(
38
			$expected,
39
			$instance->text()
40
		);
41
	}
42
43
	/**
44
	 * @dataProvider formatterCallbackFieldsProvider
45
	 */
46
	public function testFormatterCallback( $fields, $expected ) {
47
48
		$instance = new Item();
49
		$instance->setFormatterCallback( function( $key, $values ) {
50
			return implode( '#', $values );
51
		} );
52
53
		foreach ( $fields as $key => $value ) {
54
			$instance->set( $key, $value );
55
		}
56
57
		$this->assertEquals(
58
			$expected,
59
			$instance->text()
60
		);
61
	}
62
63
	/**
64
	 * @dataProvider replaceTextProvider
65
	 */
66
	public function testReplace( $key, $text, $expected ) {
67
68
		$instance = new Item();
69
70
		$this->assertEquals(
71
			$expected,
72
			$instance->replace( 'uri', $text )
73
		);
74
	}
75
76
	public function fieldsProvider() {
77
78
		yield [
79
			[ 'foo' => 'test', 'author' => [ 'abc', 'def', '123' ] ],
80
			"@Book{abc,
81
  author = \"abc, def, 123\", \r\n}"
82
		];
83
84
		yield [
85
			[ 'foo' => 'test', 'title' => 'foo bar', 'editor' => [ 'abc', 'def', '123' ] ],
86
			"@Book{fb,
87
  editor = \"abc, def, 123\", \r\n  title = \"foo bar\", \r\n}"
88
		];
89
	}
90
91
	public function formatterCallbackFieldsProvider() {
92
93
		yield [
94
			[ 'foo' => 'test', 'author' => [ 'abc', 'def', '123' ] ],
95
			"@Book{abc,
96
  author = \"abc#def#123\", \r\n}"
97
		];
98
99
		yield [
100
			[ 'foo' => 'test', 'title' => 'foo bar', 'editor' => [ 'abc', 'def', '123' ] ],
101
			"@Book{fb,
102
  editor = \"abc#def#123\", \r\n  title = \"foo bar\", \r\n}"
103
		];
104
	}
105
106
	public function replaceTextProvider() {
107
108
		yield [
109
			'uri',
110
			'abc-_+ÄäÖ',
111
			'abcAeaeOe'
112
		];
113
	}
114
115
}
116