Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed bug when the package doesn't exists, created createErrorImage #49

Merged
merged 3 commits into from
Jun 6, 2013
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
moved redis hostname to the parameter, fixed bug and created the crea…
…teErrorImage
  • Loading branch information
liuggio committed Jun 6, 2013
commit 82f85915a61964d99badc324172776dc2ec6b203
Binary file added app/badge-assets/images/error.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion app/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ snc_redis:
default:
type: predis
alias: default
dsn: redis:https://localhost
dsn: redis:https://%redis_hostname%
1 change: 1 addition & 0 deletions app/config/parameters.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ parameters:

locale: en
secret: ThisTokenIsNotSoSecretChangeIt
redis_hostname: localhost
19 changes: 14 additions & 5 deletions src/PUGX/BadgeBundle/Controller/BadgeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace PUGX\BadgeBundle\Controller;

use Guzzle\Http\Exception\BadResponseException;
use Symfony\Component\DependencyInjection\ContainerAware;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\StreamedResponse;
Expand All @@ -26,9 +27,9 @@
*/
class BadgeController extends ContainerAware
{
CONST ERROR_TEXT_GENERIC = 'ERR 1 ';
CONST ERROR_TEXT_NOT_A_NUMBER = 'ERR 2 ';
CONST ERROR_TEXT_CLIENT_EXCEPTION = 'ERR 3 ';
CONST ERROR_TEXT_GENERIC = 'repository';
CONST ERROR_TEXT_CLIENT_EXCEPTION = 'packagist';
CONST ERROR_TEXT_CLIENT_BAD_RESPONSE = 'not found?';

/**
* Downloads action.
Expand Down Expand Up @@ -58,18 +59,24 @@ public function downloadsAction($repository, $type = 'total')
$imageCreator = $this->container->get('image_creator');
$outputFilename = sprintf('%s.png', $type);
$status = 500;
$image = null;

try {
$package = $this->container->get('package_manager')->fetchPackage($repository);
$text = $this->container->get('package_manager')->getPackageDownloads($package, $type);
$image = $imageCreator->createDownloadsImage($text);
$status = 200;
} catch (BadResponseException $e) {
$text = self::ERROR_TEXT_CLIENT_BAD_RESPONSE;
} catch (UnexpectedValueException $e) {
$text = self::ERROR_TEXT_CLIENT_EXCEPTION;
} catch (\Exception $e) {
$text = self::ERROR_TEXT_GENERIC;
}

$image = $imageCreator->createDownloadsImage($text);
if (null == $image) {
$image = $imageCreator->createErrorImage($text);
}

return $this->streamImage($status, $image, $outputFilename);
}
Expand Down Expand Up @@ -115,14 +122,16 @@ public function versionAction($repository, $latest = 'stable')
}

$status = 200;
} catch (BadResponseException $e) {
$error = self::ERROR_TEXT_CLIENT_BAD_RESPONSE;
} catch (UnexpectedValueException $e) {
$error = self::ERROR_TEXT_CLIENT_EXCEPTION;
} catch (\Exception $e) {
$error = self::ERROR_TEXT_GENERIC;
}

if (null == $image) {
$image = $this->container->get('image_creator')->createStableImage($error);
$image = $this->container->get('image_creator')->createErrorImage($error);
}

return $this->streamImage($status, $image, $outputFilename);
Expand Down
24 changes: 20 additions & 4 deletions src/PUGX/BadgeBundle/Service/ImageCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ImageCreator implements ImageCreatorInterface
{
private $logger;
protected $dispatcher;
protected $imageNames = array('empty' => 'empty.png', 'downloads' => 'downloads.png', 'stable' => 'stable.png', 'unstable' => 'unstable.png');
protected $imageNames = array('empty' => 'empty.png', 'downloads' => 'downloads.png', 'stable' => 'stable.png', 'unstable' => 'unstable.png', 'error' => 'error.png');
protected $imagePath;
protected $fontPath;
protected $defaultFont;
Expand Down Expand Up @@ -188,7 +188,7 @@ private function createImage($imagePath)
}

/**
* Create the 'downloads' image with the standard Font and standard Image.
* Create the 'downloads' image with the standard Font and download image template.
*
* @param string $value
*
Expand All @@ -205,7 +205,7 @@ public function createDownloadsImage($value)
}

/**
* Create the 'stable' image with the standard Font and standard Image.
* Create the 'stable' image with the standard Font and stable image template.
*
* @param string $value
*
Expand All @@ -222,7 +222,7 @@ public function createStableImage($value)
}

/**
* Create the 'stable' image with the standard Font and standard Image.
* Create the 'stable' image with the standard Font and unstable image template.
*
* @param string $value
*
Expand All @@ -237,4 +237,20 @@ public function createUnstableImage($value = '@dev')

return $image;
}

/**
* Create the 'error' image
*
* @param string $value
*
* @return resource
*/
public function createErrorImage($value)
{
$imagePath = $this->imagePath . DIRECTORY_SEPARATOR . $this->imageNames['error'];
$image = $this->createImage($imagePath);
$this->addShadowedText($image, $value, 50, 13.5, 7);

return $image;
}
}
24 changes: 22 additions & 2 deletions src/PUGX/BadgeBundle/Tests/Controller/BadgeControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,20 @@ public function setUp()
{
// see the config_test.yml, there's different parameters and services.
$data = '{"package":{"name":"pugx\/badge-poser","description":"add badges on your readme, such as downloads number or latest version.","time":"2013-05-24T14:45:06+00:00","maintainers":[{"name":"liuggio","email":"[email protected]"}],"versions":{"dev-master":{"name":"pugx\/badge-poser","description":"add badges on your readme, such as downloads number or latest version.","keywords":[],"homepage":"","version":"dev-master","version_normalized":"9999999-dev","license":[],"authors":[],"source":{"type":"git","url":"https:\/\/github.com\/PUGX\/badge-poser.git","reference":"024df1d420cd715aea3400bfea9b87ed0f3bb47e"},"dist":{"type":"zip","url":"https:\/\/api.github.com\/repos\/PUGX\/badge-poser\/zipball\/024df1d420cd715aea3400bfea9b87ed0f3bb47e","reference":"024df1d420cd715aea3400bfea9b87ed0f3bb47e","shasum":""},"type":"library","time":"2013-05-24T19:19:25+00:00","autoload":{"psr-0":{"":"src\/"}},"extra":{"symfony-app-dir":"app","symfony-web-dir":"web","branch-alias":{"dev-master":"0.1-dev"}},"require":{"php":">=5.3.3","symfony\/symfony":"2.2.*","doctrine\/orm":">=2.2,<3.0,>=2.2.3","doctrine\/doctrine-bundle":"1.2.*","twig\/extensions":"1.0.*","symfony\/assetic-bundle":"2.1.*","symfony\/swiftmailer-bundle":"2.2.*","symfony\/monolog-bundle":"2.2.*","sensio\/distribution-bundle":"2.2.*","sensio\/framework-extra-bundle":"2.2.*","sensio\/generator-bundle":"2.2.*","jms\/security-extra-bundle":"1.4.*","jms\/di-extra-bundle":"1.3.*","knplabs\/packagist-api":"dev-master"},"require-dev":{"guzzle\/plugin-mock":"*"}},"dev-develop":{"name":"pugx\/badge-poser","description":"add badges on your readme, such as downloads number or latest version.","keywords":[],"homepage":"","version":"dev-develop","version_normalized":"dev-develop","license":[],"authors":[],"source":{"type":"git","url":"https:\/\/github.com\/PUGX\/badge-poser.git","reference":"024df1d420cd715aea3400bfea9b87ed0f3bb47e"},"dist":{"type":"zip","url":"https:\/\/api.github.com\/repos\/PUGX\/badge-poser\/zipball\/024df1d420cd715aea3400bfea9b87ed0f3bb47e","reference":"024df1d420cd715aea3400bfea9b87ed0f3bb47e","shasum":""},"type":"library","time":"2013-05-24T19:19:25+00:00","autoload":{"psr-0":{"":"src\/"}},"extra":{"symfony-app-dir":"app","symfony-web-dir":"web","branch-alias":{"dev-master":"0.1-dev"}},"require":{"php":">=5.3.3","symfony\/symfony":"2.2.*","doctrine\/orm":">=2.2,<3.0,>=2.2.3","doctrine\/doctrine-bundle":"1.2.*","twig\/extensions":"1.0.*","symfony\/assetic-bundle":"2.1.*","symfony\/swiftmailer-bundle":"2.2.*","symfony\/monolog-bundle":"2.2.*","sensio\/distribution-bundle":"2.2.*","sensio\/framework-extra-bundle":"2.2.*","sensio\/generator-bundle":"2.2.*","jms\/security-extra-bundle":"1.4.*","jms\/di-extra-bundle":"1.3.*","knplabs\/packagist-api":"dev-master"},"require-dev":{"guzzle\/plugin-mock":"*"}}},"type":"library","repository":"https:\/\/github.com\/PUGX\/badge-poser","downloads":{"total":99,"monthly":12,"daily":9},"favers":9}}';
$packagistResponse = new \Guzzle\Http\Message\Response(200);
$this->packagistClient = $this->createPackagistClient($data, 200);
}

private function createPackagistClient($data, $status = 200)
{

$packagistResponse = new \Guzzle\Http\Message\Response($status);
$packagistResponse->setBody($data);
$plugin = new \Guzzle\Plugin\Mock\MockPlugin();
$plugin->addResponse($packagistResponse);
$clientHttp = new \Guzzle\Http\Client();
$clientHttp->addSubscriber($plugin);

$this->packagistClient = new Client($clientHttp);
return new Client($clientHttp);
}

public function testDownloadsAction()
Expand Down Expand Up @@ -55,4 +61,18 @@ public function testLatestUnstableAction()

$this->assertTrue($client->getResponse()->isSuccessful());
}

public function testPackageDoesntExists()
{
$data = '{"status":"error","message":"Package not found"}';

$packagistClient = $this->createPackagistClient($data, 500);

$client = static::createClient();
static::$kernel->getContainer()->set('packagist_client', $packagistClient);
$crawler = $client->request('GET', '/pugx/microsoft-lover/d/total.png');

$this->assertFalse($client->getResponse()->getContent());
$this->assertTrue($client->getResponse()->isServerError());
}
}