Skip to content

Commit

Permalink
Start unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nicksagona committed Jul 2, 2015
1 parent 03c4316 commit a0ee766
Show file tree
Hide file tree
Showing 22 changed files with 254 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .gitignore
@@ -0,0 +1,4 @@
composer.lock
public
vendor

11 changes: 11 additions & 0 deletions .travis.yml
@@ -0,0 +1,11 @@
before_script:
- composer self-update
- composer install
language: php
php:
- 5.4
- 5.5
script:
- ./vendor/bin/phpunit
notifications:
email: false
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -2,6 +2,8 @@ Pop PDF
=======
Part of the Pop PHP Framework (http://github.com/popphp/popphp)

[![Build Status](https://travis-ci.org/popphp/pop-pdf.svg?branch=master)](https://travis-ci.org/popphp/pop-pdf)

OVERVIEW
--------
Pop PDF is a component of the Pop PHP Framework 2. It is a powerful and robust PDF processing
Expand Down
8 changes: 8 additions & 0 deletions composer.json
Expand Up @@ -24,6 +24,9 @@
"require": {
"php": ">=5.4.0"
},
"require-dev": {
"phpunit/phpunit": "4.6.*"
},
"suggest": {
"ext-gd": "For the image parser"
},
Expand All @@ -32,6 +35,11 @@
"Pop\\Pdf\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Pop\\Pdf\\Test\\": "tests/"
}
},
"extra": {
"branch-alias": {
"dev-master": "2.0.x-dev"
Expand Down
18 changes: 18 additions & 0 deletions phpunit.xml
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<phpunit bootstrap="vendor/autoload.php">
<testsuites>
<testsuite name="Pop PDF Component PHPUnit Test">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</whitelist>
</filter>
<logging>
<log type="coverage-html" target="/tmp/pop-pdf-cc" charset="UTF-8"
yui="true" highlight="false" showUncoveredFiles="true"
lowUpperBound="35" highLowerBound="70" />
</logging>
</phpunit>
2 changes: 1 addition & 1 deletion src/AbstractDocument.php
Expand Up @@ -411,7 +411,7 @@ abstract public function copyPage($p);
* @throws Exception
* @return AbstractDocument
*/
abstract public function orderPages($pages);
abstract public function orderPages(array $pages);

/**
* Delete a page from the PDF document
Expand Down
2 changes: 1 addition & 1 deletion src/Document.php
Expand Up @@ -127,7 +127,7 @@ public function copyPage($p)
* @throws Exception
* @return Document
*/
public function orderPages($pages)
public function orderPages(array $pages)
{
$newOrder = [];

Expand Down
Empty file removed tests/.empty
Empty file.
146 changes: 146 additions & 0 deletions tests/DocumentTest.php
@@ -0,0 +1,146 @@
<?php

namespace Pop\Pdf\Test;

use Pop\Pdf\Document;
use Pop\Pdf\Document\Page;
use Pop\Pdf\Document\Font;

class DocumentTest extends \PHPUnit_Framework_TestCase
{

public function testConstructor()
{
$doc = new Document(new Page(Page::LETTER));
$this->assertInstanceOf('Pop\Pdf\Document', $doc);
}

public function testAddPages()
{
$doc = new Document();
$doc->addPages([new Page(Page::LETTER), new Page(Page::LETTER)]);
$doc->createPage(Page::LEGAL);
$doc->copyPage(1);
$this->assertEquals(4, $doc->getNumberOfPages());
}

public function testCopyPageException()
{
$this->setExpectedException('Pop\Pdf\Exception');
$doc = new Document();
$doc->createPage(Page::LEGAL);
$doc->copyPage(3);
}

public function testOrderPages()
{
$doc = new Document();
$doc->addPages([new Page(Page::LETTER), new Page(Page::LETTER)]);
$doc->createPage(Page::LEGAL);
$doc->copyPage(1);
$doc->orderPages([4, 3, 2, 1]);
$this->assertEquals(4, $doc->getNumberOfPages());
}

public function testOrderPagesBadCountException()
{
$this->setExpectedException('Pop\Pdf\Exception');
$doc = new Document();
$doc->addPages([new Page(Page::LETTER), new Page(Page::LETTER)]);
$doc->createPage(Page::LEGAL);
$doc->copyPage(1);
$doc->orderPages([4, 2, 1]);
}

public function testOrderPagesPageNotExistException()
{
$this->setExpectedException('Pop\Pdf\Exception');
$doc = new Document();
$doc->addPages([new Page(Page::LETTER), new Page(Page::LETTER)]);
$doc->createPage(Page::LEGAL);
$doc->copyPage(1);
$doc->orderPages([4, 8, 3, 1]);
}

public function testDeletePage()
{
$doc = new Document();
$doc->addPages([new Page(Page::LETTER), new Page(Page::LETTER)]);
$doc->createPage(Page::LEGAL);
$this->assertEquals(3, $doc->getNumberOfPages());
$doc->deletePage(3);
}

public function testDeletePageException()
{
$this->setExpectedException('Pop\Pdf\Exception');
$doc = new Document();
$doc->addPages([new Page(Page::LETTER), new Page(Page::LETTER)]);
$doc->createPage(Page::LEGAL);
$this->assertEquals(3, $doc->getNumberOfPages());
$doc->deletePage(8);
}

public function testAddFont()
{
$doc = new Document();
$doc->addFont(new Font('Arial'));
$this->assertEquals(1, $doc->getNumberOfFonts());
}

public function testAddFontException()
{
$this->setExpectedException('Pop\Pdf\Exception');
$doc = new Document();
$doc->addFont(new Font(__DIR__ . '/tmp/fonts/times.ttf'));
}

public function testEmbedFont()
{
$doc = new Document();
$doc->embedFont(new Font(__DIR__ . '/tmp/fonts/times.ttf'));
$this->assertEquals(1, $doc->getNumberOfFonts());
$this->assertFalse($doc->hasImportedFonts());
$this->assertEquals(0, count($doc->getImportedFonts()));
}

public function testEmbedFontException()
{
$this->setExpectedException('Pop\Pdf\Exception');
$doc = new Document();
$doc->embedFont(new Font('Arial'));
}

public function testSetCurrentPage()
{
$doc = new Document();
$doc->addPages([new Page(Page::LETTER), new Page(Page::LETTER)]);
$doc->setCurrentPage(1);
$this->assertEquals(2, $doc->getNumberOfPages());
$this->assertEquals(1, $doc->getCurrentPage());
}

public function testSetCurrentPageException()
{
$this->setExpectedException('Pop\Pdf\Exception');
$doc = new Document();
$doc->addPages([new Page(Page::LETTER), new Page(Page::LETTER)]);
$doc->setCurrentPage(8);
}

public function testSetCurrentFont()
{
$doc = new Document();
$doc->addFont(new Font('Arial'));
$doc->setCurrentFont('Arial');
$this->assertEquals('Arial', $doc->getCurrentFont());
}

public function testSetCurrentFontException()
{
$this->setExpectedException('Pop\Pdf\Exception');
$doc = new Document();
$doc->setCurrentFont('Arial');
}

}
63 changes: 63 additions & 0 deletions tests/PdfTest.php
@@ -0,0 +1,63 @@
<?php

namespace Pop\Pdf\Test;

use Pop\Pdf;

class PdfTest extends \PHPUnit_Framework_TestCase
{

public function testConstructor()
{
$pdf = new Pdf\Pdf();
$this->assertInstanceOf('Pop\Pdf\Pdf', $pdf);
}

public function testImportFromFile()
{
$pdf = new Pdf\Pdf();
$this->assertInstanceOf('Pop\Pdf\Document', $pdf->importFromFile(__DIR__ . '/tmp/doc.pdf', 1));
}

public function testWriteToFile()
{
$pdf = new Pdf\Pdf();
$doc = $pdf->importFromFile(__DIR__ . '/tmp/doc.pdf', 1);
$pdf->writeToFile($doc, __DIR__ . '/tmp/mytest.pdf');
$this->assertFileExists(__DIR__ . '/tmp/mytest.pdf');
unlink(__DIR__ . '/tmp/mytest.pdf');
}

/**
* @runInSeparateProcess
*/
public function testOutputToHttp()
{
$_SERVER['SERVER_PORT'] = 443;

$pdf = new Pdf\Pdf();
$doc = $pdf->importFromFile(__DIR__ . '/tmp/doc.pdf', 1);

ob_start();
$pdf->outputToHttp($doc);
$result = ob_get_clean();

$this->assertContains('%PDF', $result);
}

/**
* @runInSeparateProcess
*/
public function testToString()
{
$pdf = new Pdf\Pdf();
$doc = $pdf->importFromFile(__DIR__ . '/tmp/doc.pdf', 1);

ob_start();
echo $doc;
$result = ob_get_clean();

$this->assertContains('%PDF', $result);
}

}
Binary file added tests/tmp/doc.pdf
Binary file not shown.
Binary file added tests/tmp/fonts/carltonn.ttf
Binary file not shown.
Binary file added tests/tmp/fonts/times.ttf
Binary file not shown.
Binary file added tests/tmp/images/logo-cmyk.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/tmp/images/logo-gray.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/tmp/images/logo-index-trans.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/tmp/images/logo-index.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/tmp/images/logo-rgb.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/tmp/images/logo-rgb.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/tmp/images/logo-trans.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/tmp/images/logo.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/tmp/test.pdf
Binary file not shown.

0 comments on commit a0ee766

Please sign in to comment.