tests: mock parser tests file access
[lhc/web/wiklou.git] / tests / phpunit / mocks / media / MockBitmapHandler.php
1 <?php
2 /**
3 * Fake handler for images.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Media
22 */
23
24 /**
25 * Mock handler for images.
26 *
27 * This is really intended for unit testing.
28 *
29 * @ingroup Media
30 */
31 class MockBitmapHandler extends BitmapHandler {
32
33 /**
34 * Override BitmapHandler::doTransform() making sure we do not generate
35 * a thumbnail at all. That is merely returning a ThumbnailImage that
36 * will be consumed by the unit test. There is no need to create a real
37 * thumbnail on the filesystem.
38 */
39 function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
40 # Example of what we receive:
41 # $image: LocalFile
42 # $dstPath: /tmp/transform_7d0a7a2f1a09-1.jpg
43 # $dstUrl : http://example.com/images/thumb/0/09/Bad.jpg/320px-Bad.jpg
44 # $params: width: 320, descriptionUrl http://trunk.dev/wiki/File:Bad.jpg
45
46 $this->normaliseParams( $image, $params );
47
48 $scalerParams = array(
49 # The size to which the image will be resized
50 'physicalWidth' => $params['physicalWidth'],
51 'physicalHeight' => $params['physicalHeight'],
52 'physicalDimensions' => "{$params['physicalWidth']}x{$params['physicalHeight']}",
53 # The size of the image on the page
54 'clientWidth' => $params['width'],
55 'clientHeight' => $params['height'],
56 # Comment as will be added to the EXIF of the thumbnail
57 'comment' => isset( $params['descriptionUrl'] ) ?
58 "File source: {$params['descriptionUrl']}" : '',
59 # Properties of the original image
60 'srcWidth' => $image->getWidth(),
61 'srcHeight' => $image->getHeight(),
62 'mimeType' => $image->getMimeType(),
63 'dstPath' => $dstPath,
64 'dstUrl' => $dstUrl,
65 );
66
67 # In some cases, we do not bother generating a thumbnail.
68 if ( !$image->mustRender() &&
69 $scalerParams['physicalWidth'] == $scalerParams['srcWidth']
70 && $scalerParams['physicalHeight'] == $scalerParams['srcHeight'] ) {
71 wfDebug( __METHOD__ . ": returning unscaled image\n" );
72 return $this->getClientScalingThumbnailImage( $image, $scalerParams );
73 }
74
75 return new ThumbnailImage( $image, $dstUrl, false, $params );
76 }
77 }