Skip to content

Commit

Permalink
[TESTS] Improve coverage (rjanot#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
rjanot committed Oct 13, 2013
1 parent 05522e7 commit 4de742b
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 5 deletions.
2 changes: 1 addition & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public function getConfigTreeBuilder()
->example('[]')
->end()
->arrayNode('blacklist_urls')
->defaultValue($this->defaultIgnoredExtensions)
->defaultValue(array())
->prototype('scalar')->end()
->info('whitelisted urls. Not routing keys, but urls')
->example('[.css,.js]')
Expand Down
115 changes: 115 additions & 0 deletions Tests/DependencyInjection/YuccaPrerenderExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php
/**
* Created by PhpStorm.
* User: rjanot
* Date: 13/10/13
* Time: 12:48
*/

namespace Yucca\PrerenderBundle\Tests\DependencyInjection;


use Symfony\Component\DependencyInjection\ContainerBuilder;
use Yucca\PrerenderBundle\DependencyInjection\Configuration;
use Yucca\PrerenderBundle\DependencyInjection\YuccaPrerenderExtension;

class YuccaPrerenderExtensionTest extends \PHPUnit_Framework_TestCase{
protected $containerBuilder;

/**
* @param $class
* @param $propertyName
* @return mixed
*/
static public function getReflectedPropertyValue($class, $propertyName)
{
$reflectedClass = new \ReflectionClass($class);
$property = $reflectedClass->getProperty($propertyName);
$property->setAccessible(true);

return $property->getValue($class);
}

/**
* @param mixed $value
* @param string $key
*/
private function assertParameter($value, $key)
{
$this->assertEquals($value, $this->containerBuilder->getParameter($key), sprintf('%s parameter is correct', $key));
}

/**
* @return mixed
*/
public function testDefaultLoad() {
$config = new Configuration();
$defaultIgnoredExtensions = self::getReflectedPropertyValue($config, 'defaultIgnoredExtensions');
$defaultCrawlerUserAgents = self::getReflectedPropertyValue($config, 'defaultCrawlerUserAgents');

$this->containerBuilder = new ContainerBuilder();
$extension = new YuccaPrerenderExtension();
$extension->load(array(), $this->containerBuilder);

$this->assertParameter('http://prerender.herokuapp.com', 'yucca_prerender.backend_url');
$this->assertParameter($defaultCrawlerUserAgents, 'yucca_prerender.crawler_user_agents');
$this->assertParameter($defaultIgnoredExtensions, 'yucca_prerender.ignored_extensions');
$this->assertParameter(array(), 'yucca_prerender.whitelist_urls');
$this->assertParameter(array(), 'yucca_prerender.blacklist_urls');
}

/**
* @return mixed
*/
public function testBackendUrl() {
$this->containerBuilder = new ContainerBuilder();
$extension = new YuccaPrerenderExtension();
$extension->load(array(array('backend_url'=>'http://localhost:3000')), $this->containerBuilder);

$this->assertParameter('http://localhost:3000', 'yucca_prerender.backend_url');
}

/**
* @return mixed
*/
public function testCrawler() {
$this->containerBuilder = new ContainerBuilder();
$extension = new YuccaPrerenderExtension();
$extension->load(array(array('crawler_user_agents'=>array('My new bot'))), $this->containerBuilder);

$this->assertParameter(array('My new bot'), 'yucca_prerender.crawler_user_agents');
}

/**
* @return mixed
*/
public function testIgnoredExtensions() {
$this->containerBuilder = new ContainerBuilder();
$extension = new YuccaPrerenderExtension();
$extension->load(array(array('ignored_extensions'=>array('.io'))), $this->containerBuilder);

$this->assertParameter(array('.io'), 'yucca_prerender.ignored_extensions');
}

/**
* @return mixed
*/
public function testWhitelist() {
$this->containerBuilder = new ContainerBuilder();
$extension = new YuccaPrerenderExtension();
$extension->load(array(array('whitelist_urls'=>array('/users'))), $this->containerBuilder);

$this->assertParameter(array('/users'), 'yucca_prerender.whitelist_urls');
}

/**
* @return mixed
*/
public function testBlacklist() {
$this->containerBuilder = new ContainerBuilder();
$extension = new YuccaPrerenderExtension();
$extension->load(array(array('blacklist_urls'=>array('/users'))), $this->containerBuilder);

$this->assertParameter(array('/users'), 'yucca_prerender.blacklist_urls');
}
}
28 changes: 26 additions & 2 deletions Tests/Listener/KernelListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Yucca\PrerenderBundle\Tests\Listener;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Yucca\PrerenderBundle\Listener\KernelListener;

class KernelListenerTest extends \PHPUnit_Framework_TestCase
Expand Down Expand Up @@ -209,13 +211,24 @@ public function shouldRenderProvider()
'blacklist' => array('/users/michael'),
'should_prerender' => true
),
//Test _escaped_fragment_
array(
'user_agent' => '',
'host' => 'www.example.com',
'uri' => '?_escaped_fragment_=heyaImABot',
'referer' => 'http://google.com',
'ignored_extensions' => array(),
'whitelist' => array(),
'blacklist' => array(),
'should_prerender' => true
),
);
}

/**
* @dataProvider shouldRenderProvider
*/
public function testShouldRender(
public function testOnKernelRequest(
$userAgent,
$request_host,
$request_uri,
Expand All @@ -225,12 +238,16 @@ public function testShouldRender(
$blacklist,
$result
) {

$request = new Request();
$request->headers->set('HOST', $request_host);
$request->headers->set('User-Agent', $userAgent);
$request->headers->set('Referer', $referer);
$request->server->set('REQUEST_URI', $request_uri);

parse_str(parse_url($request_host.'/'.$request_uri, PHP_URL_QUERY), $query);
$request->query->replace($query);

$httpClient = $this->getMock('Yucca\PrerenderBundle\HttpClient\ClientInterface');

$listener = new KernelListener(
Expand All @@ -242,6 +259,13 @@ public function testShouldRender(
$httpClient
);

$this->assertEquals($result, $listener->shouldPrerenderPage($request));

$event = new GetResponseEvent(
$this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface'),
$request,
HttpKernelInterface::MASTER_REQUEST
);
$listener->onKernelRequest($event);
$this->assertEquals($result, $event->hasResponse());
}
}
20 changes: 20 additions & 0 deletions Tests/YuccaPrerenderBundleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
/**
* Created by PhpStorm.
* User: rjanot
* Date: 13/10/13
* Time: 12:16
*/

namespace Yucca\PrerenderBundle\Tests;


use Yucca\PrerenderBundle\YuccaPrerenderBundle;

class YuccaPrerenderBundleTest extends \PHPUnit_Framework_TestCase{
public function testGetName()
{
$bundle = new YuccaPrerenderBundle();
$this->assertEquals('YuccaPrerenderBundle', $bundle->getName());
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
],
"require": {
"php": ">=5.3.2",
"symfony/http-foundation": "2.3.*@dev"
"symfony/framework-bundle": "2.3.*@dev"
},
"require-dev": {
"phpunit/phpunit": "~3.7",
Expand Down
4 changes: 3 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
<whitelist addUncoveredFilesFromWhitelist="true">
<directory suffix=".php">.</directory>
<exclude>
<directory suffix=".php">vendor</directory>
<directory>Resources</directory>
<directory>Tests</directory>
<directory>vendor</directory>
</exclude>
</whitelist>
</filter>
Expand Down

0 comments on commit 4de742b

Please sign in to comment.