Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Herzult committed Jul 11, 2011
1 parent 48953a0 commit a6992b8
Show file tree
Hide file tree
Showing 13 changed files with 334 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[submodule "Resources/vendor/symfony"]
path = Resources/vendor/symfony
url = https://github.com/symfony/symfony.git
[submodule "Resources/vendor/snappy"]
path = Resources/vendor/snappy
url = https://github.com/knplabs/snappy.git
1 change: 1 addition & 0 deletions Resources/vendor/snappy
Submodule snappy added at 196959
1 change: 1 addition & 0 deletions Resources/vendor/symfony
Submodule symfony added at f0f83a
90 changes: 90 additions & 0 deletions Tests/FunctionalTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace Knp\Bundle\SnappyBundle\Tests;

use Symfony\Component\HttpKernel\Util\Filesystem;

class FunctionalTest extends \PHPUnit_Framework_TestCase
{
private $kernel;
private $filesystem;

public function setUp()
{
$this->kernel = new TestKernel(uniqid(), false);

$this->filesystem = new Filesystem();
$this->filesystem->mkdir($this->kernel->getCacheDir());
}

public function tearDown()
{
$this->filesystem->remove($this->kernel->getCacheDir());
}

public function testServicesAreBothAvailableOutOfTheBox()
{
$this->kernel->setConfigurationFilename(__DIR__.'/fixtures/config/out_of_the_box.yml');
$this->kernel->boot();

$container = $this->kernel->getContainer();

$this->assertTrue($container->has('knp_snappy.pdf'), 'The pdf service is available.');

$pdf = $container->get('knp_snappy.pdf');

$this->assertInstanceof('Knp\Bundle\SnappyBundle\Snappy\LoggableGenerator', $pdf);
$this->assertInstanceof('Knp\Snappy\Pdf', $pdf->getInternalGenerator());
$this->assertEquals('wkhtmltopdf', $pdf->getInternalGenerator()->getBinary());

$this->assertTrue($container->has('knp_snappy.image'), 'The image service is available.');

$image = $container->get('knp_snappy.image');

$this->assertInstanceof('Knp\Bundle\SnappyBundle\Snappy\LoggableGenerator', $image);
$this->assertInstanceof('Knp\Snappy\Image', $image->getInternalGenerator());
$this->assertEquals('wkhtmltoimage', $image->getInternalGenerator()->getBinary());
}

public function testChangeBinaries()
{
$this->kernel->setConfigurationFilename(__DIR__.'/fixtures/config/change_binaries.yml');
$this->kernel->boot();

$container = $this->kernel->getContainer();

$this->assertTrue($container->has('knp_snappy.pdf'));

$pdf = $container->get('knp_snappy.pdf');

$this->assertEquals('/custom/binary/for/wkhtmltopdf', $pdf->getInternalGenerator()->getBinary());

$this->assertTrue($container->has('knp_snappy.image'));

$image = $container->get('knp_snappy.image');

$this->assertEquals('/custom/binary/for/wkhtmltoimage', $image->getInternalGenerator()->getBinary());
}

public function testDisablePdf()
{
$this->kernel->setConfigurationFilename(__DIR__.'/fixtures/config/disable_pdf.yml');
$this->kernel->boot();

$container = $this->kernel->getContainer();

$this->assertFalse($container->has('knp_snappy.pdf'), 'The pdf service is NOT available.');
$this->assertTrue($container->has('knp_snappy.image'), 'The image service is available.');
}

public function testDisableImage()
{
$this->kernel->setConfigurationFilename(__DIR__.'/fixtures/config/disable_image.yml');
$this->kernel->boot();

$container = $this->kernel->getContainer();

$this->assertTrue($container->has('knp_snappy.pdf'), 'The pdf service is available.');
$this->assertFalse($container->has('knp_snappy.image'), 'The image service is NOT available.');
}
}
104 changes: 104 additions & 0 deletions Tests/Snappy/LoggableGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

namespace Knp\Bundle\SnappyBundle\Tests\Snappy;

use Knp\Bundle\SnappyBundle\Snappy\LoggableGenerator;

class LoggableGeneratorTests extends \PHPUnit_Framework_TestCase
{
public function testGenerate()
{
$internal = $this->getMock('Knp\Snappy\GeneratorInterface');
$internal
->expects($this->once())
->method('generate')
->with(
$this->equalTo('the_input_file'),
$this->equalTo('the_output_file'),
$this->equalTo(array('foo' => 'bar')),
$this->equalTo(true)
)
;

$logger = $this->getMock('Symfony\Component\HttpKernel\Log\LoggerInterface');
$logger
->expects($this->once())
->method('debug')
->with($this->equalTo('Generate from file (the_input_file) to file (the_output_file).'))
;

$generator = new LoggableGenerator($internal, $logger);
$generator->generate('the_input_file', 'the_output_file', array('foo' => 'bar'), true);
}

public function testGenerateFromHtml()
{
$internal = $this->getMock('Knp\Snappy\GeneratorInterface');
$internal
->expects($this->once())
->method('generateFromHtml')
->with(
$this->equalTo('<html>foo</html>'),
$this->equalTo('the_output_file'),
$this->equalTo(array('foo' => 'bar')),
$this->equalTo(true)
)
;

$logger = $this->getMock('Symfony\Component\HttpKernel\Log\LoggerInterface');
$logger
->expects($this->once())
->method('debug')
->with($this->equalTo('Generate from HTML (<html>foo</html>) to file (the_output_file).'))
;

$generator = new LoggableGenerator($internal, $logger);
$generator->generateFromHtml('<html>foo</html>', 'the_output_file', array('foo' => 'bar'), true);
}

public function testOutput()
{
$internal = $this->getMock('Knp\Snappy\GeneratorInterface');
$internal
->expects($this->once())
->method('getOutput')
->with(
$this->equalTo('the_input_file'),
$this->equalTo(array('foo' => 'bar'))
)
;

$logger = $this->getMock('Symfony\Component\HttpKernel\Log\LoggerInterface');
$logger
->expects($this->once())
->method('debug')
->with($this->equalTo('Output from file (the_input_file).'))
;

$generator = new LoggableGenerator($internal, $logger);
$generator->getOutput('the_input_file', array('foo' => 'bar'), true);
}

public function testOutputFromHtml()
{
$internal = $this->getMock('Knp\Snappy\GeneratorInterface');
$internal
->expects($this->once())
->method('getOutputFromHtml')
->with(
$this->equalTo('<html>foo</html>'),
$this->equalTo(array('foo' => 'bar'))
)
;

$logger = $this->getMock('Symfony\Component\HttpKernel\Log\LoggerInterface');
$logger
->expects($this->once())
->method('debug')
->with($this->equalTo('Output from HTML (<html>foo</html>).'))
;

$generator = new LoggableGenerator($internal, $logger);
$generator->getOutputFromHtml('<html>foo</html>', array('foo' => 'bar'), true);
}
}
40 changes: 40 additions & 0 deletions Tests/TestKernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Knp\Bundle\SnappyBundle\Tests;

use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Config\Loader\LoaderInterface;

class TestKernel extends Kernel
{
private $configurationFilename;

/**
* Defines the configuration filename
*
* @param string $filename
*/
public function setConfigurationFilename($filename)
{
$this->configurationFilename = $filename;
}

/**
* {@inheritDoc}
*/
public function registerBundles()
{
return array(
new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
new \Knp\Bundle\SnappyBundle\KnpSnappyBundle()
);
}

/**
* {@inheritDoc}
*/
public function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->load($this->configurationFilename);
}
}
19 changes: 19 additions & 0 deletions Tests/bootstrap.php.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

require_once $_SERVER['SYMFONY'].'/Symfony/Component/ClassLoader/UniversalClassLoader.php';

use Symfony\Component\ClassLoader\UniversalClassLoader;

$loader = new UniversalClassLoader();
$loader->registerNamespaces(array(
'Symfony' => $_SERVER['SYMFONY'],
'Knp\\Snappy' => $_SERVER['SNAPPY'],
));
$loader->register();

spl_autoload_register(function($class) {
if (0 === strpos($class, 'Knp\\Bundle\\SnappyBundle\\')) {
require_once __DIR__.'/../'.implode('/', array_slice(explode('\\', $class), 3)).'.php';
return true;
}
});
13 changes: 13 additions & 0 deletions Tests/fixtures/config/base.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
framework:
secret: ThisIsNotReallyASecretSoPleaseChangeIt
charset: UTF-8
router: { resource: "%kernel.root_dir%/config/routing.yml" }
form: true
csrf_protection: true
validation: { enabled: true, enable_annotations: true }
templating: { engines: ['php'] } #assets_version: SomeVersionScheme
session:
default_locale: en
lifetime: 3600
auto_start: true
translator: { fallback: en }
8 changes: 8 additions & 0 deletions Tests/fixtures/config/change_binaries.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
imports:
- { resource: 'base.yml' }

knp_snappy:
pdf:
binary: /custom/binary/for/wkhtmltopdf
image:
binary: /custom/binary/for/wkhtmltoimage
6 changes: 6 additions & 0 deletions Tests/fixtures/config/disable_image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
imports:
- { resource: 'base.yml' }

knp_snappy:
image:
enabled: false
6 changes: 6 additions & 0 deletions Tests/fixtures/config/disable_pdf.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
imports:
- { resource: 'base.yml' }

knp_snappy:
pdf:
enabled: false
2 changes: 2 additions & 0 deletions Tests/fixtures/config/out_of_the_box.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
imports:
- { resource: 'base.yml' }
38 changes: 38 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>

<!-- http://www.phpunit.de/manual/current/en/appendixes.configuration.html -->

<phpunit
backupGlobals = "false"
backupStaticAttributes = "false"
colors = "true"
convertErrorsToExceptions = "true"
convertNoticesToExceptions = "true"
convertWarningsToExceptions = "true"
processIsolation = "false"
stopOnFailure = "false"
syntaxCheck = "false"
bootstrap = "./Tests/bootstrap.php.dist" >

<testsuites>
<testsuite name="KnpSnappyBundle Test Suite">
<directory>./Tests</directory>
</testsuite>
</testsuites>

<php>
<server name="SYMFONY" value="./Resources/vendor/symfony/src" />
<server name="SNAPPY" value="./Resources/vendor/snappy/src" />
</php>


<filter>
<whitelist>
<directory>.</directory>
<exclude>
<directory>./Tests</directory>
</exclude>
</whitelist>
</filter>

</phpunit>

0 comments on commit a6992b8

Please sign in to comment.