From 6beee2535f37e71588fc360cc48eae6735e390df Mon Sep 17 00:00:00 2001 From: Brian Wolff Date: Mon, 26 May 2014 17:16:07 -0300 Subject: [PATCH] Restructure Media related tests to avoid duplicated code Change-Id: I4428d38e0199b12255f3746e79f8093259a03da1 --- tests/TestsAutoLoader.php | 1 + tests/phpunit/includes/media/DjVuTest.php | 38 +--------- .../includes/media/ExifRotationTest.php | 28 ++----- .../includes/media/FormatMetadataTest.php | 24 +----- tests/phpunit/includes/media/GIFTest.php | 24 +----- tests/phpunit/includes/media/JpegTest.php | 22 +----- .../includes/media/MediaWikiMediaTestCase.php | 73 +++++++++++++++++++ tests/phpunit/includes/media/PNGTest.php | 25 +------ tests/phpunit/includes/media/SVGTest.php | 18 +---- tests/phpunit/structure/StructureTest.php | 1 + 10 files changed, 91 insertions(+), 163 deletions(-) create mode 100644 tests/phpunit/includes/media/MediaWikiMediaTestCase.php diff --git a/tests/TestsAutoLoader.php b/tests/TestsAutoLoader.php index a5eb29bca0..37cf68f5ad 100644 --- a/tests/TestsAutoLoader.php +++ b/tests/TestsAutoLoader.php @@ -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", diff --git a/tests/phpunit/includes/media/DjVuTest.php b/tests/phpunit/includes/media/DjVuTest.php index 5657a5ac4f..76cefe5537 100644 --- a/tests/phpunit/includes/media/DjVuTest.php +++ b/tests/phpunit/includes/media/DjVuTest.php @@ -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"' ), diff --git a/tests/phpunit/includes/media/ExifRotationTest.php b/tests/phpunit/includes/media/ExifRotationTest.php index 438dc79ea9..6a1e422651 100644 --- a/tests/phpunit/includes/media/ExifRotationTest.php +++ b/tests/phpunit/includes/media/ExifRotationTest.php @@ -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( diff --git a/tests/phpunit/includes/media/FormatMetadataTest.php b/tests/phpunit/includes/media/FormatMetadataTest.php index a0ab92cb8d..daaefc06fd 100644 --- a/tests/phpunit/includes/media/FormatMetadataTest.php +++ b/tests/phpunit/includes/media/FormatMetadataTest.php @@ -1,28 +1,11 @@ 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 ); - } } diff --git a/tests/phpunit/includes/media/GIFTest.php b/tests/phpunit/includes/media/GIFTest.php index 7a7ed217e8..17b296415e 100644 --- a/tests/phpunit/includes/media/GIFTest.php +++ b/tests/phpunit/includes/media/GIFTest.php @@ -1,29 +1,12 @@ 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 ); - } } diff --git a/tests/phpunit/includes/media/JpegTest.php b/tests/phpunit/includes/media/JpegTest.php index b2611371f8..c856b1ca82 100644 --- a/tests/phpunit/includes/media/JpegTest.php +++ b/tests/phpunit/includes/media/JpegTest.php @@ -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 index 0000000000..ebfcba9ec5 --- /dev/null +++ b/tests/phpunit/includes/media/MediaWikiMediaTestCase.php @@ -0,0 +1,73 @@ +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 ); + } +} diff --git a/tests/phpunit/includes/media/PNGTest.php b/tests/phpunit/includes/media/PNGTest.php index 59d40de3f1..14e4d57c5d 100644 --- a/tests/phpunit/includes/media/PNGTest.php +++ b/tests/phpunit/includes/media/PNGTest.php @@ -1,29 +1,11 @@ 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 ); - } } diff --git a/tests/phpunit/includes/media/SVGTest.php b/tests/phpunit/includes/media/SVGTest.php index 8a3d527c84..e3bb05eb1b 100644 --- a/tests/phpunit/includes/media/SVGTest.php +++ b/tests/phpunit/includes/media/SVGTest.php @@ -1,6 +1,6 @@ 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 ); - } } diff --git a/tests/phpunit/structure/StructureTest.php b/tests/phpunit/structure/StructureTest.php index 23932997cc..f5cd8927c6 100644 --- a/tests/phpunit/structure/StructureTest.php +++ b/tests/phpunit/structure/StructureTest.php @@ -21,6 +21,7 @@ class StructureTest extends MediaWikiTestCase { 'ApiQueryTestBase', 'ApiQueryContinueTestBase', 'MediaWikiLangTestCase', + 'MediaWikiMediaTestCase', 'MediaWikiTestCase', 'ResourceLoaderTestCase', 'PHPUnit_Framework_TestCase', -- 2.20.1