Passed
Push — master ( 3c6756...8b3b37 )
by Michael
20:43 queued 12:59
created

BMPTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 29
c 2
b 0
f 0
dl 0
loc 65
rs 10
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testSaveToFile() 0 13 1
A testLoad() 0 7 1
A imageProvider() 0 5 1
A testSaveToString() 0 13 1
A setup() 0 3 1
A teardown() 0 3 1
1
<?php
2
	/**
3
    This file is part of WideImage.
4
5
    WideImage is free software; you can redistribute it and/or modify
6
    it under the terms of the GNU Lesser General Public License as published by
7
    the Free Software Foundation; either version 2.1 of the License, or
8
    (at your option) any later version.
9
10
    WideImage 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 Lesser General Public License for more details.
14
15
    You should have received a copy of the GNU Lesser General Public License
16
    along with WideImage; if not, write to the Free Software
17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
19
    * @package Tests
20
  **/
21
22
namespace Test\WideImage\Mapper;
23
24
use WideImage\WideImage;
25
use WideImage\MapperFactory;
26
use WideImage\vendor\de77;
27
use Test\WideImage_TestCase;
28
29
/**
30
 * @package Tests
31
 */
32
class BMPTest extends WideImage_TestCase
33
{
34
	/**
35
	 * @var WideImage_Mapper_BMP
0 ignored issues
show
Bug introduced by
The type Test\WideImage\Mapper\WideImage_Mapper_BMP was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
36
	 */
37
	protected $mapper;
38
39
	public function setup()
40
	{
41
		$this->mapper = MapperFactory::selectMapper(null, 'bmp');
0 ignored issues
show
Documentation Bug introduced by
It seems like WideImage\MapperFactory:...lectMapper(null, 'bmp') can also be of type false. However, the property $mapper is declared as type Test\WideImage\Mapper\WideImage_Mapper_BMP. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
42
	}
43
44
	public function teardown()
45
	{
46
		$this->mapper = null;
47
	}
48
49
	public function imageProvider()
50
	{
51
		return array(
52
			array(IMG_PATH . 'fgnl.bmp', 174, 287),
53
			array(IMG_PATH . 'bmp' . DIRECTORY_SEPARATOR . 'favicon.ico', 30, 30)
54
		);
55
	}
56
57
	/**
58
	 * @dataProvider imageProvider
59
	 */
60
	public function testLoad($image, $width, $height)
61
	{
62
		$handle = $this->mapper->load($image);
63
		$this->assertTrue(WideImage::isValidImageHandle($handle));
64
		$this->assertEquals($width, imagesx($handle));
65
		$this->assertEquals($height, imagesy($handle));
66
		imagedestroy($handle);
67
	}
68
69
	public function testSaveToString()
70
	{
71
		$handle = de77\BMP::imagecreatefrombmp(IMG_PATH . 'fgnl.bmp');
72
		ob_start();
73
		$this->mapper->save($handle);
74
		$string = ob_get_clean();
75
		$this->assertTrue(strlen($string) > 0);
76
		imagedestroy($handle);
0 ignored issues
show
Bug introduced by
It seems like $handle can also be of type false; however, parameter $image of imagedestroy() does only seem to accept GdImage|resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

76
		imagedestroy(/** @scrutinizer ignore-type */ $handle);
Loading history...
77
78
		// string contains valid image data
79
		$handle = $this->mapper->loadFromString($string);
80
		$this->assertTrue(WideImage::isValidImageHandle($handle));
81
		imagedestroy($handle);
82
	}
83
84
	public function testSaveToFile()
85
	{
86
		$handle = imagecreatefromgif(IMG_PATH . '100x100-color-hole.gif');
87
		$this->mapper->save($handle, IMG_PATH . 'temp' . DIRECTORY_SEPARATOR . 'test.bmp');
88
		$this->assertTrue(filesize(IMG_PATH . 'temp' . DIRECTORY_SEPARATOR . 'test.bmp') > 0);
89
		imagedestroy($handle);
90
91
		// file is a valid image
92
		$handle = $this->mapper->load(IMG_PATH . 'temp' . DIRECTORY_SEPARATOR . 'test.bmp');
93
		$this->assertTrue(WideImage::isValidImageHandle($handle));
94
		imagedestroy($handle);
95
96
		unlink(IMG_PATH . 'temp' . DIRECTORY_SEPARATOR . 'test.bmp');
97
	}
98
}
99