SimpleItemBuilderTest   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 14

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 14
dl 0
loc 125
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A newItem() 0 12 1
A newFingerprint() 0 14 1
A newSiteLinks() 0 8 1
A addStatements() 0 11 1
A testSerializationForDe() 0 18 1
A buildNewSimpleItemForLanguage() 0 12 1
A getSimpleStatement() 0 7 1
A testSerializationForEn() 0 16 1
A testSerializationForNl() 0 15 1
1
<?php
2
3
namespace Tests\Queryr\Resources\Builders;
4
5
use DataValues\StringValue;
6
use Queryr\Resources\Builders\SimpleItemBuilder;
7
use Queryr\Resources\Builders\SimpleStatementsBuilder;
8
use Queryr\Resources\SimpleItem;
9
use Queryr\Resources\SimpleStatement;
10
use Wikibase\DataModel\Entity\Item;
11
use Wikibase\DataModel\Entity\PropertyId;
12
use Wikibase\DataModel\SiteLinkList;
13
use Wikibase\DataModel\Snak\PropertyNoValueSnak;
14
use Wikibase\DataModel\Snak\PropertyValueSnak;
15
use Wikibase\DataModel\Statement\Statement;
16
use Wikibase\DataModel\Term\Fingerprint;
17
18
/**
19
 * @covers Queryr\Resources\Builders\SimpleItemBuilder
20
 *
21
 * @licence GNU GPL v2+
22
 * @author Jeroen De Dauw < [email protected] >
23
 */
24
class SimpleItemBuilderTest extends \PHPUnit_Framework_TestCase {
25
26
	private function newItem() {
27
		$item = new Item();
28
29
		$item->setId( 1337 );
30
31
		$item->setFingerprint( $this->newFingerprint() );
32
		$item->setSiteLinkList( $this->newSiteLinks() );
33
34
		$this->addStatements( $item );
35
36
		return $item;
37
	}
38
39
	private function newFingerprint() {
40
		$fingerprint = new Fingerprint();
41
42
		$fingerprint->setLabel( 'en', 'foo' );
43
		$fingerprint->setLabel( 'de', 'bar' );
44
		$fingerprint->setLabel( 'nl', 'baz' );
45
46
		$fingerprint->setDescription( 'de', 'de description' );
47
48
		$fingerprint->setAliasGroup( 'en', [ 'first en alias', 'second en alias' ] );
49
		$fingerprint->setAliasGroup( 'de', [ 'first de alias', 'second de alias' ] );
50
51
		return $fingerprint;
52
	}
53
54
	private function newSiteLinks() {
55
		$links = new SiteLinkList();
56
57
		$links->addNewSiteLink( 'enwiki', 'En Page' );
58
		$links->addNewSiteLink( 'dewiki', 'De Page' );
59
60
		return $links;
61
	}
62
63
	private function addStatements( Item $item ) {
64
		$statement = new Statement( new PropertyValueSnak( 42, new StringValue( 'kittens' ) ) );
65
		$statement->setGuid( 'first guid' );
66
67
		$item->getStatements()->addStatement( $statement );
68
69
		$statement = new Statement( new PropertyNoValueSnak( 23 ) );
70
		$statement->setGuid( 'second guid' );
71
72
		$item->getStatements()->addStatement( $statement );
73
	}
74
75
	public function testSerializationForDe() {
76
		$simpleItem = $this->buildNewSimpleItemForLanguage( 'de' );
77
78
		$expected = new SimpleItem();
79
		$expected->ids = [
0 ignored issues
show
Documentation Bug introduced by
It seems like array('wikidata' => 'Q13...ikipedia' => 'De Page') of type array<string,string,{"wi...e_wikipedia":"string"}> is incompatible with the declared type array<integer,string> of property $ids.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
80
			'wikidata' => 'Q1337',
81
			'en_wikipedia' => 'En Page',
82
			'de_wikipedia' => 'De Page',
83
		];
84
85
		$expected->label = 'bar';
86
		$expected->description = 'de description';
87
		$expected->aliases = [ 'first de alias', 'second de alias' ];
88
89
		$expected->statements = [ $this->getSimpleStatement() ];
90
91
		$this->assertEquals( $expected, $simpleItem );
92
	}
93
94
	private function buildNewSimpleItemForLanguage( $languageCode ) {
95
		$labelLookup = $this->getMock( 'Queryr\Resources\Builders\ResourceLabelLookup' );
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0; use createMock() or getMockBuilder() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
96
97
		$labelLookup->expects( $this->any() )
98
			->method( 'getLabelByIdAndLanguage' )
99
			->will( $this->returnValue( 'awesome label' ) );
100
101
		$statementsBuilder = new SimpleStatementsBuilder( $languageCode, $labelLookup );
102
		$itemBuilder = new SimpleItemBuilder( $languageCode, $statementsBuilder );
103
104
		return $itemBuilder->buildFromItem( $this->newItem() );
105
	}
106
107
	private function getSimpleStatement() {
108
		return SimpleStatement::newInstance()
109
			->withPropertyName( 'awesome label' )
110
			->withPropertyId( new PropertyId( 'P42' ) )
111
			->withType( 'string' )
112
			->withValues( [ new StringValue( 'kittens' ) ] );
113
	}
114
115
	public function testSerializationForEn() {
116
		$simpleItem = $this->buildNewSimpleItemForLanguage( 'en' );
117
118
		$expected = new SimpleItem();
119
		$expected->ids = [
0 ignored issues
show
Documentation Bug introduced by
It seems like array('wikidata' => 'Q13...ikipedia' => 'En Page') of type array<string,string,{"wi...n_wikipedia":"string"}> is incompatible with the declared type array<integer,string> of property $ids.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
120
			'wikidata' => 'Q1337',
121
			'en_wikipedia' => 'En Page',
122
		];
123
124
		$expected->label = 'foo';
125
		$expected->aliases = [ 'first en alias', 'second en alias' ];
126
127
		$expected->statements = [ $this->getSimpleStatement() ];
128
129
		$this->assertEquals( $expected, $simpleItem );
130
	}
131
132
	public function testSerializationForNl() {
133
		$simpleItem = $this->buildNewSimpleItemForLanguage( 'nl' );
134
135
		$expected = new SimpleItem();
136
		$expected->ids = [
0 ignored issues
show
Documentation Bug introduced by
It seems like array('wikidata' => 'Q13...ikipedia' => 'En Page') of type array<string,string,{"wi...n_wikipedia":"string"}> is incompatible with the declared type array<integer,string> of property $ids.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
137
			'wikidata' => 'Q1337',
138
			'en_wikipedia' => 'En Page',
139
		];
140
141
		$expected->label = 'baz';
142
143
		$expected->statements = [ $this->getSimpleStatement() ];
144
145
		$this->assertEquals( $expected, $simpleItem );
146
	}
147
148
}
149