Merge "FauxRequest: don’t override getValues()"
[lhc/web/wiklou.git] / tests / phpunit / unit / includes / search / SearchSuggestionSetTest.php
1 <?php
2
3 /**
4 * Test for filter utilities.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 */
21
22 class SearchSuggestionSetTest extends \MediaWikiUnitTestCase {
23 /**
24 * Test that adding a new suggestion at the end
25 * will keep proper score ordering
26 * @covers SearchSuggestionSet::append
27 */
28 public function testAppend() {
29 $set = SearchSuggestionSet::emptySuggestionSet();
30 $this->assertSame( 0, $set->getSize() );
31 $set->append( new SearchSuggestion( 3 ) );
32 $this->assertEquals( 3, $set->getWorstScore() );
33 $this->assertEquals( 3, $set->getBestScore() );
34
35 $suggestion = new SearchSuggestion( 4 );
36 $set->append( $suggestion );
37 $this->assertEquals( 2, $set->getWorstScore() );
38 $this->assertEquals( 3, $set->getBestScore() );
39 $this->assertEquals( 2, $suggestion->getScore() );
40
41 $suggestion = new SearchSuggestion( 2 );
42 $set->append( $suggestion );
43 $this->assertEquals( 1, $set->getWorstScore() );
44 $this->assertEquals( 3, $set->getBestScore() );
45 $this->assertEquals( 1, $suggestion->getScore() );
46
47 $scores = $set->map( function ( $s ) {
48 return $s->getScore();
49 } );
50 $sorted = $scores;
51 asort( $sorted );
52 $this->assertEquals( $sorted, $scores );
53 }
54
55 /**
56 * Test that adding a new best suggestion will keep proper score
57 * ordering
58 * @covers SearchSuggestionSet::getWorstScore
59 * @covers SearchSuggestionSet::getBestScore
60 * @covers SearchSuggestionSet::prepend
61 */
62 public function testInsertBest() {
63 $set = SearchSuggestionSet::emptySuggestionSet();
64 $this->assertSame( 0, $set->getSize() );
65 $set->prepend( new SearchSuggestion( 3 ) );
66 $this->assertEquals( 3, $set->getWorstScore() );
67 $this->assertEquals( 3, $set->getBestScore() );
68
69 $suggestion = new SearchSuggestion( 4 );
70 $set->prepend( $suggestion );
71 $this->assertEquals( 3, $set->getWorstScore() );
72 $this->assertEquals( 4, $set->getBestScore() );
73 $this->assertEquals( 4, $suggestion->getScore() );
74
75 $suggestion = new SearchSuggestion( 0 );
76 $set->prepend( $suggestion );
77 $this->assertEquals( 3, $set->getWorstScore() );
78 $this->assertEquals( 5, $set->getBestScore() );
79 $this->assertEquals( 5, $suggestion->getScore() );
80
81 $suggestion = new SearchSuggestion( 2 );
82 $set->prepend( $suggestion );
83 $this->assertEquals( 3, $set->getWorstScore() );
84 $this->assertEquals( 6, $set->getBestScore() );
85 $this->assertEquals( 6, $suggestion->getScore() );
86
87 $scores = $set->map( function ( $s ) {
88 return $s->getScore();
89 } );
90 $sorted = $scores;
91 asort( $sorted );
92 $this->assertEquals( $sorted, $scores );
93 }
94
95 /**
96 * @covers SearchSuggestionSet::shrink
97 */
98 public function testShrink() {
99 $set = SearchSuggestionSet::emptySuggestionSet();
100 for ( $i = 0; $i < 100; $i++ ) {
101 $set->append( new SearchSuggestion( 0 ) );
102 }
103 $set->shrink( 10 );
104 $this->assertEquals( 10, $set->getSize() );
105
106 $set->shrink( 0 );
107 $this->assertSame( 0, $set->getSize() );
108 }
109
110 // TODO: test for fromTitles
111 }