From e32634b542e5166ec1497ac2d9be3beb1f30ba97 Mon Sep 17 00:00:00 2001 From: Andrea Giuliano Date: Thu, 22 May 2014 12:59:56 +0200 Subject: [PATCH] add aliases for svg images --- app/config/config.yml | 4 +- app/config/routing.yml | 8 +- .../Controller/BadgeController.php | 1 + .../Controller/PngBadgeController.php | 138 ------------------ .../config/routing/routing_aliases.yml | 45 ++++++ .../config/routing/routing_redirects.yml | 53 +++++++ .../Controller/PngBadgeControllerTest.php | 52 ------- .../Controller/SnippetControllerTest.php | 3 +- src/PUGX/BadgeBundle/Tests/RedirectsTest.php | 40 +++++ 9 files changed, 149 insertions(+), 195 deletions(-) delete mode 100644 src/PUGX/BadgeBundle/Controller/PngBadgeController.php create mode 100644 src/PUGX/BadgeBundle/Resources/config/routing/routing_aliases.yml create mode 100644 src/PUGX/BadgeBundle/Resources/config/routing/routing_redirects.yml delete mode 100644 src/PUGX/BadgeBundle/Tests/Controller/PngBadgeControllerTest.php create mode 100644 src/PUGX/BadgeBundle/Tests/RedirectsTest.php diff --git a/app/config/config.yml b/app/config/config.yml index 31bb36af..d4548156 100644 --- a/app/config/config.yml +++ b/app/config/config.yml @@ -40,8 +40,8 @@ pugx_badge: - { name: 'total', label: 'Total Downloads', route: "pugx_badge_download" } - { name: 'latest_unstable_version', label: 'Latest Unstable Version', route: "pugx_badge_version_latest", latest: 'unstable'} - { name: 'license', label: 'License', route: "pugx_badge_license"} - - { name: 'monthly', label: 'Monthly Downloads', route: "pugx_badge_download_type_png", type: 'monthly'} - - { name: 'daily', label: 'Daily Downloads', route: "pugx_badge_download_type_png", type: 'daily'} + - { name: 'monthly', label: 'Monthly Downloads', route: "pugx_badge_download_type", type: 'monthly'} + - { name: 'daily', label: 'Daily Downloads', route: "pugx_badge_download_type", type: 'daily'} - { name: 'version', label: 'Version', route: "pugx_badge_version" } diff --git a/app/config/routing.yml b/app/config/routing.yml index 89c4b064..19324423 100644 --- a/app/config/routing.yml +++ b/app/config/routing.yml @@ -15,4 +15,10 @@ pugx_badge_shieldio: host: "img.shields.io" requirements: vendor: "[A-Za-z0-9_.-]+" - value: "[A-Za-z0-9_.-]+" \ No newline at end of file + value: "[A-Za-z0-9_.-]+" + +redirects: + resource: "@PUGXBadgeBundle/Resources/config/routing/routing_redirects.yml" + +aliases: + resource: "@PUGXBadgeBundle/Resources/config/routing/routing_aliases.yml" diff --git a/src/PUGX/BadgeBundle/Controller/BadgeController.php b/src/PUGX/BadgeBundle/Controller/BadgeController.php index 5d5b2988..8c8636a7 100644 --- a/src/PUGX/BadgeBundle/Controller/BadgeController.php +++ b/src/PUGX/BadgeBundle/Controller/BadgeController.php @@ -29,6 +29,7 @@ * @author Giulio De Donato * @author Leonardo Proietti * @author Simone Fumagalli + * @author Andrea Giuliano */ class BadgeController extends Controller { diff --git a/src/PUGX/BadgeBundle/Controller/PngBadgeController.php b/src/PUGX/BadgeBundle/Controller/PngBadgeController.php deleted file mode 100644 index 1aa915d7..00000000 --- a/src/PUGX/BadgeBundle/Controller/PngBadgeController.php +++ /dev/null @@ -1,138 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace PUGX\BadgeBundle\Controller; - -use Symfony\Bundle\FrameworkBundle\Controller\Controller; -use Symfony\Component\HttpFoundation\StreamedResponse; -use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; -use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; -use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache; - -/** - * Class PngBadgeController is deprecated see BadgeController - * - * @author Giulio De Donato - * @author Leonardo Proietti - * @author Simone Fumagalli - * - * @deprecated - */ -class PngBadgeController extends Controller -{ - /** - * Png Downloads action. - * - * @param string $repository repository - * @param string $type badge type - * - * @Route("/{repository}/downloads.png", - * name = "pugx_badge_download_png", - * requirements = { - * "repository" = "[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+?" - * } - * ) - * @Method({"GET"}) - * - * @return RedirectResponse - */ - public function downloadsTotalPNGAction($repository, $type = 'total') - { - return $this->redirect($this->generateUrl('pugx_badge_download', array('repository' => $repository)), 301); - } - - /** - * Downloads action. - * - * @param $repository - * @param string $type - * - * @Route("/{repository}/d/{type}.png", - * name = "pugx_badge_download_type_png", - * defaults = {"type" = "total"}, - * requirements = { - * "type" = "total|daily|monthly", - * "repository" = "[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+?" - * } - * ) - * @Method({"GET"}) - * - * @return RedirectResponse - */ - public function downloadsPNGAction($repository, $type = 'total') - { - return $this->redirect($this->generateUrl('pugx_badge_download_type', array('repository' => $repository, 'type' => $type)), 301); - } - - /** - * Version action. - * - * @param string $repository repository - * @param string $latest latest - * - * @Route("/{repository}/version.png", - * name="pugx_badge_version_png", - * requirements={"repository" = "[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+?"} - * ) - * - * @Method({"GET"}) - * @Cache(maxage="3600", smaxage="3600", public=true) - * - * @return StreamedResponse - */ - public function versionPngAction($repository, $latest = 'stable') - { - return $this->redirect($this->generateUrl('pugx_badge_version', array('repository' => $repository)), 301); - } - /** - * Version action. - * - * @param string $repository repository - * @param string $latest latest - * - * @Route("/{repository}/v/{latest}.png", - * name = "pugx_badge_version_latest_png", - * defaults = {"latest" = "stable"}, - * requirements = { - * "type" = "stable|unstable", - * "repository" = "[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+?" - * } - * ) - * @Method({"GET"}) - * @Cache(maxage="3600", smaxage="3600", public=true) - * - * @return StreamedResponse - */ - public function versionPngLatestAction($repository, $latest = 'stable') - { - return $this->redirect($this->generateUrl('pugx_badge_version_latest', array('repository' => $repository, 'latest' => $latest)), 301); - } - - /** - * License action. - * - * @param string $repository repository - * - * @Route("/{repository}/license.png", - * name="pugx_badge_license_png", - * requirements={"repository" = "[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+?"} - * ) - * - * @Method({"GET"}) - * @Cache(maxage="3600", smaxage="3600", public=true) - * - * @return StreamedResponse - */ - public function licensePngAction($repository) - { - return $this->redirect($this->generateUrl('pugx_badge_license', array('repository'=>$repository)), 301); - } -} diff --git a/src/PUGX/BadgeBundle/Resources/config/routing/routing_aliases.yml b/src/PUGX/BadgeBundle/Resources/config/routing/routing_aliases.yml new file mode 100644 index 00000000..71b03994 --- /dev/null +++ b/src/PUGX/BadgeBundle/Resources/config/routing/routing_aliases.yml @@ -0,0 +1,45 @@ +download_svg_alias: + path: /{repository}/downloads.svg + defaults: + _controller: PUGXBadgeBundle:Badge:downloads + type: "total" + requirements: + repository: "[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+?" + methods: [GET] + +download_type_svg_alias: + path: /{repository}/d/{type}.svg + defaults: + _controller: PUGXBadgeBundle:Badge:downloads + type: "total" + requirements: + repository: "[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+?" + type: "total|daily|monthly" + methods: [GET] + +badge_version_svg_alias: + path: /{repository}/version.svg + defaults: + _controller: PUGXBadgeBundle:Badge:version + latest: "stable" + requirements: + repository: "[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+?" + methods: [GET] + +version_latest_svg_alias: + path: /{repository}/v/{latest}.svg + defaults: + _controller: PUGXBadgeBundle:Badge:version + latest: "stable" + requirements: + repository: "[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+?" + latest: "stable|unstable" + methods: [GET] + +badge_license_svg_alias: + path: /{repository}/license.svg + defaults: + _controller: PUGXBadgeBundle:Badge:license + requirements: + repository: "[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+?" + methods: [GET] \ No newline at end of file diff --git a/src/PUGX/BadgeBundle/Resources/config/routing/routing_redirects.yml b/src/PUGX/BadgeBundle/Resources/config/routing/routing_redirects.yml new file mode 100644 index 00000000..cafecc5b --- /dev/null +++ b/src/PUGX/BadgeBundle/Resources/config/routing/routing_redirects.yml @@ -0,0 +1,53 @@ +download_png_redirect: + path: /{repository}/downloads.png + defaults: + _controller: FrameworkBundle:Redirect:redirect + route: pugx_badge_download + permanent: true + requirements: + repository: "[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+?" + methods: [GET] + +download_type_png_redirect: + path: /{repository}/d/{type}.png + defaults: + _controller: FrameworkBundle:Redirect:redirect + route: pugx_badge_download_type + permanent: true + type: "total" + requirements: + repository: "[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+?" + type: "total|daily|monthly" + methods: [GET] + +badge_version_png_redirect: + path: /{repository}/version.png + defaults: + _controller: FrameworkBundle:Redirect:redirect + route: pugx_badge_version + permanent: true + requirements: + repository: "[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+?" + methods: [GET] + +version_latest_png_redirect: + path: /{repository}/v/{latest}.png + defaults: + _controller: FrameworkBundle:Redirect:redirect + route: pugx_badge_version_latest + permanent: true + latest: "stable" + requirements: + repository: "[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+?" + type: "stable|unstable" + methods: [GET] + +badge_license_png_redirect: + path: /{repository}/license.png + defaults: + _controller: FrameworkBundle:Redirect:redirect + route: pugx_badge_license + permanent: true + requirements: + repository: "[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+?" + methods: [GET] \ No newline at end of file diff --git a/src/PUGX/BadgeBundle/Tests/Controller/PngBadgeControllerTest.php b/src/PUGX/BadgeBundle/Tests/Controller/PngBadgeControllerTest.php deleted file mode 100644 index d37361f2..00000000 --- a/src/PUGX/BadgeBundle/Tests/Controller/PngBadgeControllerTest.php +++ /dev/null @@ -1,52 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace PUGX\BadgeBundle\Tests\Controller; - -use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; - -class PngBadgeControllerTest extends WebTestCase -{ - public function testDownloadsPngActionShouldRedirectToSvg() - { - $client = static::createClient(); - $client->request('GET', '/pugx/badge-poser/d/total.png'); - $this->assertTrue($client->getResponse()->isRedirect('/pugx/badge-poser/d/total')); - } - - public function testLatestStableAction() - { - $client = static::createClient(); - $client->request('GET', '/pugx/badge-poser/version.png'); - $this->assertTrue($client->getResponse()->isRedirect('/pugx/badge-poser/version')); - } - - public function testLatestUnstableAction() - { - $client = static::createClient(); - $client->request('GET', '/pugx/badge-poser/v/unstable.png'); - $this->assertTrue($client->getResponse()->isRedirect('/pugx/badge-poser/v/unstable')); - } - - public function testLicenseAction() - { - $client = static::createClient(); - $client->request('GET', '/pugx/badge-poser/license.png'); - $this->assertTrue($client->getResponse()->isRedirect('/pugx/badge-poser/license')); - } - - public function testIfPackageDoesntExist() - { - $client = static::createClient(); - $client->request('GET', '/pugx/microsoft-lover/d/total.png'); - $this->assertTrue($client->getResponse()->isRedirect('/pugx/microsoft-lover/d/total')); - } -} diff --git a/src/PUGX/BadgeBundle/Tests/Controller/SnippetControllerTest.php b/src/PUGX/BadgeBundle/Tests/Controller/SnippetControllerTest.php index 937bf86a..43a96c7e 100644 --- a/src/PUGX/BadgeBundle/Tests/Controller/SnippetControllerTest.php +++ b/src/PUGX/BadgeBundle/Tests/Controller/SnippetControllerTest.php @@ -31,13 +31,12 @@ public function setUp() public function testAllAction() { - $expectedData = '{"clip_all":{"markdown":"[![Latest Stable Version](http:\/\/localhost\/pugx\/badge-poser\/v\/stable)](https:\/\/packagist.org\/packages\/pugx\/badge-poser) [![Total Downloads](http:\/\/localhost\/pugx\/badge-poser\/downloads)](https:\/\/packagist.org\/packages\/pugx\/badge-poser) [![Latest Unstable Version](http:\/\/localhost\/pugx\/badge-poser\/v\/unstable)](https:\/\/packagist.org\/packages\/pugx\/badge-poser) [![License](http:\/\/localhost\/pugx\/badge-poser\/license)](https:\/\/packagist.org\/packages\/pugx\/badge-poser)"},"latest_stable_version":{"markdown":"[![Latest Stable Version](http:\/\/localhost\/pugx\/badge-poser\/v\/stable)](https:\/\/packagist.org\/packages\/pugx\/badge-poser)","img":"http:\/\/localhost\/pugx\/badge-poser\/v\/stable"},"total":{"markdown":"[![Total Downloads](http:\/\/localhost\/pugx\/badge-poser\/downloads)](https:\/\/packagist.org\/packages\/pugx\/badge-poser)","img":"http:\/\/localhost\/pugx\/badge-poser\/downloads"},"latest_unstable_version":{"markdown":"[![Latest Unstable Version](http:\/\/localhost\/pugx\/badge-poser\/v\/unstable)](https:\/\/packagist.org\/packages\/pugx\/badge-poser)","img":"http:\/\/localhost\/pugx\/badge-poser\/v\/unstable"},"license":{"markdown":"[![License](http:\/\/localhost\/pugx\/badge-poser\/license)](https:\/\/packagist.org\/packages\/pugx\/badge-poser)","img":"http:\/\/localhost\/pugx\/badge-poser\/license"},"monthly":{"markdown":"[![Monthly Downloads](http:\/\/localhost\/pugx\/badge-poser\/d\/monthly.png)](https:\/\/packagist.org\/packages\/pugx\/badge-poser)","img":"http:\/\/localhost\/pugx\/badge-poser\/d\/monthly.png"},"daily":{"markdown":"[![Daily Downloads](http:\/\/localhost\/pugx\/badge-poser\/d\/daily.png)](https:\/\/packagist.org\/packages\/pugx\/badge-poser)","img":"http:\/\/localhost\/pugx\/badge-poser\/d\/daily.png"},"version":{"markdown":"[![Version](http:\/\/localhost\/pugx\/badge-poser\/version)](https:\/\/packagist.org\/packages\/pugx\/badge-poser)","img":"http:\/\/localhost\/pugx\/badge-poser\/version"},"repository":{"html":"pugx\/badge-poser"}}'; + $expectedData = '{"clip_all":{"markdown":"[![Latest Stable Version](http:\/\/localhost\/pugx\/badge-poser\/v\/stable)](https:\/\/packagist.org\/packages\/pugx\/badge-poser) [![Total Downloads](http:\/\/localhost\/pugx\/badge-poser\/downloads)](https:\/\/packagist.org\/packages\/pugx\/badge-poser) [![Latest Unstable Version](http:\/\/localhost\/pugx\/badge-poser\/v\/unstable)](https:\/\/packagist.org\/packages\/pugx\/badge-poser) [![License](http:\/\/localhost\/pugx\/badge-poser\/license)](https:\/\/packagist.org\/packages\/pugx\/badge-poser)"},"latest_stable_version":{"markdown":"[![Latest Stable Version](http:\/\/localhost\/pugx\/badge-poser\/v\/stable)](https:\/\/packagist.org\/packages\/pugx\/badge-poser)","img":"http:\/\/localhost\/pugx\/badge-poser\/v\/stable"},"total":{"markdown":"[![Total Downloads](http:\/\/localhost\/pugx\/badge-poser\/downloads)](https:\/\/packagist.org\/packages\/pugx\/badge-poser)","img":"http:\/\/localhost\/pugx\/badge-poser\/downloads"},"latest_unstable_version":{"markdown":"[![Latest Unstable Version](http:\/\/localhost\/pugx\/badge-poser\/v\/unstable)](https:\/\/packagist.org\/packages\/pugx\/badge-poser)","img":"http:\/\/localhost\/pugx\/badge-poser\/v\/unstable"},"license":{"markdown":"[![License](http:\/\/localhost\/pugx\/badge-poser\/license)](https:\/\/packagist.org\/packages\/pugx\/badge-poser)","img":"http:\/\/localhost\/pugx\/badge-poser\/license"},"monthly":{"markdown":"[![Monthly Downloads](http:\/\/localhost\/pugx\/badge-poser\/d\/monthly)](https:\/\/packagist.org\/packages\/pugx\/badge-poser)","img":"http:\/\/localhost\/pugx\/badge-poser\/d\/monthly"},"daily":{"markdown":"[![Daily Downloads](http:\/\/localhost\/pugx\/badge-poser\/d\/daily)](https:\/\/packagist.org\/packages\/pugx\/badge-poser)","img":"http:\/\/localhost\/pugx\/badge-poser\/d\/daily"},"version":{"markdown":"[![Version](http:\/\/localhost\/pugx\/badge-poser\/version)](https:\/\/packagist.org\/packages\/pugx\/badge-poser)","img":"http:\/\/localhost\/pugx\/badge-poser\/version"},"repository":{"html":"pugx\/badge-poser"}}'; $client = static::createClient(); static::$kernel->getContainer()->set('packagist_client', $this->packagistClient); $client->request('GET','/snippet/all/?repository=pugx/badge-poser'); $this->assertTrue($client->getResponse()->isSuccessful()); -// echo $client->getResponse()->getContent();die; $this->assertEquals($expectedData, $client->getResponse()->getContent()); } diff --git a/src/PUGX/BadgeBundle/Tests/RedirectsTest.php b/src/PUGX/BadgeBundle/Tests/RedirectsTest.php new file mode 100644 index 00000000..8de9c61c --- /dev/null +++ b/src/PUGX/BadgeBundle/Tests/RedirectsTest.php @@ -0,0 +1,40 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace PUGX\BadgeBundle\Tests; + +use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; + +class RedirectsTest extends WebTestCase +{ + /** + * @dataProvider getUrlToVerify + */ + public function testOldPageShouldRedirectToNewOnes($oldUrl, $newUrl) + { + $client = static::createClient(); + $client->request('GET', $oldUrl); + $this->assertTrue($client->getResponse()->isRedirect($newUrl)); + $this->assertEquals(301, $client->getResponse()->getStatusCode()); + } + + public function getUrlToVerify() + { + return array( + array('/pugx/badge-poser/downloads.png', 'http://localhost/pugx/badge-poser/downloads'), + array('/pugx/badge-poser/d/total.png', 'http://localhost/pugx/badge-poser/d/total'), + array('/pugx/badge-poser/version.png', 'http://localhost/pugx/badge-poser/version'), + array('/pugx/badge-poser/v/unstable.png', 'http://localhost/pugx/badge-poser/v/unstable'), + array('/pugx/badge-poser/license.png', 'http://localhost/pugx/badge-poser/license'), + array('/pugx/microsoft-lover/d/total.png', 'http://localhost/pugx/microsoft-lover/d/total'), + ); + } +}