From 09601072c2dd6b1c7293de151c37efaf1ce0442f Mon Sep 17 00:00:00 2001 From: Toretto460 Date: Fri, 31 May 2013 16:50:38 +0200 Subject: [PATCH] Added unit tests for ImageCreator --- src/PUGX/BadgeBundle/Service/ImageCreator.php | 8 +- .../Tests/Service/ImageCreatorTest.php | 85 ++++++++++++++++++- 2 files changed, 84 insertions(+), 9 deletions(-) diff --git a/src/PUGX/BadgeBundle/Service/ImageCreator.php b/src/PUGX/BadgeBundle/Service/ImageCreator.php index 1704fb8a..e01bcc9e 100644 --- a/src/PUGX/BadgeBundle/Service/ImageCreator.php +++ b/src/PUGX/BadgeBundle/Service/ImageCreator.php @@ -30,20 +30,16 @@ class ImageCreator protected $defaultFont; protected $defaultImage; - public function __construct(Logger $logger, $fontPath, $imagePath, $defaultFont = null, $defaultImage = null) + public function __construct(Logger $logger, $fontPath, $imagePath, $defaultFont = 'DroidSans.ttf', $defaultImage = null) { $this->logger = $logger; $this->fontPath = $fontPath; $this->imagePath = $imagePath; - if (!$defaultFont) { - $defaultFont = 'DroidSans.ttf'; - } if (!$defaultImage) { - $defaultImage = $this->imageNames['empty']; + $this->defaultImage = $this->imageNames['empty']; } $this->defaultFont = $defaultFont; - $this->defaultImage = $defaultImage; } /** diff --git a/src/PUGX/BadgeBundle/Tests/Service/ImageCreatorTest.php b/src/PUGX/BadgeBundle/Tests/Service/ImageCreatorTest.php index 7c97963d..0ef7a6ff 100644 --- a/src/PUGX/BadgeBundle/Tests/Service/ImageCreatorTest.php +++ b/src/PUGX/BadgeBundle/Tests/Service/ImageCreatorTest.php @@ -20,13 +20,24 @@ class ImageCreatorTest extends WebTestCase private $logger; private $packagistClient; + private $fontPath; + private $imagesPath; + private $imageCreator; - public function setUp() { + public function setUp() + { $this->logger = $this->getMockBuilder('Symfony\Bridge\Monolog\Logger') ->disableOriginalConstructor() ->getMock(); $this->packagistClient = $this->getMock('Packagist\Api\Client'); + + $kernelDir = $_SERVER['KERNEL_DIR']; + + $this->fontPath = $kernelDir . '/badge-assets/fonts'; + $this->imagesPath = $kernelDir . '/badge-assets/images'; + + $this->imageCreator = new ImageCreator($this->logger, $this->fontPath, $this->imagesPath); } public static function provider() @@ -56,10 +67,78 @@ public function testNumberToTextConversion($input, $output, $withException) if (null !== $withException) { $this->setExpectedException($withException); } - $imageCreator = new ImageCreator($this->logger, 'font', 'image'); - $res = $imageCreator->transformNumberToReadableFormat($input); + + $res = $this->imageCreator->transformNumberToReadableFormat($input); if (null === $withException) { $this->assertEquals($output, $res); } } + + /** + * @expectedException \ErrorException + */ + public function testAddShadowedText_withBadImage() + { + + $reflectionMethod = new \ReflectionMethod($this->imageCreator, 'addShadowedText'); + $reflectionMethod->setAccessible(true); + + $reflectionMethod->invokeArgs($this->imageCreator, array(false, 'test text')); + } + + + public function provideShadow() + { + return array( + array(false, 'downloads.png'), + array(true, 'downloads.png') + ); + } + + /** + * + * @dataProvider provideShadow + */ + public function testAddShadowedText_maintainsOriginalDimension($withShadow, $imageFile) + { + $reflectionMethod = new \ReflectionMethod($this->imageCreator, 'addShadowedText'); + $reflectionMethod->setAccessible(true); + + $image = imagecreatefrompng($this->imagesPath . DIRECTORY_SEPARATOR . $imageFile); + $this->assertTrue(is_resource($image)); + $expectedWidth = imagesx($image); + $expectedHeight = imagesy($image); + + $reflectionMethod->invokeArgs($this->imageCreator, array($image, 'TEST_TEXT', 3, 13, 8.5, null, $withShadow)); + + $this->assertEquals($expectedWidth, imagesx($image), 'The method should not modify the image width'); + $this->assertEquals($expectedHeight, imagesy($image), 'The method should not modify the image height'); + + imagedestroy($image); + } + + public function testCreateImage() + { + $reflectionMethod = new \ReflectionMethod($this->imageCreator, 'createImage'); + $reflectionMethod->setAccessible(true); + $image = $reflectionMethod->invokeArgs( + $this->imageCreator, + array($this->imagesPath . DIRECTORY_SEPARATOR . 'empty.png') + ); + + $this->assertTrue(is_resource($image)); + } + + /** + * @expectedException \ErrorException + */ + public function testCreateImage_throwException() + { + $reflectionMethod = new \ReflectionMethod($this->imageCreator, 'createImage'); + $reflectionMethod->setAccessible(true); + $image = $reflectionMethod->invokeArgs( + $this->imageCreator, + array($this->imagesPath . DIRECTORY_SEPARATOR . 'invalid_file.png') + ); + } }