mediawiki-extensions-PageIm.../tests/phpunit/unit/PageImageCandidateTest.php
Petr Pchelko 6fa0a18037 Stop writing objects into ParserOutput::setExtensionData.
As we switch ParserCache to JSON, we can no longer serialized
class instances in extension data. PageImages was writing the
full set of properies it received from Parser into it's extension
data, some of which are sometimes class instances. Instead, only
write the nessessary subset of data into extension-data.

This change is completely forward and backward compatible.
Since before this change we were already writing the same array,
but with many additional unused properties.

Bug: T266251
Change-Id: Ieb4a139465159611e6b3a99c4b68c3c174b1944f
2020-10-26 13:35:38 -07:00

88 lines
2.6 KiB
PHP

<?php
namespace MediaWiki\Tests\PageImages;
use File;
use MediaWikiUnitTestCase;
use PageImages\PageImageCandidate;
use Title;
/**
* @covers \PageImages\PageImageCandidate
* @package MediaWiki\Tests\PageImages
*/
class PageImageCandidateTest extends MediaWikiUnitTestCase {
/**
* @param int|bool $width
* @param int|bool $height
* @return File
*/
private function fileMock( $width, $height ) : File {
$title = $this->createMock( Title::class );
$title->method( 'getDBKey' )->willReturn( 'Testing' );
$file = $this->createMock( File::class );
$file->method( 'getTitle' )->willReturn( $title );
$file->method( 'getWidth' )->willReturn( $width );
$file->method( 'getHeight' )->willReturn( $height );
return $file;
}
public function provideTestCases() {
yield 'file no width, no height' => [
$this->fileMock( false, false ),
[], 'Testing', 0, 0, 0
];
yield 'file with width and height' => [
$this->fileMock( 42, 24 ),
[], 'Testing', 42, 24, 0
];
yield 'file with width and height, handler, no width' => [
$this->fileMock( 42, 24 ),
[ 'handler' => [] ], 'Testing', 42, 24, 0
];
yield 'file with width and height, handler, with width' => [
$this->fileMock( 42, 24 ),
[ 'handler' => [ 'width' => 4224 ] ], 'Testing', 42, 24, 4224
];
}
/**
* @dataProvider provideTestCases
* @param File $file
* @param array $params
* @param string $expectedName
* @param int $expectedWidth
* @param int $expectedHeight
* @param int $expectedHandlerWidth
*/
public function testNewFromFileAndParams(
File $file,
array $params,
string $expectedName,
int $expectedWidth,
int $expectedHeight,
int $expectedHandlerWidth
) {
$image = PageImageCandidate::newFromFileAndParams( $file, $params );
$this->assertSame( $expectedName, $image->getFileName() );
$this->assertSame( $expectedWidth, $image->getFullWidth() );
$this->assertSame( $expectedHeight, $image->getFullHeight() );
$this->assertSame( $expectedHandlerWidth, $image->getHandlerWidth() );
}
/**
* @dataProvider provideTestCases
* @param File $file
* @param array $params
*/
public function testSerializeDeserialize( File $file, array $params ) {
$candidate = PageImageCandidate::newFromFileAndParams( $file, $params );
$deserialized = PageImageCandidate::newFromArray( $candidate->jsonSerialize() );
$this->assertSame( $candidate->getFileName(), $deserialized->getFileName() );
$this->assertSame( $candidate->getFullHeight(), $deserialized->getFullHeight() );
$this->assertSame( $candidate->getFullHeight(), $deserialized->getFullHeight() );
$this->assertSame( $candidate->getHandlerWidth(), $deserialized->getHandlerWidth() );
}
}