Debian Codegraph @

extensions/PageImages/tests/phpunit/unit/PageImageCandidateTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\PageImages;
4
5 use File;
6 use MediaWiki\Title\Title;
7 use MediaWikiUnitTestCase;
8 use PageImages\PageImageCandidate;
9
10 /**
11 * @covers \PageImages\PageImageCandidate
12 * @package MediaWiki\Tests\PageImages
13 */
14 class PageImageCandidateTest extends MediaWikiUnitTestCase {
15
16 /**
17 * @param int|bool $width
18 * @param int|bool $height
19 * @return File
20 */
21 private function fileMock( $width, $height ): File {
22 $title = $this->createMock( Title::class );
23 $title->method( 'getDBKey' )->willReturn( 'Testing' );
24 $file = $this->createMock( File::class );
25 $file->method( 'getTitle' )->willReturn( $title );
26 $file->method( 'getWidth' )->willReturn( $width );
27 $file->method( 'getHeight' )->willReturn( $height );
28 return $file;
29 }
30
31 public static function provideTestCases() {
32 yield 'file no width, no height' => [
33 false, false,
34 [], 'Testing', 0, 0, 0
35 ];
36 yield 'file with width and height' => [
37 42, 24,
38 [], 'Testing', 42, 24, 0
39 ];
40 yield 'file with width and height, handler, no width' => [
41 42, 24,
42 [ 'handler' => [] ], 'Testing', 42, 24, 0
43 ];
44 yield 'file with width and height, handler, with width' => [
45 42, 24,
46 [ 'handler' => [ 'width' => 4224 ] ], 'Testing', 42, 24, 4224
47 ];
48 }
49
50 /**
51 * @dataProvider provideTestCases
52 * @param int|false $mockWidth
53 * @param int|false $mockHeight
54 * @param array $params
55 * @param string $expectedName
56 * @param int $expectedWidth
57 * @param int $expectedHeight
58 * @param int $expectedHandlerWidth
59 */
60 public function testNewFromFileAndParams(
61 $mockWidth,
62 $mockHeight,
63 array $params,
64 string $expectedName,
65 int $expectedWidth,
66 int $expectedHeight,
67 int $expectedHandlerWidth
68 ) {
69 $file = $this->fileMock( $mockWidth, $mockHeight );
70 $image = PageImageCandidate::newFromFileAndParams( $file, $params );
71 $this->assertSame( $expectedName, $image->getFileName() );
72 $this->assertSame( $expectedWidth, $image->getFullWidth() );
73 $this->assertSame( $expectedHeight, $image->getFullHeight() );
74 $this->assertSame( $expectedHandlerWidth, $image->getHandlerWidth() );
75 }
76
77 /**
78 * @dataProvider provideTestCases
79 * @param int|false $mockWidth
80 * @param int|false $mockHeight
81 * @param array $params
82 */
83 public function testSerializeDeserialize( $mockWidth, $mockHeight, array $params ) {
84 $file = $this->fileMock( $mockWidth, $mockHeight );
85 $candidate = PageImageCandidate::newFromFileAndParams( $file, $params );
86 $deserialized = PageImageCandidate::newFromArray( $candidate->jsonSerialize() );
87 $this->assertSame( $candidate->getFileName(), $deserialized->getFileName() );
88 $this->assertSame( $candidate->getFullHeight(), $deserialized->getFullHeight() );
89 $this->assertSame( $candidate->getFullHeight(), $deserialized->getFullHeight() );
90 $this->assertSame( $candidate->getHandlerWidth(), $deserialized->getHandlerWidth() );
91 }
92 }