SimpleStatementsBuilderTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testBuildFromSingleStatementWithPropertyValueSnak() 0 12 1
A assertBuildsFrom() 0 12 1
A testEntityIdValueGetsSimplified() 0 12 1
A testLabelLookupFallsBackToId() 0 21 1
1
<?php
2
3
namespace Tests\Queryr\Resources\Builders;
4
5
use DataValues\StringValue;
6
use Queryr\Resources\Builders\SimpleStatementsBuilder;
7
use Queryr\Resources\SimpleStatement;
8
use Wikibase\DataModel\Entity\EntityIdValue;
9
use Wikibase\DataModel\Entity\ItemId;
10
use Wikibase\DataModel\Entity\PropertyId;
11
use Wikibase\DataModel\Snak\PropertyValueSnak;
12
use Wikibase\DataModel\Statement\Statement;
13
use Wikibase\DataModel\Statement\StatementList;
14
15
/**
16
 * @covers Queryr\Resources\Builders\SimpleStatementsBuilder
17
 *
18
 * @licence GNU GPL v2+
19
 * @author Jeroen De Dauw < [email protected] >
20
 */
21
class SimpleStatementsBuilderTest extends \PHPUnit_Framework_TestCase {
22
23
	public function testBuildFromSingleStatementWithPropertyValueSnak() {
24
		$statement = new Statement( new PropertyValueSnak( 42, new StringValue( 'kittens' ) ) );
25
		$statement->setGuid( 'first guid' );
26
27
		$expected = SimpleStatement::newInstance()
28
			->withPropertyName( 'awesome label' )
29
			->withPropertyId( new PropertyId( 'P42' ) )
30
			->withType( 'string' )
31
			->withValues( [ new StringValue( 'kittens' ) ] );
32
33
		$this->assertBuildsFrom( [ $statement ], [ $expected ] );
34
	}
35
36
	private function assertBuildsFrom( array $statements, array $expected ) {
37
		$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...
38
39
		$labelLookup->expects( $this->any() )
40
			->method( 'getLabelByIdAndLanguage' )
41
			->will( $this->returnValue( 'awesome label' ) );
42
43
		$builder = new SimpleStatementsBuilder( 'en', $labelLookup );
44
		$simpleStatements = $builder->buildFromStatements( new StatementList( $statements ) );
45
46
		$this->assertEquals( $expected, $simpleStatements );
47
	}
48
49
	public function testEntityIdValueGetsSimplified() {
50
		$statement = new Statement( new PropertyValueSnak( 42, new EntityIdValue( new ItemId( 'Q1337' ) ) ) );
51
		$statement->setGuid( 'first guid' );
52
53
		$expected = SimpleStatement::newInstance()
54
			->withPropertyName( 'awesome label' )
55
			->withPropertyId( new PropertyId( 'P42' ) )
56
			->withType( 'string' )
57
			->withValues( [ new StringValue( 'awesome label' ) ] );
58
59
		$this->assertBuildsFrom( [ $statement ], [ $expected ] );
60
	}
61
62
	public function testLabelLookupFallsBackToId() {
63
		$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...
64
65
		$labelLookup->expects( $this->any() )
66
			->method( 'getLabelByIdAndLanguage' )
67
			->will( $this->returnValue( null ) );
68
69
		$statement = new Statement( new PropertyValueSnak( 42, new EntityIdValue( new ItemId( 'Q1337' ) ) ) );
70
		$statement->setGuid( 'first guid' );
71
72
		$builder = new SimpleStatementsBuilder( 'en', $labelLookup );
73
		$simpleStatements = $builder->buildFromStatements( new StatementList( [ $statement ] ) );
74
75
		$expected = SimpleStatement::newInstance()
76
			->withPropertyName( 'P42' )
77
			->withPropertyId( new PropertyId( 'P42' ) )
78
			->withType( 'string' )
79
			->withValues( [ new StringValue( 'Q1337' ) ] );
80
81
		$this->assertEquals( [ $expected ], $simpleStatements );
82
	}
83
84
}
85