Skip to content

Commit

Permalink
Merge pull request PUGX#23 from toretto460/master
Browse files Browse the repository at this point in the history
Added unit tests for ImageCreator
  • Loading branch information
leopro committed May 31, 2013
2 parents 135d4d7 + 0960107 commit d931ad4
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 9 deletions.
8 changes: 2 additions & 6 deletions src/PUGX/BadgeBundle/Service/ImageCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
85 changes: 82 additions & 3 deletions src/PUGX/BadgeBundle/Tests/Service/ImageCreatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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')
);
}
}

0 comments on commit d931ad4

Please sign in to comment.