'jpeg.jpg', 'png' => 'png.png', 'gif' => 'gif_nonanimated.gif', 'animated' => 'gif_animated.gif', 'nonexistant' => 'nonexisting.jpg', 'invalid' => 'text.txt', ); protected $testFiltersSuccess = array(); protected $testFiltersFailure = array(); protected $converter; protected $basePath; protected $testPath; public static function suite() { return new ezcTestSuite( "ezcImageConversionTransformationTest" ); } /** * setUp * * @access public */ public function setUp() { $this->testFiltersSuccess = array( 0 => array( 0 => new ezcImageFilter( 'scaleExact', array( 'width' => 50, 'height' => 50, 'direction' => ezcImageGeometryFilters::SCALE_BOTH, ) ), 1 => new ezcImageFilter( 'crop', array( 'xStart' => 10, 'xEnd' => 40, 'yStart' => 10, 'yEnd' => 40, ) ), 2 => new ezcImageFilter( 'colorspace', array( 'space' => ezcImageColorspaceFilters::COLORSPACE_GREY, ) ), ), 1 => array( 0 => new ezcImageFilter( 'scale', array( 'width' => 50, 'height' => 1000, 'direction' => ezcImageGeometryFilters::SCALE_DOWN, ) ), 2 => new ezcImageFilter( 'colorspace', array( 'space' => ezcImageColorspaceFilters::COLORSPACE_MONOCHROME, ) ), ), 2 => array( 0 => new ezcImageFilter( 'scaleHeight', array( 'height' => 70, 'direction' => ezcImageGeometryFilters::SCALE_BOTH, ) ), 2 => new ezcImageFilter( 'colorspace', array( 'space' => ezcImageColorspaceFilters::COLORSPACE_SEPIA, ) ), ), ); $this->testFiltersFailure = array( // Nonexistant filter 0 => array( 0 => new ezcImageFilter( 'toby', array( 'width' => 50, 'height' => 50, 'direction' => ezcImageGeometryFilters::SCALE_BOTH, ) ), 1 => new ezcImageFilter( 'crop', array( 'xStart' => 10, 'xEnd' => 40, 'yStart' => 10, 'yEnd' => 40, ) ), 2 => new ezcImageFilter( 'colorspace', array( 'space' => ezcImageColorspaceFilters::COLORSPACE_GREY, ) ), ), // Missing option 1 => array( 0 => new ezcImageFilter( 'scale', array( ) ), 2 => new ezcImageFilter( 'colorspace', array( 'space' => ezcImageColorspaceFilters::COLORSPACE_MONOCHROME, ) ), ), ); static $i = 1; $this->basePath = dirname( __FILE__ ) . '/data/'; $conversionsIn = array( 'image/gif' => 'image/png', 'image/xpm' => 'image/jpeg', 'image/wbmp' => 'image/jpeg', ); $settings = new ezcImageConverterSettings( array( new ezcImageHandlerSettings( 'GD', 'ezcImageGdHandler' ) ), $conversionsIn ); $this->converter = new ezcImageConverter( $settings ); } /** * tearDown * * @access public */ public function tearDown() { unset( $this->converter ); // $this->removeTempDir(); } public function testConstructSuccess() { $filtersIn = array( 0 => new ezcImageFilter( 'scale', array( 'width' => 50, 'height' => 50, 'direction' => ezcImageGeometryFilters::SCALE_BOTH, ) ), 1 => new ezcImageFilter( 'scaleWidth', array( 'width' => 40, 'direction' => ezcImageGeometryFilters::SCALE_BOTH, ) ), 2 => new ezcImageFilter( 'crop', array( 'xStart' => 10, 'xEnd' => 30, 'yStart' => 10, 'yEnd' => 30, ) ), ); $mimeIn = array( 'image/jpeg' ); $trans = new ezcImageTransformation( $this->converter, 'test', $filtersIn, $mimeIn ); $transArr = (array)$trans; $filters = $transArr["\0*\0filters"]; $mimeOut = $transArr["\0*\0mimeOut"]; $this->assertEquals( $mimeIn, $mimeOut, 'MIME types not registered correctly in transformation.' ); $this->assertEquals( $filtersIn, $filters, 'Filters not registered correctly in transformation.' ); } public function testConstructFailure_1() { $filtersIn = array( 0 => new ezcImageFilter( 'toby', array( 'width' => 50, 'height' => 50, 'direction' => ezcImageGeometryFilters::SCALE_BOTH, ) ), ); $mimeIn = array( 'image/jpeg' ); try { $trans = new ezcImageTransformation( $this->converter, 'test', $filtersIn, $mimeIn ); } catch ( ezcImageFiltersException $e ) { $this->assertEquals( ezcImageFiltersException::NOT_AVAILABLE, $e->getCode(), 'Transformation threw incorrect exception on invalid filter.' ); return; } $this->fail( 'Transformation did not throw exception on invalid filter.' ); } public function testConstructFailure_2() { $filtersIn = array( 0 => new ezcImageFilter( 'scale', array( 'width' => 50, 'height' => 50, 'direction' => ezcImageGeometryFilters::SCALE_BOTH, ) ), ); $mimeIn = array( 'application/toby' ); try { $trans = new ezcImageTransformation( $this->converter, 'test', $filtersIn, $mimeIn ); } catch ( ezcImageConverterException $e ) { $this->assertEquals( ezcImageConverterException::MIMETYPE_NOT_AVAILABLE, $e->getCode(), 'Transformation threw incorrect exception on invalid MIME type.' ); return; } $this->fail( 'Transformation did not throw exception on invalid MIME type.' ); } public function testAddFilterSuccess() { $filtersIn = array( 0 => new ezcImageFilter( 'scale', array( 'width' => 50, 'height' => 50, 'direction' => ezcImageGeometryFilters::SCALE_BOTH, ) ), ); $newFilter = new ezcImageFilter( 'scaleWidth', array( 'width' => 40, 'direction' => ezcImageGeometryFilters::SCALE_BOTH, ) ); $filtersOut = $filtersIn; $filtersOut[] = $newFilter; $mimeIn = array( 'image/jpeg' ); $trans = new ezcImageTransformation( $this->converter, 'test', $filtersIn, $mimeIn ); $trans->addFilter( $newFilter ); $transArr = (array)$trans; $filters = $transArr["\0*\0filters"]; $this->assertEquals( $filtersOut, $filters, 'Filters not added correctly to transformation.' ); } public function testAddFilterFailure() { $filtersIn = array( 0 => new ezcImageFilter( 'scale', array( 'width' => 50, 'height' => 50, 'direction' => ezcImageGeometryFilters::SCALE_BOTH, ) ), ); $newFilter = new ezcImageFilter( 'toby', array( 'width' => 40, 'direction' => ezcImageGeometryFilters::SCALE_BOTH, ) ); $filtersOut = $filtersIn; $filtersOut[] = $newFilter; $mimeIn = array( 'image/jpeg' ); $trans = new ezcImageTransformation( $this->converter, 'test', $filtersIn, $mimeIn ); try { $trans->addFilter( $newFilter ); } catch ( ezcImageFiltersException $e ) { $this->assertEquals( ezcImageFiltersException::NOT_AVAILABLE, $e->getCode(), 'Transformation threw incorrect exception on invalid filter.' ); return; } $this->fail( 'Transformation did not throw exception on invalid filter.' ); } public function testGetOutMimeSuccess_1() { $filtersIn = array( 0 => new ezcImageFilter( 'scale', array( 'width' => 50, 'height' => 50, 'direction' => ezcImageGeometryFilters::SCALE_BOTH, ) ), ); $mimeIn = array( 'image/jpeg' ); $trans = new ezcImageTransformation( $this->converter, 'test', $filtersIn, $mimeIn ); $this->assertEquals( 'image/jpeg', $trans->getOutMime( $this->basePath . $this->testFiles['jpeg'] ), 'Transformation returned incorrect output MIME type.' ); } public function testGetOutMimeSuccess_2() { $filtersIn = array( 0 => new ezcImageFilter( 'scale', array( 'width' => 50, 'height' => 50, 'direction' => ezcImageGeometryFilters::SCALE_BOTH, ) ), ); $mimeIn = array( 'image/jpeg', 'image/png' ); $trans = new ezcImageTransformation( $this->converter, 'test', $filtersIn, $mimeIn ); $this->assertEquals( 'image/png', $trans->getOutMime( $this->basePath . $this->testFiles['gif'] ), 'Transformation returned incorrect output MIME type.' ); } public function testGetOutMimeSuccess_3() { $filtersIn = array( 0 => new ezcImageFilter( 'scale', array( 'width' => 50, 'height' => 50, 'direction' => ezcImageGeometryFilters::SCALE_BOTH, ) ), ); $mimeIn = array( 'image/jpeg' ); $trans = new ezcImageTransformation( $this->converter, 'test', $filtersIn, $mimeIn ); $this->assertEquals( 'image/jpeg', $trans->getOutMime( $this->basePath . $this->testFiles['gif'] ), 'Transformation returned incorrect output MIME type.' ); } public function testTransformSuccessPng_1() { $this->commonTransformTestSuccess( 'png', 0, 'b1d842a9d1f1558c8f6e2db67a90ab30', __METHOD__ ); } public function testTransformSuccessPng_2() { $this->commonTransformTestSuccess( 'png', 1, 'f68c2cfeb59e835a50c22e2935c97721', __METHOD__ ); } public function testTransformSuccessPng_3() { $this->commonTransformTestSuccess( 'png', 2, '7d767ecd7b180b9927a0f765355a6ab0', __METHOD__ ); } public function testTransformSuccessJpeg_1() { $this->commonTransformTestSuccess( 'jpeg', 0, '6189790a171de230781c13a51cd05713', __METHOD__ ); } public function testTransformSuccessJpeg_2() { $this->commonTransformTestSuccess( 'jpeg', 1, 'ab06a96e6af122a972bd86159b71b228', __METHOD__ ); } public function testTransformSuccessJpeg_3() { $this->commonTransformTestSuccess( 'jpeg', 2, 'f1a2fede0c65f3d807c66f39f090b399', __METHOD__ ); } public function testTransformSuccessGif_1() { $this->commonTransformTestSuccess( 'gif', 0, 'fac15d3387899936b4c74417b4a1f1a2', __METHOD__ ); } public function testTransformSuccessGif_2() { $this->commonTransformTestSuccess( 'gif', 1, 'acf997971e72d3857ae2f2b9e670f8dd', __METHOD__ ); } public function testTransformSuccessGif_3() { $this->commonTransformTestSuccess( 'gif', 2, '9c505c2263a65dec7a6167d4f0ae0de1', __METHOD__ ); } public function testTransformSuccessGifAnimated() { $this->commonTransformTestSuccess( 'animated', 2, '9a6e0141efc695a7bca2c0889b329935', __METHOD__ ); } public function testTransformFailure_1() { $this->commonTransformTestFailure( 'jpeg', 0, 'ezcImageFiltersException', ezcImageFiltersException::NOT_AVAILABLE, __METHOD__ ); } public function testTransformFailure_2() { $this->commonTransformTestFailure( 'jpeg', 1, 'ezcImageTransformationException', 0, __METHOD__ ); } protected function commonTransformTestSuccess( $inFileRef, $filtersRef, $md5sum, $name ) { $inFile = $this->basePath . $this->testFiles[$inFileRef]; $outFile = $this->createTempDir( str_replace( '::', '_', $name ) . '_' ) . '/result'; $mimeIn = array( 'image/jpeg', 'image/png' ); $trans = new ezcImageTransformation( $this->converter, 'test', $this->testFiltersSuccess[$filtersRef], $mimeIn ); $trans->transform( $inFile, $outFile ); // Uncomment to get md5 sums: // echo "\n$name: ".md5_file( $outFile )."\n\n"; $this->assertEquals( $md5sum, md5_file( $outFile ), 'Transformation did not produce correct output.' ); // Comment this for visual inspection $this->removeTempDir(); } protected function commonTransformTestFailure( $inFileRef, $filtersRef, $exceptionClass, $exceptionCode, $name ) { $inFile = $this->basePath . $this->testFiles[$inFileRef]; $outFile = $this->createTempDir( str_replace( '::', '_', $name ) . '_' ) . '/result'; $mimeIn = array( 'image/jpeg', 'image/png' ); try { $trans = new ezcImageTransformation( $this->converter, 'test', $this->testFiltersFailure[$filtersRef], $mimeIn ); $trans->transform( $inFile, $outFile ); } catch ( Exception $e ) { $this->assertTrue( get_class( $e ) === $exceptionClass, 'Transformation threw incorrect exception class <'.get_class( $e ).'> on invalid data <'.$filtersRef.'>.' ); $this->assertEquals( $exceptionCode, $e->getCode(), 'Transformation did not throw the correct exception code <'.$exceptionCode.'> on invalid data <'.$filtersRef.'>, but: <'.$e->getCode().'>.' ); $this->removeTempDir(); return; } // If we failed, this stays. $this->removeTempDir(); $this->fail( 'Transformation did not throw exception on invalid data.' ); } } ?>