Restructure Media related tests to avoid duplicated code
authorBrian Wolff <bawolff+wn@gmail.com>
Mon, 26 May 2014 20:16:07 +0000 (17:16 -0300)
committerGilles Dubuc <gdubuc@wikimedia.org>
Tue, 27 May 2014 09:01:12 +0000 (11:01 +0200)
Change-Id: I4428d38e0199b12255f3746e79f8093259a03da1

tests/TestsAutoLoader.php
tests/phpunit/includes/media/DjVuTest.php
tests/phpunit/includes/media/ExifRotationTest.php
tests/phpunit/includes/media/FormatMetadataTest.php
tests/phpunit/includes/media/GIFTest.php
tests/phpunit/includes/media/JpegTest.php
tests/phpunit/includes/media/MediaWikiMediaTestCase.php [new file with mode: 0644]
tests/phpunit/includes/media/PNGTest.php
tests/phpunit/includes/media/SVGTest.php
tests/phpunit/structure/StructureTest.php

index a5eb29b..37cf68f 100644 (file)
@@ -86,6 +86,7 @@ $wgAutoloadClasses += array(
 
        # tests/phpunit/media
        'FakeDimensionFile' => "$testDir/phpunit/includes/media/FakeDimensionFile.php",
+       'MediaWikiMediaTestCase' => "$testDir/phpunit/includes/media/MediaWikiMediaTestCase.php",
 
        # tests/phpunit/mocks
        'MockFSFile' => "$testDir/phpunit/mocks/filebackend/MockFSFile.php",
index 5657a5a..76cefe5 100644 (file)
@@ -2,17 +2,7 @@
 /**
  * @covers DjVuHandler
  */
-class DjVuTest extends MediaWikiTestCase {
-
-       /**
-        * @var string the directory where test files are
-        */
-       protected $filePath;
-
-       /**
-        * @var FSRepo the repository to use
-        */
-       protected $repo;
+class DjVuTest extends MediaWikiMediaTestCase {
 
        /**
         * @var DjVuHandler
@@ -26,35 +16,13 @@ class DjVuTest extends MediaWikiTestCase {
                $djvuSupport = new DjVuSupport();
 
                if ( !$djvuSupport->isEnabled() ) {
-                       $this->markTestSkipped( 'This test needs the installation of the ddjvu, djvutoxml and djvudump tools' );
+                       $this->markTestSkipped(
+                       'This test needs the installation of the ddjvu, djvutoxml and djvudump tools' );
                }
 
-               //file repo setup
-               $this->filePath = __DIR__ . '/../../data/media/';
-               $backend = new FSFileBackend( array(
-                       'name' => 'localtesting',
-                       'wikiId' => wfWikiId(),
-                       'lockManager' => new NullLockManager( array() ),
-                       'containerPaths' => array( 'data' => $this->filePath )
-               ) );
-               $this->repo = new FSRepo( array(
-                       'name' => 'temp',
-                       'url' => 'http://localhost/thumbtest',
-                       'backend' => $backend
-               ) );
-
                $this->handler = new DjVuHandler();
        }
 
-       protected function dataFile( $name, $type ) {
-               return new UnregisteredLocalFile(
-                       false,
-                       $this->repo,
-                       'mwstore://localtesting/data/' . $name,
-                       $type
-               );
-       }
-
        public function testGetImageSize() {
                $this->assertArrayEquals(
                        array( 2480, 3508, 'DjVu', 'width="2480" height="3508"' ),
index 438dc79..6a1e422 100644 (file)
@@ -6,26 +6,13 @@
  *
  * @todo covers tags
  */
-class ExifRotationTest extends MediaWikiTestCase {
+class ExifRotationTest extends MediaWikiMediaTestCase {
 
        protected function setUp() {
                parent::setUp();
                $this->checkPHPExtension( 'exif' );
 
                $this->handler = new BitmapHandler();
-               $filePath = __DIR__ . '/../../data/media';
-
-               $tmpDir = $this->getNewTempDirectory();
-
-               $this->repo = new FSRepo( array(
-                       'name' => 'temp',
-                       'url' => 'http://localhost/thumbtest',
-                       'backend' => new FSFileBackend( array(
-                               'name' => 'localtesting',
-                               'wikiId' => wfWikiId(),
-                               'containerPaths' => array( 'temp-thumb' => $tmpDir, 'data' => $filePath )
-                       ) )
-               ) );
 
                $this->setMwGlobals( array(
                        'wgShowEXIF' => true,
@@ -33,6 +20,13 @@ class ExifRotationTest extends MediaWikiTestCase {
                ) );
        }
 
+       /**
+        * Mark this test as creating thumbnail files.
+        */
+       protected function createsThumbnails() {
+               return true;
+       }
+
        /**
         * @dataProvider provideFiles
         */
@@ -93,12 +87,6 @@ class ExifRotationTest extends MediaWikiTestCase {
                }
        }
 
-       /* Utility function */
-       private function dataFile( $name, $type ) {
-               return new UnregisteredLocalFile( false, $this->repo,
-                       "mwstore://localtesting/data/$name", $type );
-       }
-
        public static function provideFiles() {
                return array(
                        array(
index a0ab92c..daaefc0 100644 (file)
@@ -1,28 +1,11 @@
 <?php
 
-class FormatMetadataTest extends MediaWikiTestCase {
-
-       /** @var FSFileBackend */
-       protected $backend;
-       /** @var FSRepo */
-       protected $repo;
+class FormatMetadataTest extends MediaWikiMediaTestCase {
 
        protected function setUp() {
                parent::setUp();
 
                $this->checkPHPExtension( 'exif' );
-               $filePath = __DIR__ . '/../../data/media';
-               $this->backend = new FSFileBackend( array(
-                       'name' => 'localtesting',
-                       'wikiId' => wfWikiId(),
-                       'containerPaths' => array( 'data' => $filePath )
-               ) );
-               $this->repo = new FSRepo( array(
-                       'name' => 'temp',
-                       'url' => 'http://localhost/thumbtest',
-                       'backend' => $this->backend
-               ) );
-
                $this->setMwGlobals( 'wgShowEXIF', true );
        }
 
@@ -82,9 +65,4 @@ class FormatMetadataTest extends MediaWikiTestCase {
                        // TODO: more test cases
                );
        }
-
-       private function dataFile( $name, $type ) {
-               return new UnregisteredLocalFile( false, $this->repo,
-                       "mwstore://localtesting/data/$name", $type );
-       }
 }
index 7a7ed21..17b2964 100644 (file)
@@ -1,29 +1,12 @@
 <?php
-class GIFHandlerTest extends MediaWikiTestCase {
+class GIFHandlerTest extends MediaWikiMediaTestCase {
 
-       /** @var FSFileBackend */
-       protected $backend;
        /** @var GIFHandler */
        protected $handler;
-       /** @var FSRepo */
-       protected $repo;
-       /** @var string */
-       protected $filePath;
 
        protected function setUp() {
                parent::setUp();
 
-               $this->filePath = __DIR__ . '/../../data/media';
-               $this->backend = new FSFileBackend( array(
-                       'name' => 'localtesting',
-                       'wikiId' => wfWikiId(),
-                       'containerPaths' => array( 'data' => $this->filePath )
-               ) );
-               $this->repo = new FSRepo( array(
-                       'name' => 'temp',
-                       'url' => 'http://localhost/thumbtest',
-                       'backend' => $this->backend
-               ) );
                $this->handler = new GIFHandler();
        }
 
@@ -152,9 +135,4 @@ class GIFHandlerTest extends MediaWikiTestCase {
                        ),
                );
        }
-
-       private function dataFile( $name, $type ) {
-               return new UnregisteredLocalFile( false, $this->repo,
-                       "mwstore://localtesting/data/$name", $type );
-       }
 }
index b261137..c856b1c 100644 (file)
@@ -2,29 +2,14 @@
 /**
  * @covers JpegHandler
  */
-class JpegTest extends MediaWikiTestCase {
-
-       protected $filePath;
+class JpegTest extends MediaWikiMediaTestCase {
 
        protected function setUp() {
                parent::setUp();
                $this->checkPHPExtension( 'exif' );
 
-               $this->filePath = __DIR__ . '/../../data/media/';
-
                $this->setMwGlobals( 'wgShowEXIF', true );
 
-               $this->backend = new FSFileBackend( array(
-                       'name' => 'localtesting',
-                       'wikiId' => wfWikiId(),
-                       'containerPaths' => array( 'data' => $this->filePath )
-               ) );
-               $this->repo = new FSRepo( array(
-                       'name' => 'temp',
-                       'url' => 'http://localhost/thumbtest',
-                       'backend' => $this->backend
-               ) );
-
                $this->handler = new JpegHandler;
        }
 
@@ -64,9 +49,4 @@ class JpegTest extends MediaWikiTestCase {
 
                $this->assertEquals( $res, $expected );
        }
-
-       private function dataFile( $name, $type ) {
-               return new UnregisteredLocalFile( false, $this->repo,
-                       "mwstore://localtesting/data/$name", $type );
-       }
 }
diff --git a/tests/phpunit/includes/media/MediaWikiMediaTestCase.php b/tests/phpunit/includes/media/MediaWikiMediaTestCase.php
new file mode 100644 (file)
index 0000000..ebfcba9
--- /dev/null
@@ -0,0 +1,73 @@
+<?php
+/**
+ * Specificly for testing Media handlers. Sets up a FSFile backend
+ */
+abstract class MediaWikiMediaTestCase extends MediaWikiTestCase {
+
+       /** @var FSRepo */
+       protected $repo;
+       /** @var FSFileBackend */
+       protected $backend;
+       /** @var string */
+       protected $filePath;
+
+
+       protected function setUp() {
+               parent::setUp();
+
+               $this->filePath = $this->getFilePath();
+               $containers = array( 'data' => $this->filePath );
+               if ( $this->createsThumbnails() ) {
+                       // We need a temp directory for the thumbnails
+                       // the container is named 'temp-thumb' because it is the
+                       // thumb directory for a FSRepo named "temp".
+                       $containers['temp-thumb'] = $this->getNewTempDirectory();
+               }
+
+               $this->backend = new FSFileBackend( array(
+                       'name' => 'localtesting',
+                       'wikiId' => wfWikiId(),
+                       'containerPaths' => $containers
+               ) );
+               $this->repo = new FSRepo( array(
+                       'name' => 'temp',
+                       'url' => 'http://localhost/thumbtest',
+                       'backend' => $this->backend
+               ) );
+       }
+
+       /**
+        * The result of this method will set the file path to use,
+        * as well as the protected member $filePath
+        *
+        * @return String path where files are
+        */
+       protected function getFilePath() {
+               return __DIR__ . '/../../data/media/';
+       }
+
+       /**
+        * Will the test create thumbnails (and thus do we need to set aside
+        * a temporary directory for them?)
+        *
+        * Override this method if your test case creates thumbnails
+        *
+        * @return boolean
+        */
+       protected function createsThumbnails() {
+               return false;
+       }
+
+       /**
+        * Utility function: Get a new file object for a file on disk but not actually in db.
+        *
+        * File must be in the path returned by getFilePath()
+        * @param $name String File name
+        * @param $type String MIME type
+        * @return UnregisteredLocalFile
+        */
+       protected function dataFile( $name, $type ) {
+               return new UnregisteredLocalFile( false, $this->repo,
+                       "mwstore://localtesting/data/$name", $type );
+       }
+}
index 59d40de..14e4d57 100644 (file)
@@ -1,29 +1,11 @@
 <?php
-class PNGHandlerTest extends MediaWikiTestCase {
+class PNGHandlerTest extends MediaWikiMediaTestCase {
 
        /** @var PNGHandler */
        protected $handler;
-       /** @var FSRepo */
-       protected $repo;
-       /** @var FSFileBackend */
-       protected $backend;
-       /** @var string */
-       protected $filePath;
 
        protected function setUp() {
                parent::setUp();
-
-               $this->filePath = __DIR__ . '/../../data/media';
-               $this->backend = new FSFileBackend( array(
-                       'name' => 'localtesting',
-                       'wikiId' => wfWikiId(),
-                       'containerPaths' => array( 'data' => $this->filePath )
-               ) );
-               $this->repo = new FSRepo( array(
-                       'name' => 'temp',
-                       'url' => 'http://localhost/thumbtest',
-                       'backend' => $this->backend
-               ) );
                $this->handler = new PNGHandler();
        }
 
@@ -142,9 +124,4 @@ class PNGHandlerTest extends MediaWikiTestCase {
                        ),
                );
        }
-
-       private function dataFile( $name, $type ) {
-               return new UnregisteredLocalFile( false, $this->repo,
-                       "mwstore://localtesting/data/$name", $type );
-       }
 }
index 8a3d527..e3bb05e 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 
-class SvgTest extends MediaWikiTestCase {
+class SvgTest extends MediaWikiMediaTestCase {
 
        protected function setUp() {
                parent::setUp();
@@ -9,17 +9,6 @@ class SvgTest extends MediaWikiTestCase {
 
                $this->setMwGlobals( 'wgShowEXIF', true );
 
-               $this->backend = new FSFileBackend( array(
-                       'name' => 'localtesting',
-                       'wikiId' => wfWikiId(),
-                       'containerPaths' => array( 'data' => $this->filePath )
-               ) );
-               $this->repo = new FSRepo( array(
-                       'name' => 'temp',
-                       'url' => 'http://localhost/thumbtest',
-                       'backend' => $this->backend
-               ) );
-
                $this->handler = new SvgHandler;
        }
 
@@ -46,9 +35,4 @@ class SvgTest extends MediaWikiTestCase {
                        array( 'Wikimedia-logo.svg', array() )
                );
        }
-
-       private function dataFile( $name, $type ) {
-               return new UnregisteredLocalFile( false, $this->repo,
-                       "mwstore://localtesting/data/$name", $type );
-       }
 }
index 2393299..f5cd892 100644 (file)
@@ -21,6 +21,7 @@ class StructureTest extends MediaWikiTestCase {
                        'ApiQueryTestBase',
                        'ApiQueryContinueTestBase',
                        'MediaWikiLangTestCase',
+                       'MediaWikiMediaTestCase',
                        'MediaWikiTestCase',
                        'ResourceLoaderTestCase',
                        'PHPUnit_Framework_TestCase',