1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wikibase\DataModel\Services\Tests\Statement\Grouper; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
use Wikibase\DataModel\Services\Statement\Grouper\FilteringStatementGrouper; |
8
|
|
|
use Wikibase\DataModel\Snak\PropertyNoValueSnak; |
9
|
|
|
use Wikibase\DataModel\Statement\Statement; |
10
|
|
|
use Wikibase\DataModel\Statement\StatementFilter; |
11
|
|
|
use Wikibase\DataModel\Statement\StatementList; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @covers \Wikibase\DataModel\Services\Statement\Grouper\FilteringStatementGrouper |
15
|
|
|
* |
16
|
|
|
* @license GPL-2.0-or-later |
17
|
|
|
* @author Thiemo Kreuz |
18
|
|
|
*/ |
19
|
|
|
class FilteringStatementGrouperTest extends TestCase { |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param string $propertyId |
23
|
|
|
* |
24
|
|
|
* @return StatementFilter |
25
|
|
|
*/ |
26
|
|
|
private function newStatementFilter( $propertyId = 'P1' ) { |
27
|
|
|
$filter = $this->createMock( StatementFilter::class ); |
28
|
|
|
|
29
|
|
|
$filter->expects( $this->any() ) |
30
|
|
|
->method( 'statementMatches' ) |
31
|
|
|
->will( $this->returnCallback( function( Statement $statement ) use ( $propertyId ) { |
32
|
|
|
return $statement->getPropertyId()->getSerialization() === $propertyId; |
33
|
|
|
} ) ); |
34
|
|
|
|
35
|
|
|
return $filter; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testConstructorThrowsException() { |
39
|
|
|
$this->expectException( InvalidArgumentException::class ); |
40
|
|
|
new FilteringStatementGrouper( [ 'invalid' ] ); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testDoesNotAcceptTwoDefaults() { |
44
|
|
|
$this->expectException( InvalidArgumentException::class ); |
45
|
|
|
new FilteringStatementGrouper( [ 'default1' => null, 'default2' => null ] ); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function testDefaultGroupIsAlwaysThere() { |
49
|
|
|
$grouper = new FilteringStatementGrouper( [] ); |
50
|
|
|
$groups = $grouper->groupStatements( new StatementList() ); |
51
|
|
|
|
52
|
|
|
$this->assertArrayHasKey( 'statements', $groups ); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testCanOverrideDefaultGroup() { |
56
|
|
|
$grouper = new FilteringStatementGrouper( [ |
57
|
|
|
'default' => null, |
58
|
|
|
] ); |
59
|
|
|
$groups = $grouper->groupStatements( new StatementList() ); |
60
|
|
|
|
61
|
|
|
$this->assertArrayHasKey( 'default', $groups ); |
62
|
|
|
$this->assertArrayNotHasKey( 'statements', $groups ); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testAllGroupsAreAlwaysThere() { |
66
|
|
|
$grouper = new FilteringStatementGrouper( [ |
67
|
|
|
'p1' => $this->newStatementFilter(), |
68
|
|
|
] ); |
69
|
|
|
$groups = $grouper->groupStatements( new StatementList() ); |
70
|
|
|
|
71
|
|
|
$this->assertArrayHasKey( 'p1', $groups ); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function testDefaultGroupIsLast() { |
75
|
|
|
$statements = new StatementList(); |
76
|
|
|
$statements->addNewStatement( new PropertyNoValueSnak( 1 ) ); |
77
|
|
|
|
78
|
|
|
$grouper = new FilteringStatementGrouper( [ |
79
|
|
|
'p1' => $this->newStatementFilter(), |
80
|
|
|
] ); |
81
|
|
|
$groups = $grouper->groupStatements( $statements ); |
82
|
|
|
|
83
|
|
|
$this->assertSame( [ 'p1', 'statements' ], array_keys( $groups ) ); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function testCanOverrideDefaultGroupPosition() { |
87
|
|
|
$statements = new StatementList(); |
88
|
|
|
$statements->addNewStatement( new PropertyNoValueSnak( 1 ) ); |
89
|
|
|
|
90
|
|
|
$grouper = new FilteringStatementGrouper( [ |
91
|
|
|
'statements' => null, |
92
|
|
|
'p1' => $this->newStatementFilter(), |
93
|
|
|
] ); |
94
|
|
|
$groups = $grouper->groupStatements( $statements ); |
95
|
|
|
|
96
|
|
|
$this->assertSame( [ 'statements', 'p1' ], array_keys( $groups ) ); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function testCanRepurposeDefaultGroup() { |
100
|
|
|
$statements = new StatementList(); |
101
|
|
|
$statements->addNewStatement( new PropertyNoValueSnak( 1 ) ); |
102
|
|
|
|
103
|
|
|
$grouper = new FilteringStatementGrouper( [ |
104
|
|
|
'statements' => $this->newStatementFilter(), |
105
|
|
|
] ); |
106
|
|
|
$groups = $grouper->groupStatements( $statements ); |
107
|
|
|
|
108
|
|
|
$this->assertCount( 1, $groups ); |
|
|
|
|
109
|
|
|
$this->assertArrayHasKey( 'statements', $groups ); |
110
|
|
|
$this->assertCount( 1, $groups['statements'] ); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function testGroupStatements() { |
114
|
|
|
$statement1 = new Statement( new PropertyNoValueSnak( 1 ) ); |
115
|
|
|
$statement2 = new Statement( new PropertyNoValueSnak( 2 ) ); |
116
|
|
|
$statement3 = new Statement( new PropertyNoValueSnak( 1 ) ); |
117
|
|
|
$statements = new StatementList( $statement1, $statement2, $statement3 ); |
118
|
|
|
|
119
|
|
|
$grouper = new FilteringStatementGrouper( [ |
120
|
|
|
'p1' => $this->newStatementFilter( 'P1' ), |
121
|
|
|
'p2' => $this->newStatementFilter( 'P2' ), |
122
|
|
|
'p3' => $this->newStatementFilter( 'P3' ), |
123
|
|
|
] ); |
124
|
|
|
|
125
|
|
|
$expected = [ |
126
|
|
|
'p1' => new StatementList( $statement1, $statement3 ), |
127
|
|
|
'p2' => new StatementList( $statement2 ), |
128
|
|
|
'p3' => new StatementList(), |
129
|
|
|
'statements' => new StatementList(), |
130
|
|
|
]; |
131
|
|
|
|
132
|
|
|
$groups = $grouper->groupStatements( $statements ); |
133
|
|
|
$this->assertEquals( $expected, $groups, 'first call' ); |
134
|
|
|
|
135
|
|
|
$groups = $grouper->groupStatements( $statements ); |
136
|
|
|
$this->assertEquals( $expected, $groups, 'second call' ); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
} |
140
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: