1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wikibase\DataModel\Services\Tests\Statement\Grouper; |
4
|
|
|
|
5
|
|
|
use PHPUnit_Framework_TestCase; |
6
|
|
|
use Wikibase\DataModel\Services\Statement\Grouper\FilteringStatementGrouper; |
7
|
|
|
use Wikibase\DataModel\Snak\PropertyNoValueSnak; |
8
|
|
|
use Wikibase\DataModel\Statement\Statement; |
9
|
|
|
use Wikibase\DataModel\Statement\StatementList; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @covers Wikibase\DataModel\Services\Statement\Grouper\FilteringStatementGrouper |
13
|
|
|
* |
14
|
|
|
* @licence GNU GPL v2+ |
15
|
|
|
* @author Thiemo Mättig |
16
|
|
|
*/ |
17
|
|
|
class FilteringStatementGrouperTest extends PHPUnit_Framework_TestCase { |
18
|
|
|
|
19
|
|
|
private function newStatementFilter( $propertyId = 'P1' ) { |
20
|
|
|
$filter = $this->getMock( 'Wikibase\DataModel\Statement\StatementFilter' ); |
21
|
|
|
|
22
|
|
|
$filter->expects( $this->any() ) |
23
|
|
|
->method( 'statementMatches' ) |
24
|
|
|
->will( $this->returnCallback( function( Statement $statement ) use ( $propertyId ) { |
25
|
|
|
return $statement->getPropertyId()->getSerialization() === $propertyId; |
26
|
|
|
} ) ); |
27
|
|
|
|
28
|
|
|
return $filter; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function testConstructorThrowsException() { |
32
|
|
|
$this->setExpectedException( 'InvalidArgumentException' ); |
33
|
|
|
new FilteringStatementGrouper( array( 'invalid' ) ); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function testDoesNotAcceptTwoDefaults() { |
37
|
|
|
$this->setExpectedException( 'InvalidArgumentException' ); |
38
|
|
|
new FilteringStatementGrouper( array( 'default1' => null, 'default2' => null ) ); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testDefaultGroupIsAlwaysThere() { |
42
|
|
|
$grouper = new FilteringStatementGrouper( array() ); |
43
|
|
|
$groups = $grouper->groupStatements( new StatementList() ); |
44
|
|
|
|
45
|
|
|
$this->assertArrayHasKey( 'statements', $groups ); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function testCanOverrideDefaultGroup() { |
49
|
|
|
$grouper = new FilteringStatementGrouper( array( |
50
|
|
|
'default' => null, |
51
|
|
|
) ); |
52
|
|
|
$groups = $grouper->groupStatements( new StatementList() ); |
53
|
|
|
|
54
|
|
|
$this->assertArrayHasKey( 'default', $groups ); |
55
|
|
|
$this->assertArrayNotHasKey( 'statements', $groups ); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testAllGroupsAreAlwaysThere() { |
59
|
|
|
$grouper = new FilteringStatementGrouper( array( |
60
|
|
|
'p1' => $this->newStatementFilter(), |
61
|
|
|
) ); |
62
|
|
|
$groups = $grouper->groupStatements( new StatementList() ); |
63
|
|
|
|
64
|
|
|
$this->assertArrayHasKey( 'p1', $groups ); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function testDefaultGroupIsLast() { |
68
|
|
|
$statements = new StatementList(); |
69
|
|
|
$statements->addNewStatement( new PropertyNoValueSnak( 1 ) ); |
70
|
|
|
|
71
|
|
|
$grouper = new FilteringStatementGrouper( array( |
72
|
|
|
'p1' => $this->newStatementFilter(), |
73
|
|
|
) ); |
74
|
|
|
$groups = $grouper->groupStatements( $statements ); |
75
|
|
|
|
76
|
|
|
$this->assertSame( array( 'p1', 'statements' ), array_keys( $groups ) ); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testCanOverrideDefaultGroupPosition() { |
80
|
|
|
$statements = new StatementList(); |
81
|
|
|
$statements->addNewStatement( new PropertyNoValueSnak( 1 ) ); |
82
|
|
|
|
83
|
|
|
$grouper = new FilteringStatementGrouper( array( |
84
|
|
|
'statements' => null, |
85
|
|
|
'p1' => $this->newStatementFilter(), |
86
|
|
|
) ); |
87
|
|
|
$groups = $grouper->groupStatements( $statements ); |
88
|
|
|
|
89
|
|
|
$this->assertSame( array( 'statements', 'p1' ), array_keys( $groups ) ); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function testCanRepurposeDefaultGroup() { |
93
|
|
|
$statements = new StatementList(); |
94
|
|
|
$statements->addNewStatement( new PropertyNoValueSnak( 1 ) ); |
95
|
|
|
|
96
|
|
|
$grouper = new FilteringStatementGrouper( array( |
97
|
|
|
'statements' => $this->newStatementFilter(), |
98
|
|
|
) ); |
99
|
|
|
$groups = $grouper->groupStatements( $statements ); |
100
|
|
|
|
101
|
|
|
$this->assertCount( 1, $groups ); |
102
|
|
|
$this->assertArrayHasKey( 'statements', $groups ); |
103
|
|
|
$this->assertCount( 1, $groups['statements'] ); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function testGroupStatements() { |
107
|
|
|
$statement1 = new Statement( new PropertyNoValueSnak( 1 ) ); |
108
|
|
|
$statement2 = new Statement( new PropertyNoValueSnak( 2 ) ); |
109
|
|
|
$statement3 = new Statement( new PropertyNoValueSnak( 1 ) ); |
110
|
|
|
$statements = new StatementList( $statement1, $statement2, $statement3 ); |
111
|
|
|
|
112
|
|
|
$grouper = new FilteringStatementGrouper( array( |
113
|
|
|
'p1' => $this->newStatementFilter( 'P1' ), |
114
|
|
|
'p2' => $this->newStatementFilter( 'P2' ), |
115
|
|
|
'p3' => $this->newStatementFilter( 'P3' ), |
116
|
|
|
) ); |
117
|
|
|
|
118
|
|
|
$expected = array( |
119
|
|
|
'p1' => new StatementList( $statement1, $statement3 ), |
120
|
|
|
'p2' => new StatementList( $statement2 ), |
121
|
|
|
'p3' => new StatementList(), |
122
|
|
|
'statements' => new StatementList(), |
123
|
|
|
); |
124
|
|
|
|
125
|
|
|
$groups = $grouper->groupStatements( $statements ); |
126
|
|
|
$this->assertEquals( $expected, $groups, 'first call' ); |
127
|
|
|
|
128
|
|
|
$groups = $grouper->groupStatements( $statements ); |
129
|
|
|
$this->assertEquals( $expected, $groups, 'second call' ); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
} |
133
|
|
|
|