aboutsummaryrefslogtreecommitdiffstats
path: root/tests/unit/Photo/PhotoGdTest.php
blob: ae7382c430e66bdb3ef28639b45f09dac56c1349 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php

namespace Zotlabs\Tests\Unit\Photo;

use Zotlabs\Photo\PhotoGd;
use phpmock\phpunit\PHPMock;
use Zotlabs\Tests\Unit\UnitTestCase;

/**
 * @brief PhotoGd test case.
 *
 * These tests are not really useful yet, just some obvious behaviour.
 *
 * @todo Compare the actual results.
 * @todo Test different image types.
 */
class PhotoGdTest extends UnitTestCase {

	use PHPMock;

	/**
	 * @var PhotoGd
	 */
	private $photoGd;

	/**
	 * Prepares the environment before running a test.
	 */
	protected function setUp(): void {
		parent::setUp();

		$data = file_get_contents('images/hz-16.png');

		$this->photoGd = new PhotoGd($data, 'image/png');
	}

	/**
	 * Cleans up the environment after running a test.
	 */
	protected function tearDown(): void {
		$this->photoGd = null;

		parent::tearDown();
	}

	/**
	 * Tests PhotoGd->supportedTypes()
	 *
	 * Without mocking gd this check is environment dependent.
	 *
	public function testSupportedTypes() {
		$sft = $this->photoGd->supportedTypes();

		$this->assertArrayHasKey('image/jpeg', $sft);
		$this->assertArrayHasKey('image/gif', $sft);
		$this->assertArrayHasKey('image/png', $sft);

		$this->assertArrayNotHasKey('image/foo', $sft);
	}
	*/

	/**
	 * Tests PhotoGd->clearexif()
	 */
	public function testClearexifIsNotImplementedInGdAndDoesNotAlterImageOrReturnSomething() {
		$data_before = $this->photoGd->getImage();
		$this->assertNull($this->photoGd->clearexif());
		$this->assertSame($data_before, $this->photoGd->getImage());
	}

	/**
	 * Tests PhotoGd->getImage()
	 */
	public function testGetimageReturnsAResource() {
		$res = $this->photoGd->getImage();
		$this->assertIsResource($res);
		$this->assertEquals('gd', get_resource_type($res));
	}
	public function testGetimageReturnsFalseOnFailure() {
		$this->photoGd = new PhotoGd('');
		$this->assertFalse($this->photoGd->getImage());
	}

	/**
	 * Tests PhotoGd->doScaleImage()
	 */
	public function testDoscaleImageSetsCorrectDimensions() {
		$this->photoGd->doScaleImage(5, 8);

		$this->assertSame(5, $this->photoGd->getWidth());
		$this->assertSame(8, $this->photoGd->getHeight());
	}

	/**
	 * Tests PhotoGd->rotate()
	 */
	public function testRotate360DegreesCreatesANewImage() {
		$data = $this->photoGd->getImage();
		$this->photoGd->rotate(360);
		$this->assertNotEquals($data, $this->photoGd->getImage());
	}

	/**
	 * Tests PhotoGd->flip()
	 *
	public function testFlip() {
		// TODO Auto-generated PhotoGdTest->testFlip()
		$this->markTestIncomplete("flip test not implemented");

		$this->photoGd->flip();
	}
	*/

	/**
	 * Tests PhotoGd->cropImageRect()
	 */
	public function testCropimagerectSetsCorrectDimensions() {
		$this->photoGd->cropImageRect(10, 12, 1, 2, 11, 11);

		$this->assertSame(10, $this->photoGd->getWidth());
		$this->assertSame(12, $this->photoGd->getHeight());
	}

	/**
	 * Tests PhotoGd->imageString()
	 */
	public function testImagestringReturnsABinaryString() {
		// Create a stub for global function get_config()
		// get_config('system', 'png_quality')
		// get_config('system', 'jpeg_quality');
		$gc = $this->getFunctionMock('Zotlabs\Photo', 'get_config');
		$gc->expects($this->once())->willReturnCallback(
				function() {
					switch($this->photoGd->getType()){
						case 'image/png':
							return 7;
						case 'image/jpeg':
						default:
							return 70;
					}
				}
		);

		$this->assertIsString($this->photoGd->imageString());
	}

}