Debian Codegraph @

tests/phpunit/unit/includes/watchlist/NoWriteWatchedItemStoreUnitTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\Title\TitleValue;
4 use MediaWiki\User\UserIdentityValue;
5 use MediaWiki\Watchlist\NoWriteWatchedItemStore;
6 use MediaWiki\Watchlist\WatchedItemStoreInterface;
7 use Wikimedia\Rdbms\DBReadOnlyError;
8
9 /**
10 * @author Addshore
11 *
12 * @covers \MediaWiki\Watchlist\NoWriteWatchedItemStore
13 */
14 class NoWriteWatchedItemStoreUnitTest extends \MediaWikiUnitTestCase {
15
16 /**
17 * @return NoWriteWatchedItemStore
18 */
19 private function getNoWriteStoreForErrors(): NoWriteWatchedItemStore {
20 // NoWriteWatchedItemStore where the inner actual store should never be called,
21 // because we are testing the methods that throw exceptions instead
22 // We could do a fancy constrant for never having a method that matches the
23 // specific list, but since we don't use this for the cases that we have the
24 // inner actual store do anything, it should never be used
25 $innerService = $this->createNoOpAbstractMock( WatchedItemStoreInterface::class );
26 return new NoWriteWatchedItemStore( $innerService );
27 }
28
29 /**
30 * @param string $method
31 * @param mixed $result
32 * @return NoWriteWatchedItemStore
33 */
34 private function getNoWriteStoreForProxyCall( string $method, $result ): NoWriteWatchedItemStore {
35 // NoWriteWatchedItemStore where the inner actual store is used a single time
36 // for a method call
37 $innerService = $this->createNoOpAbstractMock(
38 WatchedItemStoreInterface::class,
39 [ $method ]
40 );
41 $innerService->expects( $this->once() )->method( $method )->willReturn( $result );
42 return new NoWriteWatchedItemStore( $innerService );
43 }
44
45 public function testAddWatch() {
46 $noWriteService = $this->getNoWriteStoreForErrors();
47
48 $this->expectException( DBReadOnlyError::class );
49 $noWriteService->addWatch(
50 new UserIdentityValue( 1, 'MockUser' ), new TitleValue( 0, 'Foo' ) );
51 }
52
53 public function testAddWatchBatchForUser() {
54 $noWriteService = $this->getNoWriteStoreForErrors();
55
56 $this->expectException( DBReadOnlyError::class );
57 $noWriteService->addWatchBatchForUser( new UserIdentityValue( 1, 'MockUser' ), [] );
58 }
59
60 public function testRemoveWatch() {
61 $noWriteService = $this->getNoWriteStoreForErrors();
62
63 $this->expectException( DBReadOnlyError::class );
64 $noWriteService->removeWatch(
65 new UserIdentityValue( 1, 'MockUser' ), new TitleValue( 0, 'Foo' ) );
66 }
67
68 public function testSetNotificationTimestampsForUser() {
69 $noWriteService = $this->getNoWriteStoreForErrors();
70
71 $this->expectException( DBReadOnlyError::class );
72 $noWriteService->setNotificationTimestampsForUser(
73 new UserIdentityValue( 1, 'MockUser' ),
74 'timestamp',
75 []
76 );
77 }
78
79 public function testUpdateNotificationTimestamp() {
80 $noWriteService = $this->getNoWriteStoreForErrors();
81
82 $this->expectException( DBReadOnlyError::class );
83 $noWriteService->updateNotificationTimestamp(
84 new UserIdentityValue( 1, 'MockUser' ),
85 new TitleValue( 0, 'Foo' ),
86 'timestamp'
87 );
88 }
89
90 public function testResetNotificationTimestamp() {
91 $noWriteService = $this->getNoWriteStoreForErrors();
92
93 $this->expectException( DBReadOnlyError::class );
94 $noWriteService->resetNotificationTimestamp(
95 new UserIdentityValue( 1, 'MockUser' ),
96 new TitleValue( 0, 'Foo' )
97 );
98 }
99
100 public function testCountWatchedItems() {
101 $noWriteService = $this->getNoWriteStoreForProxyCall( 'countWatchedItems', __METHOD__ );
102
103 $return = $noWriteService->countWatchedItems(
104 new UserIdentityValue( 1, 'MockUser' )
105 );
106 $this->assertEquals( __METHOD__, $return );
107 }
108
109 public function testCountWatchers() {
110 $noWriteService = $this->getNoWriteStoreForProxyCall( 'countWatchers', __METHOD__ );
111
112 $return = $noWriteService->countWatchers(
113 new TitleValue( 0, 'Foo' )
114 );
115 $this->assertEquals( __METHOD__, $return );
116 }
117
118 public function testCountVisitingWatchers() {
119 $noWriteService = $this->getNoWriteStoreForProxyCall( 'countVisitingWatchers', __METHOD__ );
120
121 $return = $noWriteService->countVisitingWatchers(
122 new TitleValue( 0, 'Foo' ),
123 9
124 );
125 $this->assertEquals( __METHOD__, $return );
126 }
127
128 public function testCountWatchersMultiple() {
129 $noWriteService = $this->getNoWriteStoreForProxyCall( 'countWatchersMultiple', __METHOD__ );
130
131 $return = $noWriteService->countWatchersMultiple(
132 [ new TitleValue( 0, 'Foo' ) ],
133 []
134 );
135 $this->assertEquals( __METHOD__, $return );
136 }
137
138 public function testCountVisitingWatchersMultiple() {
139 $noWriteService = $this->getNoWriteStoreForProxyCall( 'countVisitingWatchersMultiple', __METHOD__ );
140
141 $return = $noWriteService->countVisitingWatchersMultiple(
142 [ [ new TitleValue( 0, 'Foo' ), 99 ] ],
143 11
144 );
145 $this->assertEquals( __METHOD__, $return );
146 }
147
148 public function testGetWatchedItem() {
149 $noWriteService = $this->getNoWriteStoreForProxyCall( 'getWatchedItem', __METHOD__ );
150
151 $return = $noWriteService->getWatchedItem(
152 new UserIdentityValue( 1, 'MockUser' ),
153 new TitleValue( 0, 'Foo' )
154 );
155 $this->assertEquals( __METHOD__, $return );
156 }
157
158 public function testLoadWatchedItem() {
159 $noWriteService = $this->getNoWriteStoreForProxyCall( 'loadWatchedItem', __METHOD__ );
160
161 $return = $noWriteService->loadWatchedItem(
162 new UserIdentityValue( 1, 'MockUser' ),
163 new TitleValue( 0, 'Foo' )
164 );
165 $this->assertEquals( __METHOD__, $return );
166 }
167
168 public function testGetWatchedItemsForUser() {
169 $noWriteService = $this->getNoWriteStoreForProxyCall( 'getWatchedItemsForUser', __METHOD__ );
170
171 $return = $noWriteService->getWatchedItemsForUser(
172 new UserIdentityValue( 1, 'MockUser' ),
173 []
174 );
175 $this->assertEquals( __METHOD__, $return );
176 }
177
178 public function testIsWatched() {
179 $noWriteService = $this->getNoWriteStoreForProxyCall( 'isWatched', __METHOD__ );
180
181 $return = $noWriteService->isWatched(
182 new UserIdentityValue( 1, 'MockUser' ),
183 new TitleValue( 0, 'Foo' )
184 );
185 $this->assertEquals( __METHOD__, $return );
186 }
187
188 public function testGetNotificationTimestampsBatch() {
189 $noWriteService = $this->getNoWriteStoreForProxyCall( 'getNotificationTimestampsBatch', __METHOD__ );
190
191 $return = $noWriteService->getNotificationTimestampsBatch(
192 new UserIdentityValue( 1, 'MockUser' ),
193 [ new TitleValue( 0, 'Foo' ) ]
194 );
195 $this->assertEquals( __METHOD__, $return );
196 }
197
198 public function testCountUnreadNotifications() {
199 $noWriteService = $this->getNoWriteStoreForProxyCall( 'countUnreadNotifications', __METHOD__ );
200
201 $return = $noWriteService->countUnreadNotifications(
202 new UserIdentityValue( 1, 'MockUser' ),
203 88
204 );
205 $this->assertEquals( __METHOD__, $return );
206 }
207
208 public function testDuplicateAllAssociatedEntries() {
209 $noWriteService = $this->getNoWriteStoreForErrors();
210
211 $this->expectException( DBReadOnlyError::class );
212 $noWriteService->duplicateAllAssociatedEntries(
213 new TitleValue( 0, 'Foo' ),
214 new TitleValue( 0, 'Bar' )
215 );
216 }
217
218 }