aboutsummaryrefslogtreecommitdiffstats
path: root/tests/phpunit/unit/includes/Notification/NotificationBatchTest.php
blob: 4cda67741215cfb7fcc30346bf94dd650cc46e0f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php

namespace MediaWiki\Tests\Notification;

use MediaWiki\Notification\Notification;
use MediaWiki\Notification\NotificationEnvelope;
use MediaWiki\Notification\NotificationsBatch;
use MediaWiki\Notification\RecipientSet;
use MediaWikiUnitTestCase;
use stdClass;

/**
 * @covers \MediaWiki\Notification\NotificationsBatch
 * @covers \MediaWiki\Notification\NotificationEnvelope
 */
class NotificationBatchTest extends MediaWikiUnitTestCase {

	public function testAdds() {
		$first = new NotificationEnvelope( new Notification( 'first' ), new RecipientSet( [] ) );
		$second = new NotificationEnvelope( new Notification( 'second' ), new RecipientSet( [] ) );

		$batch = new NotificationsBatch( $first );
		$batch->add( $second );

		$this->assertCount( 2, $batch );
		$envelopesArray = iterator_to_array( $batch );
		$this->assertSame( $first, $envelopesArray[0] );
		$this->assertSame( $second, $envelopesArray[1] );
	}

	public function testRemoveWhenExists() {
		$first = new NotificationEnvelope( new Notification( 'first' ), new RecipientSet( [] ) );
		$second = new NotificationEnvelope( new Notification( 'second' ), new RecipientSet( [] ) );
		$third = new NotificationEnvelope( new Notification( 'third' ), new RecipientSet( [] ) );
		$last = new NotificationEnvelope( new Notification( 'fourth' ), new RecipientSet( [] ) );

		$batch = new NotificationsBatch( $first, $second, $third, $last );
		$this->assertCount( 4, $batch );

		$batch->remove( $second );
		$this->assertCount( 3, $batch );
		$envelopesArray = iterator_to_array( $batch );
		$this->assertSame( $first, $envelopesArray[0] );
		$this->assertSame( $third, $envelopesArray[1] );
		$this->assertSame( $last, $envelopesArray[2] );

		$batch->remove( $first );
		$this->assertCount( 2, $batch );
		$envelopesArray = iterator_to_array( $batch );
		$this->assertSame( $third, $envelopesArray[0] );
		$this->assertSame( $last, $envelopesArray[1] );

		$batch->remove( $last );
		$this->assertCount( 1, $batch );
		$envelopesArray = iterator_to_array( $batch );
		$this->assertSame( $third, $envelopesArray[0] );
	}

	public function testRemovesWhenNotExists() {
		$first = new NotificationEnvelope( new Notification( 'first' ), new RecipientSet( [] ) );
		$second = new NotificationEnvelope( new Notification( 'second' ), new RecipientSet( [] ) );
		$other = new NotificationEnvelope( new Notification( 'third' ), new RecipientSet( [] ) );

		$batch = new NotificationsBatch( $first, $second );
		$batch->remove( $other );

		$this->assertCount( 2, $batch );
		$envelopesArray = iterator_to_array( $batch );
		$this->assertSame( $first, $envelopesArray[0] );
		$this->assertSame( $second, $envelopesArray[1] );
	}

	public function testFilters() {
		$first = new NotificationEnvelope( new Notification( 'first' ), new RecipientSet( [] ) );
		$second = new NotificationEnvelope( new Notification( 'second' ), new RecipientSet( [] ) );

		$filterMock = $this->getMockBuilder( stdClass::class )
			->addMethods( [ '__invoke' ] )
			->getMock();

		$filterMock->expects( $this->exactly( 2 ) )
			->method( '__invoke' )
			->willReturnCallback( static function ( NotificationEnvelope $envelope ) use ( $first ) {
				return $envelope->equals( $first );
			} );

		$batch = new NotificationsBatch( $first, $second );
		$batch->filter( $filterMock );
		$this->assertCount( 1, $batch );
		$envelopesArray = iterator_to_array( $batch );
		$this->assertSame( $first, $envelopesArray[0] );
	}

}