Skip to content

Commit

Permalink
Merge pull request #84 from liuggio/master
Browse files Browse the repository at this point in the history
decoupled tests
  • Loading branch information
liuggio committed Nov 7, 2013
2 parents cc8ff2f + 69f4203 commit ff27dfb
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 13 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pugx/badge-poser",
"description": "add badges on your readme, such as downloads number or latest version.",
"description": "Poser, add badges on your readme, such as downloads number or latest version.",
"keywords": ["composer", "packagist", "badge", "pin"],
"homepage": "https://github.com/PUGX",
"type": "symfony-project",
Expand Down
10 changes: 2 additions & 8 deletions src/PUGX/BadgeBundle/Tests/Controller/BadgeControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
namespace PUGX\BadgeBundle\Tests\Controller;

use Packagist\Api\Client;
use PUGX\StatsBundle\Test\StatsFunctionalTest;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class BadgeControllerTest extends StatsFunctionalTest
class BadgeControllerTest extends WebTestCase
{
protected $packagistClient;

Expand Down Expand Up @@ -44,8 +44,6 @@ public function testDownloadsAction()
static::$kernel->getContainer()->set('packagist_client', $this->packagistClient);
$crawler = $client->request('GET', '/pugx/badge-poser/d/total.png');
$this->assertTrue($client->getResponse()->isSuccessful());

$this->checkStatsCalls($client, 'pugx/badge-poser', 'downloadsAction');
}

public function testLatestStableAction()
Expand All @@ -55,8 +53,6 @@ public function testLatestStableAction()
$crawler = $client->request('GET', '/pugx/badge-poser/version.png');

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

$this->checkStatsCalls($client, 'pugx/badge-poser', 'versionAction');
}

public function testLatestUnstableAction()
Expand All @@ -68,8 +64,6 @@ public function testLatestUnstableAction()
$this->assertTrue($client->getResponse()->isSuccessful());
$response = $client->getResponse();
$this->assertRegExp('/s-maxage=3600/', $response->headers->get('Cache-Control'));

$this->checkStatsCalls($client, 'pugx/badge-poser', 'versionAction');
}

public function testIfPackageDoesntExist()
Expand Down
6 changes: 2 additions & 4 deletions src/PUGX/BadgeBundle/Tests/Controller/PageControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@

namespace PUGX\BadgeBundle\Tests\Controller;

use PUGX\StatsBundle\Test\StatsFunctionalTest;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class PageControllerTest extends StatsFunctionalTest
class PageControllerTest extends WebTestCase
{
/**
* @dataProvider provider
Expand All @@ -32,8 +32,6 @@ public function testHomeAction($path)
$this->assertEquals(1, $crawler->filter('#container h4:contains("Monthly downloads")')->count());
$this->assertEquals(1, $crawler->filter('#container h4:contains("Latest Stable Version")')->count());
$this->assertEquals(1, $crawler->filter('#container h4:contains("Latest Unstable Version")')->count());

$this->checkStatsAreNotIncremented($client);
}

public function provider()
Expand Down
90 changes: 90 additions & 0 deletions src/PUGX/StatsBundle/Tests/FunctionalControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

/*
* This file is part of the badge-poser package.
*
* (c) PUGX <http://pugx.github.io/>
*
* 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 PUGX\StatsBundle\Test\StatsFunctionalTest;
use Packagist\Api\Client;

class FunctionalControllerTest extends StatsFunctionalTest
{
protected $packagistClient;

// this setUp fake the request/response, if you comment this function the test'd run only with internet connection
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}}';
$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);

return new Client($clientHttp);
}

public function testOnDownloadsActionStatisticShouldBeCreated()
{
$client = static::createClient();
static::$kernel->getContainer()->set('packagist_client', $this->packagistClient);
$client->request('GET', '/pugx/badge-poser/d/total.png');

$this->checkStatsCalls($client, 'pugx/badge-poser', 'downloadsAction');
}

public function testOnLatestStableActionStatisticShouldBeCreated()
{
$client = static::createClient();
static::$kernel->getContainer()->set('packagist_client', $this->packagistClient);
$client->request('GET', '/pugx/badge-poser/version.png');
$this->checkStatsCalls($client, 'pugx/badge-poser', 'versionAction');
}

public function testOnLatestUnstableActionStatisticShouldBeCreated()
{
$client = static::createClient();
static::$kernel->getContainer()->set('packagist_client', $this->packagistClient);
$client->request('GET', '/pugx/badge-poser/v/unstable.png');

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

$this->checkStatsCalls($client, 'pugx/badge-poser', 'versionAction');
}

/**
* @dataProvider provider
*/
public function testOnHomeActionStatisticShouldNotBeCreated($path)
{
$client = static::createClient();
$client->enableProfiler();

$client->request('GET', $path);
$this->assertTrue($client->getResponse()->isSuccessful());

$this->checkStatsAreNotIncremented($client);
}

public function provider()
{
return array(
array('/'),
array('/show/pugx/badge-poser')
);
}
}

0 comments on commit ff27dfb

Please sign in to comment.