foreign-resources.yaml: Add blank lines between registrations for easier merges
[lhc/web/wiklou.git] / tests / phpunit / includes / Storage / RevisionStoreFactoryTest.php
1 <?php
2
3 namespace MediaWiki\Tests\Storage;
4
5 use ActorMigration;
6 use CommentStore;
7 use MediaWiki\Logger\Spi as LoggerSpi;
8 use MediaWiki\Storage\BlobStore;
9 use MediaWiki\Storage\BlobStoreFactory;
10 use MediaWiki\Storage\NameTableStore;
11 use MediaWiki\Storage\NameTableStoreFactory;
12 use MediaWiki\Storage\RevisionStore;
13 use MediaWiki\Storage\RevisionStoreFactory;
14 use MediaWiki\Storage\SqlBlobStore;
15 use MediaWikiTestCase;
16 use Psr\Log\LoggerInterface;
17 use Psr\Log\NullLogger;
18 use WANObjectCache;
19 use Wikimedia\Rdbms\ILBFactory;
20 use Wikimedia\Rdbms\ILoadBalancer;
21 use Wikimedia\TestingAccessWrapper;
22
23 class RevisionStoreFactoryTest extends MediaWikiTestCase {
24
25 public function testValidConstruction_doesntCauseErrors() {
26 new RevisionStoreFactory(
27 $this->getMockLoadBalancerFactory(),
28 $this->getMockBlobStoreFactory(),
29 $this->getNameTableStoreFactory(),
30 $this->getHashWANObjectCache(),
31 $this->getMockCommentStore(),
32 ActorMigration::newMigration(),
33 MIGRATION_OLD,
34 $this->getMockLoggerSpi(),
35 true
36 );
37 $this->assertTrue( true );
38 }
39
40 public function provideWikiIds() {
41 yield [ true ];
42 yield [ false ];
43 yield [ 'somewiki' ];
44 yield [ 'somewiki', MIGRATION_OLD , false ];
45 yield [ 'somewiki', MIGRATION_NEW , true ];
46 }
47
48 /**
49 * @dataProvider provideWikiIds
50 */
51 public function testGetRevisionStore(
52 $wikiId,
53 $mcrMigrationStage = MIGRATION_OLD,
54 $contentHandlerUseDb = true
55 ) {
56 $lbFactory = $this->getMockLoadBalancerFactory();
57 $blobStoreFactory = $this->getMockBlobStoreFactory();
58 $nameTableStoreFactory = $this->getNameTableStoreFactory();
59 $cache = $this->getHashWANObjectCache();
60 $commentStore = $this->getMockCommentStore();
61 $actorMigration = ActorMigration::newMigration();
62 $loggerProvider = $this->getMockLoggerSpi();
63
64 $factory = new RevisionStoreFactory(
65 $lbFactory,
66 $blobStoreFactory,
67 $nameTableStoreFactory,
68 $cache,
69 $commentStore,
70 $actorMigration,
71 $mcrMigrationStage,
72 $loggerProvider,
73 $contentHandlerUseDb
74 );
75
76 $store = $factory->getRevisionStore( $wikiId );
77 $wrapper = TestingAccessWrapper::newFromObject( $store );
78
79 // ensure the correct object type is returned
80 $this->assertInstanceOf( RevisionStore::class, $store );
81
82 // ensure the RevisionStore is for the given wikiId
83 $this->assertSame( $wikiId, $wrapper->wikiId );
84
85 // ensure all other required services are correctly set
86 $this->assertSame( $cache, $wrapper->cache );
87 $this->assertSame( $commentStore, $wrapper->commentStore );
88 $this->assertSame( $mcrMigrationStage, $wrapper->mcrMigrationStage );
89 $this->assertSame( $actorMigration, $wrapper->actorMigration );
90 $this->assertSame( $contentHandlerUseDb, $store->getContentHandlerUseDB() );
91
92 $this->assertInstanceOf( ILoadBalancer::class, $wrapper->loadBalancer );
93 $this->assertInstanceOf( BlobStore::class, $wrapper->blobStore );
94 $this->assertInstanceOf( NameTableStore::class, $wrapper->contentModelStore );
95 $this->assertInstanceOf( NameTableStore::class, $wrapper->slotRoleStore );
96 $this->assertInstanceOf( LoggerInterface::class, $wrapper->logger );
97 }
98
99 /**
100 * @return \PHPUnit_Framework_MockObject_MockObject|ILoadBalancer
101 */
102 private function getMockLoadBalancer() {
103 return $this->getMockBuilder( ILoadBalancer::class )
104 ->disableOriginalConstructor()->getMock();
105 }
106
107 /**
108 * @return \PHPUnit_Framework_MockObject_MockObject|ILBFactory
109 */
110 private function getMockLoadBalancerFactory() {
111 $mock = $this->getMockBuilder( ILBFactory::class )
112 ->disableOriginalConstructor()->getMock();
113
114 $mock->method( 'getMainLB' )
115 ->willReturnCallback( function () {
116 return $this->getMockLoadBalancer();
117 } );
118
119 return $mock;
120 }
121
122 /**
123 * @return \PHPUnit_Framework_MockObject_MockObject|SqlBlobStore
124 */
125 private function getMockSqlBlobStore() {
126 return $this->getMockBuilder( SqlBlobStore::class )
127 ->disableOriginalConstructor()->getMock();
128 }
129
130 /**
131 * @return \PHPUnit_Framework_MockObject_MockObject|BlobStoreFactory
132 */
133 private function getMockBlobStoreFactory() {
134 $mock = $this->getMockBuilder( BlobStoreFactory::class )
135 ->disableOriginalConstructor()->getMock();
136
137 $mock->method( 'newSqlBlobStore' )
138 ->willReturnCallback( function () {
139 return $this->getMockSqlBlobStore();
140 } );
141
142 return $mock;
143 }
144
145 /**
146 * @return NameTableStoreFactory
147 */
148 private function getNameTableStoreFactory() {
149 return new NameTableStoreFactory(
150 $this->getMockLoadBalancerFactory(),
151 $this->getHashWANObjectCache(),
152 new NullLogger() );
153 }
154
155 /**
156 * @return \PHPUnit_Framework_MockObject_MockObject|CommentStore
157 */
158 private function getMockCommentStore() {
159 return $this->getMockBuilder( CommentStore::class )
160 ->disableOriginalConstructor()->getMock();
161 }
162
163 private function getHashWANObjectCache() {
164 return new WANObjectCache( [ 'cache' => new \HashBagOStuff() ] );
165 }
166
167 /**
168 * @return \PHPUnit_Framework_MockObject_MockObject|LoggerSpi
169 */
170 private function getMockLoggerSpi() {
171 $mock = $this->getMock( LoggerSpi::class );
172
173 $mock->method( 'getLogger' )
174 ->willReturn( new NullLogger() );
175
176 return $mock;
177 }
178
179 }