Add @covers tags to logging tests
[lhc/web/wiklou.git] / tests / phpunit / includes / media / PNGTest.php
1 <?php
2
3 /**
4 * @group Media
5 */
6 class PNGHandlerTest extends MediaWikiMediaTestCase {
7
8 /** @var PNGHandler */
9 protected $handler;
10
11 protected function setUp() {
12 parent::setUp();
13 $this->handler = new PNGHandler();
14 }
15
16 /**
17 * @covers PNGHandler::getMetadata
18 */
19 public function testInvalidFile() {
20 $res = $this->handler->getMetadata( null, $this->filePath . '/README' );
21 $this->assertEquals( PNGHandler::BROKEN_FILE, $res );
22 }
23
24 /**
25 * @param string $filename Basename of the file to check
26 * @param bool $expected Expected result.
27 * @dataProvider provideIsAnimated
28 * @covers PNGHandler::isAnimatedImage
29 */
30 public function testIsAnimanted( $filename, $expected ) {
31 $file = $this->dataFile( $filename, 'image/png' );
32 $actual = $this->handler->isAnimatedImage( $file );
33 $this->assertEquals( $expected, $actual );
34 }
35
36 public static function provideIsAnimated() {
37 return [
38 [ 'Animated_PNG_example_bouncing_beach_ball.png', true ],
39 [ '1bit-png.png', false ],
40 ];
41 }
42
43 /**
44 * @param string $filename
45 * @param int $expected Total image area
46 * @dataProvider provideGetImageArea
47 * @covers PNGHandler::getImageArea
48 */
49 public function testGetImageArea( $filename, $expected ) {
50 $file = $this->dataFile( $filename, 'image/png' );
51 $actual = $this->handler->getImageArea( $file, $file->getWidth(), $file->getHeight() );
52 $this->assertEquals( $expected, $actual );
53 }
54
55 public static function provideGetImageArea() {
56 return [
57 [ '1bit-png.png', 2500 ],
58 [ 'greyscale-png.png', 2500 ],
59 [ 'Png-native-test.png', 126000 ],
60 [ 'Animated_PNG_example_bouncing_beach_ball.png', 10000 ],
61 ];
62 }
63
64 /**
65 * @param string $metadata Serialized metadata
66 * @param int $expected One of the class constants of PNGHandler
67 * @dataProvider provideIsMetadataValid
68 * @covers PNGHandler::isMetadataValid
69 */
70 public function testIsMetadataValid( $metadata, $expected ) {
71 $actual = $this->handler->isMetadataValid( null, $metadata );
72 $this->assertEquals( $expected, $actual );
73 }
74
75 public static function provideIsMetadataValid() {
76 return [
77 [ PNGHandler::BROKEN_FILE, PNGHandler::METADATA_GOOD ],
78 [ '', PNGHandler::METADATA_BAD ],
79 [ null, PNGHandler::METADATA_BAD ],
80 [ 'Something invalid!', PNGHandler::METADATA_BAD ],
81 // @codingStandardsIgnoreStart Ignore Generic.Files.LineLength.TooLong
82 [
83 'a:6:{s:10:"frameCount";i:0;s:9:"loopCount";i:1;s:8:"duration";d:0;s:8:"bitDepth";i:8;s:9:"colorType";s:10:"truecolour";s:8:"metadata";a:1:{s:15:"_MW_PNG_VERSION";i:1;}}',
84 PNGHandler::METADATA_GOOD
85 ],
86 // @codingStandardsIgnoreEnd
87 ];
88 }
89
90 /**
91 * @param string $filename
92 * @param string $expected Serialized array
93 * @dataProvider provideGetMetadata
94 * @covers PNGHandler::getMetadata
95 */
96 public function testGetMetadata( $filename, $expected ) {
97 $file = $this->dataFile( $filename, 'image/png' );
98 $actual = $this->handler->getMetadata( $file, "$this->filePath/$filename" );
99 // $this->assertEquals( unserialize( $expected ), unserialize( $actual ) );
100 $this->assertEquals( ( $expected ), ( $actual ) );
101 }
102
103 public static function provideGetMetadata() {
104 return [
105 // @codingStandardsIgnoreStart Ignore Generic.Files.LineLength.TooLong
106 [
107 'rgb-na-png.png',
108 'a:6:{s:10:"frameCount";i:0;s:9:"loopCount";i:1;s:8:"duration";d:0;s:8:"bitDepth";i:8;s:9:"colorType";s:10:"truecolour";s:8:"metadata";a:1:{s:15:"_MW_PNG_VERSION";i:1;}}'
109 ],
110 [
111 'xmp.png',
112 'a:6:{s:10:"frameCount";i:0;s:9:"loopCount";i:1;s:8:"duration";d:0;s:8:"bitDepth";i:1;s:9:"colorType";s:14:"index-coloured";s:8:"metadata";a:2:{s:12:"SerialNumber";s:9:"123456789";s:15:"_MW_PNG_VERSION";i:1;}}'
113 ],
114 // @codingStandardsIgnoreEnd
115 ];
116 }
117
118 /**
119 * @param string $filename
120 * @param array $expected Expected standard metadata
121 * @dataProvider provideGetIndependentMetaArray
122 * @covers PNGHandler::getCommonMetaArray
123 */
124 public function testGetIndependentMetaArray( $filename, $expected ) {
125 $file = $this->dataFile( $filename, 'image/png' );
126 $actual = $this->handler->getCommonMetaArray( $file );
127 $this->assertEquals( $expected, $actual );
128 }
129
130 public static function provideGetIndependentMetaArray() {
131 return [
132 [ 'rgb-na-png.png', [] ],
133 [ 'xmp.png',
134 [
135 'SerialNumber' => '123456789',
136 ]
137 ],
138 ];
139 }
140
141 /**
142 * @param string $filename
143 * @param float $expectedLength
144 * @dataProvider provideGetLength
145 */
146 public function testGetLength( $filename, $expectedLength ) {
147 $file = $this->dataFile( $filename, 'image/png' );
148 $actualLength = $file->getLength();
149 $this->assertEquals( $expectedLength, $actualLength, '', 0.00001 );
150 }
151
152 public function provideGetLength() {
153 return [
154 [ 'Animated_PNG_example_bouncing_beach_ball.png', 1.5 ],
155 [ 'Png-native-test.png', 0.0 ],
156 [ 'greyscale-png.png', 0.0 ],
157 [ '1bit-png.png', 0.0 ],
158 ];
159 }
160 }