diff options
Diffstat (limited to 'tests/unit')
-rw-r--r-- | tests/unit/Photo/PhotoGdTest.php | 147 | ||||
-rw-r--r-- | tests/unit/get_tags_test.php | 17 | ||||
-rw-r--r-- | tests/unit/includes/PhotodriverTest.php | 39 |
3 files changed, 190 insertions, 13 deletions
diff --git a/tests/unit/Photo/PhotoGdTest.php b/tests/unit/Photo/PhotoGdTest.php new file mode 100644 index 000000000..1d4f9467f --- /dev/null +++ b/tests/unit/Photo/PhotoGdTest.php @@ -0,0 +1,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() { + 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() { + $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()); + } + +} diff --git a/tests/unit/get_tags_test.php b/tests/unit/get_tags_test.php index 40f016747..bdffd8311 100644 --- a/tests/unit/get_tags_test.php +++ b/tests/unit/get_tags_test.php @@ -106,10 +106,9 @@ class GetTagsTest extends PHPUnit_Framework_TestCase { $tags=get_tags($text);
- $inform=''; $str_tags=''; foreach($tags as $tag) { - handle_tag($this->a, $text, $inform, $str_tags, 11, $tag); + handle_tag($text, $str_tags, 11, $tag); } //correct tags found? @@ -117,7 +116,6 @@ class GetTagsTest extends PHPUnit_Framework_TestCase { $this->assertTrue(in_array("@Mike", $tags)); //correct output from handle_tag? - $this->assertEquals("cid:15", $inform); $this->assertEquals("@[url=http://justatest.de]Mike Lastname[/url]", $str_tags); $this->assertEquals("hi @[url=http://justatest.de]Mike Lastname[/url]", $text);
} @@ -135,9 +133,8 @@ class GetTagsTest extends PHPUnit_Framework_TestCase { $this->assertEquals(1, count($tags));
$this->assertTrue(in_array("@Mike.because", $tags)); - $inform='';
$str_tags='';
- handle_tag($this->a, $text, $inform, $str_tags, 11, $tags[0]);
+ handle_tag($text, $str_tags, 11, $tags[0]);
// (mike) - This is a tricky case. // we support mentions as in @mike@example.com - which contains a period. @@ -149,7 +146,6 @@ class GetTagsTest extends PHPUnit_Framework_TestCase { // $this->assertEquals("@[url=http://justatest.de]Mike Lastname[/url]", $str_tags); // $this->assertEquals("hi @[url=http://justatest.de]Mike Lastname[/url].because", $text);
- $this->assertEquals("", $inform); $this->assertEquals("", $str_tags); } @@ -195,13 +191,11 @@ class GetTagsTest extends PHPUnit_Framework_TestCase { $this->assertTrue(in_array("@Mike This", $tags));
$this->assertTrue(in_array("#test_case", $tags));
- $inform=''; $str_tags=''; foreach($tags as $tag) { - handle_tag($this->a, $text, $inform, $str_tags, 11, $tag); + handle_tag($text, $str_tags, 11, $tag); } - $this->assertEquals("cid:15", $inform); $this->assertEquals("@[url=http://justatest.de]Mike Lastname[/url],#[url=baseurl/search?tag=test%20case]test case[/url]", $str_tags); $this->assertEquals("hi @[url=http://justatest.de]Mike Lastname[/url] This is a #[url=baseurl/search?tag=test%20case]test case[/url]", $text); @@ -255,16 +249,13 @@ class GetTagsTest extends PHPUnit_Framework_TestCase { //happens right now, but it shouldn't be necessary $this->assertTrue(in_array("@mike+15 id", $tags)); - $inform='';
$str_tags=''; foreach($tags as $tag) { - handle_tag($this->a, $text, $inform, $str_tags, 11, $tag); + handle_tag($text, $str_tags, 11, $tag); } $this->assertEquals("Test with @[url=http://justatest.de]Mike Lastname[/url] id tag", $text);
$this->assertEquals("@[url=http://justatest.de]Mike Lastname[/url]", $str_tags);
- // this test may produce two cid:15 entries - which is OK because duplicates are pruned before delivery - $this->assertContains("cid:15",$inform); } /** diff --git a/tests/unit/includes/PhotodriverTest.php b/tests/unit/includes/PhotodriverTest.php new file mode 100644 index 000000000..6f6ad0ffe --- /dev/null +++ b/tests/unit/includes/PhotodriverTest.php @@ -0,0 +1,39 @@ +<?php + +namespace Zotlabs\Tests\Unit\includes; + +//use Zotlabs\Photo\PhotoGd; +use Zotlabs\Tests\Unit\UnitTestCase; +//use phpmock\phpunit\PHPMock; + +/** + * @brief Unit Test cases for include/photo/photo_driver.php file. + */ +class PhotodriverTest extends UnitTestCase { + //use PHPMock; + + public function testPhotofactoryReturnsNullForUnsupportedType() { + // php-mock can not mock global functions which is called by a global function. + // If the calling function is in a namespace it would work. + //$logger = $this->getFunctionMock(__NAMESPACE__, 'logger'); + //$logger->expects($this->once()); + + //$ph = \photo_factory('', 'image/bmp'); + //$this->assertNull($ph); + + $this->markTestIncomplete('Need to mock logger(), otherwise not unit testable.'); + } + + public function testPhotofactoryReturnsPhotogdIfConfigIgnore_imagickIsSet() { + // php-mock can not mock global functions which is called by a global function. + // If the calling function is in a namespace it would work. + //$gc = $this->getFunctionMock(__NAMESPACE__, 'get_config'); + // simulate get_config('system', 'ignore_imagick') configured + //$gc->expects($this->once())->willReturn(1) + + //$ph = \photo_factory(file_get_contents('images/hz-16.png'), 'image/png'); + //$this->assertInstanceOf(PhotoGd::class, $ph); + + $this->markTestIncomplete('Need to mock get_config(), otherwise not unit testable.'); + } +}
\ No newline at end of file |