resourceloader: Complete code coverage for MessageBlobStore
[lhc/web/wiklou.git] / tests / phpunit / includes / resourceloader / MessageBlobStoreTest.php
index 3de315a..29e1863 100644 (file)
@@ -1,34 +1,95 @@
 <?php
 
 /**
- * @group Database
  * @group Cache
  * @covers MessageBlobStore
  */
-class MessageBlobStoreTest extends ResourceLoaderTestCase {
-       protected $tablesUsed = array( 'msg_resource' );
+class MessageBlobStoreTest extends PHPUnit_Framework_TestCase {
+
+       protected function setUp() {
+               parent::setUp();
+               // MediaWiki tests defaults $wgMainWANCache to CACHE_NONE.
+               // Use hash instead so that caching is observed
+               $this->wanCache = $this->getMockBuilder( 'WANObjectCache' )
+                       ->setConstructorArgs( [ [
+                               'cache' => new HashBagOStuff(),
+                               'pool' => 'test',
+                               'relayer' => new EventRelayerNull( [] )
+                       ] ] )
+                       ->setMethods( [ 'makePurgeValue' ] )
+                       ->getMock();
+
+               $this->wanCache->expects( $this->any() )
+                       ->method( 'makePurgeValue' )
+                       ->will( $this->returnCallback( function ( $timestamp, $holdoff ) {
+                               // Disable holdoff as it messes with testing
+                               return WANObjectCache::PURGE_VAL_PREFIX . (float)$timestamp . ':0';
+                       } ) );
+       }
 
        protected function makeBlobStore( $methods = null, $rl = null ) {
                $blobStore = $this->getMockBuilder( 'MessageBlobStore' )
-                       ->setConstructorArgs( array( $rl ) )
+                       ->setConstructorArgs( [ $rl ] )
                        ->setMethods( $methods )
                        ->getMock();
 
+               $access = TestingAccessWrapper::newFromObject( $blobStore );
+               $access->wanCache = $this->wanCache;
                return $blobStore;
        }
 
        protected function makeModule( array $messages ) {
-               $module = new ResourceLoaderTestModule( array( 'messages' => $messages ) );
+               $module = new ResourceLoaderTestModule( [ 'messages' => $messages ] );
                $module->setName( 'test.blobstore' );
                return $module;
        }
 
+       /** @covers MessageBlobStore::setLogger */
+       public function testSetLogger() {
+               $blobStore = $this->makeBlobStore();
+               $this->assertSame( null, $blobStore->setLogger( new Psr\Log\NullLogger() ) );
+       }
+
+       /** @covers MessageBlobStore::getResourceLoader */
+       public function testGetResourceLoader() {
+               // Call protected method
+               $blobStore = TestingAccessWrapper::newFromObject( $this->makeBlobStore() );
+               $this->assertInstanceOf(
+                       ResourceLoader::class,
+                       $blobStore->getResourceLoader()
+               );
+       }
+
+       /** @covers MessageBlobStore::fetchMessage */
+       public function testFetchMessage() {
+               $module = $this->makeModule( [ 'mainpage' ] );
+               $rl = new ResourceLoader();
+               $rl->register( $module->getName(), $module );
+
+               $blobStore = $this->makeBlobStore( null, $rl );
+               $blob = $blobStore->getBlob( $module, 'en' );
+
+               $this->assertEquals( '{"mainpage":"Main Page"}', $blob, 'Generated blob' );
+       }
+
+       /** @covers MessageBlobStore::fetchMessage */
+       public function testFetchMessageFail() {
+               $module = $this->makeModule( [ 'i-dont-exist' ] );
+               $rl = new ResourceLoader();
+               $rl->register( $module->getName(), $module );
+
+               $blobStore = $this->makeBlobStore( null, $rl );
+               $blob = $blobStore->getBlob( $module, 'en' );
+
+               $this->assertEquals( '{"i-dont-exist":"\u29fci-dont-exist\u29fd"}', $blob, 'Generated blob' );
+       }
+
        public function testGetBlob() {
-               $module = $this->makeModule( array( 'foo' ) );
+               $module = $this->makeModule( [ 'foo' ] );
                $rl = new ResourceLoader();
                $rl->register( $module->getName(), $module );
 
-               $blobStore = $this->makeBlobStore( array( 'fetchMessage' ), $rl );
+               $blobStore = $this->makeBlobStore( [ 'fetchMessage' ], $rl );
                $blobStore->expects( $this->once() )
                        ->method( 'fetchMessage' )
                        ->will( $this->returnValue( 'Example' ) );
@@ -39,66 +100,66 @@ class MessageBlobStoreTest extends ResourceLoaderTestCase {
        }
 
        public function testGetBlobCached() {
-               $module = $this->makeModule( array( 'example' ) );
+               $module = $this->makeModule( [ 'example' ] );
                $rl = new ResourceLoader();
                $rl->register( $module->getName(), $module );
 
-               $blobStore = $this->makeBlobStore( array( 'fetchMessage' ), $rl );
+               $blobStore = $this->makeBlobStore( [ 'fetchMessage' ], $rl );
                $blobStore->expects( $this->once() )
                        ->method( 'fetchMessage' )
                        ->will( $this->returnValue( 'First' ) );
 
-               $module = $this->makeModule( array( 'example' ) );
+               $module = $this->makeModule( [ 'example' ] );
                $blob = $blobStore->getBlob( $module, 'en' );
                $this->assertEquals( '{"example":"First"}', $blob, 'Generated blob' );
 
-               $blobStore = $this->makeBlobStore( array( 'fetchMessage' ), $rl );
+               $blobStore = $this->makeBlobStore( [ 'fetchMessage' ], $rl );
                $blobStore->expects( $this->never() )
                        ->method( 'fetchMessage' )
                        ->will( $this->returnValue( 'Second' ) );
 
-               $module = $this->makeModule( array( 'example' ) );
+               $module = $this->makeModule( [ 'example' ] );
                $blob = $blobStore->getBlob( $module, 'en' );
                $this->assertEquals( '{"example":"First"}', $blob, 'Cache hit' );
        }
 
        public function testUpdateMessage() {
-               $module = $this->makeModule( array( 'example' ) );
+               $module = $this->makeModule( [ 'example' ] );
                $rl = new ResourceLoader();
                $rl->register( $module->getName(), $module );
-               $blobStore = $this->makeBlobStore( array( 'fetchMessage' ), $rl );
-               $blobStore->expects( $this->exactly( 2 ) )
+               $blobStore = $this->makeBlobStore( [ 'fetchMessage' ], $rl );
+               $blobStore->expects( $this->once() )
                        ->method( 'fetchMessage' )
-                       ->will( $this->onConsecutiveCalls( 'First', 'Second' ) );
+                       ->will( $this->returnValue( 'First' ) );
 
                $blob = $blobStore->getBlob( $module, 'en' );
                $this->assertEquals( '{"example":"First"}', $blob, 'Generated blob' );
 
                $blobStore->updateMessage( 'example' );
 
-               $module = $this->makeModule( array( 'example' ) );
+               $module = $this->makeModule( [ 'example' ] );
                $rl = new ResourceLoader();
                $rl->register( $module->getName(), $module );
-               $blobStore = $this->makeBlobStore( array( 'fetchMessage' ), $rl );
-               $blobStore->expects( $this->never() )
+               $blobStore = $this->makeBlobStore( [ 'fetchMessage' ], $rl );
+               $blobStore->expects( $this->once() )
                        ->method( 'fetchMessage' )
-                       ->will( $this->returnValue( 'Wrong' ) );
+                       ->will( $this->returnValue( 'Second' ) );
 
                $blob = $blobStore->getBlob( $module, 'en' );
                $this->assertEquals( '{"example":"Second"}', $blob, 'Updated blob' );
        }
 
        public function testValidation() {
-               $module = $this->makeModule( array( 'foo' ) );
+               $module = $this->makeModule( [ 'foo' ] );
                $rl = new ResourceLoader();
                $rl->register( $module->getName(), $module );
 
-               $blobStore = $this->makeBlobStore( array( 'fetchMessage' ), $rl );
+               $blobStore = $this->makeBlobStore( [ 'fetchMessage' ], $rl );
                $blobStore->expects( $this->once() )
                        ->method( 'fetchMessage' )
-                       ->will( $this->returnValueMap( array(
-                               array( 'foo', 'en', 'Hello' ),
-                       ) ) );
+                       ->will( $this->returnValueMap( [
+                               [ 'foo', 'en', 'Hello' ],
+                       ] ) );
 
                $blob = $blobStore->getBlob( $module, 'en' );
                $this->assertEquals( '{"foo":"Hello"}', $blob, 'Generated blob' );
@@ -107,19 +168,40 @@ class MessageBlobStoreTest extends ResourceLoaderTestCase {
                // message 'foo' and 'bar'. While updateMessage() was not called (since no
                // message values were changed) it should detect the change in list of
                // message keys.
-               $module = $this->makeModule( array( 'foo', 'bar' ) );
+               $module = $this->makeModule( [ 'foo', 'bar' ] );
                $rl = new ResourceLoader();
                $rl->register( $module->getName(), $module );
 
-               $blobStore = $this->makeBlobStore( array( 'fetchMessage' ), $rl );
+               $blobStore = $this->makeBlobStore( [ 'fetchMessage' ], $rl );
                $blobStore->expects( $this->exactly( 2 ) )
                        ->method( 'fetchMessage' )
-                       ->will( $this->returnValueMap( array(
-                               array( 'foo', 'en', 'Hello' ),
-                               array( 'bar', 'en', 'World' ),
-                       ) ) );
+                       ->will( $this->returnValueMap( [
+                               [ 'foo', 'en', 'Hello' ],
+                               [ 'bar', 'en', 'World' ],
+                       ] ) );
 
                $blob = $blobStore->getBlob( $module, 'en' );
                $this->assertEquals( '{"foo":"Hello","bar":"World"}', $blob, 'Updated blob' );
        }
+
+       public function testClear() {
+               $module = $this->makeModule( [ 'example' ] );
+               $rl = new ResourceLoader();
+               $rl->register( $module->getName(), $module );
+               $blobStore = $this->makeBlobStore( [ 'fetchMessage' ], $rl );
+               $blobStore->expects( $this->exactly( 2 ) )
+                       ->method( 'fetchMessage' )
+                       ->will( $this->onConsecutiveCalls( 'First', 'Second' ) );
+
+               $blob = $blobStore->getBlob( $module, 'en' );
+               $this->assertEquals( '{"example":"First"}', $blob, 'Generated blob' );
+
+               $blob = $blobStore->getBlob( $module, 'en' );
+               $this->assertEquals( '{"example":"First"}', $blob, 'Cache-hit' );
+
+               $blobStore->clear();
+
+               $blob = $blobStore->getBlob( $module, 'en' );
+               $this->assertEquals( '{"example":"Second"}', $blob, 'Updated blob' );
+       }
 }