mediawiki-extensions-Echo/tests/FilteredSequentialIteratorTest.php
Erik Bernhardson 1667e25854 Use batch queries for users-watching-title
The new locateUsersWatchingTitle implementation could end up returning
thousands of users, currently on enwiki there are 25 titles with more
than 10k subscribed users and aprox 550 titles with more than 1k subscribed
users.

This switches the user collection to an iterator based implementation so that
we no longer need to have the entire users list at any one time.

Change-Id: I3d3fa9328f348bb48682d3658622952ce82d3925
2014-08-15 10:44:55 -07:00

105 lines
2.8 KiB
PHP

<?php
class FilteredSequentialIteratorTest extends MediaWikiTestCase {
public function testEchoCallbackIteratorDoesntBlowUp() {
$it = new EchoCallbackIterator(
new ArrayIterator( array( 1, 2, 3 ) ),
function( $num ) {
return "There were $num items";
}
);
foreach ( $it as $val ) {
$res[] = $val;
}
$expected = array( "There were 1 items", "There were 2 items", "There were 3 items" );
$this->assertEquals( $expected, $res, 'Basic iteration with callback applied' );
}
public static function echoFilteredSequentialIteratorProvider() {
$odd = function( $v ) { return $v & 1; };
$greaterThanFour = function( $v ) { return $v > 4; };
return array(
array(
'Empty object still works',
// expected result
array(),
// list of iterators/arrays/etc each containing users
array(),
// list of filters to apply on output
array(),
),
array(
'Basic iteration with one array and no filters',
// expected result
array( 1, 2, 3 ),
// list of iterators/arrays/etc each containing users
array( array( 1, 2, 3 ) ),
// list of filters to apply on output
array()
),
array(
'Basic iteration with one array and one filters',
// expected result
array( 1, 3 ),
// list of tierators/arrays/etc each containing users
array( array( 1, 2, 3 ) ),
// list of filters to apply on output
array( $odd ),
),
array(
'Iteration with multiple input arrays and no filters',
// expected result (iterators are run in parallel)
array( 1, 4, 2, 5, 3 ),
// list of tierators/arrays/etc each containing users
array( array( 1, 2, 3 ), array( 4, 5 ) ),
// list of filters to apply on output
array(),
),
array(
'Iteration with multiple input arrays and multiple filters',
// expected result
array( 5 ),
// list of tierators/arrays/etc each containing users
array( array( 1, 2 ), array( 3, 4 ), array( 5, 6 ) ),
// list of filters to apply on output
array( $odd, $greaterThanFour ),
),
array(
'Iteration with interspersed empty arrays',
// expected result
array( 1, 3, 2 ),
// list of tierators/arrays/etc each containing users
array( array(), array( 1, 2 ), array( 3 ), array() ),
// list of filters to apply on output
array(),
),
);
}
/**
* @dataProvider echoFilteredSequentialIteratorProvider
*/
public function testEchoFilteredSequentialIterator( $message, $expect, $userLists, $filters ) {
$notify = new EchoFilteredSequentialIterator;
foreach ( $userLists as $userList ) {
$notify->add( $userList );
}
foreach ( $filters as $filter ) {
$notify->addFilter( $filter );
}
$result = array();
foreach ( $notify as $value ) {
$result[] = $value;
}
$this->assertEquals( $expect, $result, $message );
}
}