* @date 2017-01-03 * @license LGPLv3 * @url * * PdfParser is a pdf library written in PHP, extraction oriented. * Copyright (C) 2017 - Sébastien MALOT * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. * If not, see . * */ namespace Smalot\PdfParser\Tests\Units; use mageekguy\atoum; /** * Class Header * * @package Smalot\PdfParser\Tests\Units */ class Header extends atoum\test { public function testParse() { $document = new \Smalot\PdfParser\Document(); $content = '<>foo'; $position = 0; $header = \Smalot\PdfParser\Header::parse($content, $document, $position); $this->assert->object($header)->isInstanceOf('\Smalot\PdfParser\Header'); $this->assert->integer($position)->isEqualTo(27); $this->assert->array($header->getElements())->hasSize(2); // No header to parse $this->assert->castToString($header->get('Type'))->isEqualTo('Page'); $content = 'foo'; $position = 0; $header = \Smalot\PdfParser\Header::parse($content, $document, $position); $this->assert->object($header)->isInstanceOf('\Smalot\PdfParser\Header'); $this->assert->integer($position)->isEqualTo(0); $this->assert->array($header->getElements())->hasSize(0); $position = 0; $content = "<>"; $header = \Smalot\PdfParser\Header::parse($content, $document, $position); $this->assert->integer($position)->isEqualTo(212); $position = 0; $content = '[5 0 R ] foo'; $header = \Smalot\PdfParser\Header::parse($content, $document, $position); $this->assert->integer($position)->isEqualTo(8); $this->assert->array($header->getElements())->hasSize(1); } public function testGetElements() { $document = new \Smalot\PdfParser\Document(); $content = '<>foo'; $position = 0; $header = \Smalot\PdfParser\Header::parse($content, $document, $position); $this->assert->array($elements = $header->getElements())->hasSize(2); $this->assert->object(current($elements))->isInstanceOf('\Smalot\PdfParser\Element\ElementName'); $types = $header->getElementTypes(); $this->assert->array($types); $this->assert->string($types['Type'])->isEqualTo('Smalot\PdfParser\Element\ElementName'); $this->assert->string($types['Subtype'])->isEqualTo('Smalot\PdfParser\Element\ElementName'); } public function testHas() { $document = new \Smalot\PdfParser\Document(); $content = '<>foo'; $position = 0; $header = \Smalot\PdfParser\Header::parse($content, $document, $position); $this->assert->boolean($header->has('Type'))->isEqualTo(true); $this->assert->boolean($header->has('SubType'))->isEqualTo(true); $this->assert->boolean($header->has('Font'))->isEqualTo(true); $this->assert->boolean($header->has('Text'))->isEqualTo(false); } public function testGet() { $document = new \Smalot\PdfParser\Document(); $content = '<>foo'; $position = 0; $header = \Smalot\PdfParser\Header::parse($content, $document, $position); $object = new \Smalot\PdfParser\Page($document, $header); $document->setObjects(array('5_0' => $object)); $this->assert->object($header->get('Type'))->isInstanceOf('\Smalot\PdfParser\Element\ElementName'); $this->assert->object($header->get('SubType'))->isInstanceOf('\Smalot\PdfParser\Element\ElementName'); $this->assert->object($header->get('Font'))->isInstanceOf('\Smalot\PdfParser\Page'); $this->assert->object($header->get('Image'))->isInstanceOf('\Smalot\PdfParser\Element\ElementMissing'); $resources = $header->get('Resources'); $this->assert->variable($resources)->isNull(); } public function testResolveXRef() { $document = new \Smalot\PdfParser\Document(); $content = '<>foo'; $position = 0; $header = \Smalot\PdfParser\Header::parse($content, $document, $position); $object = new \Smalot\PdfParser\Page($document, $header); $document->setObjects(array('5_0' => $object)); $this->assert->object($header->get('Font'))->isInstanceOf('\Smalot\PdfParser\Object'); $header=$header->get('Resources'); try { $this->assert->variable($header)->isInstanceOf('\Smalot\PdfParser\Element\ElementMissing'); $this->assert->boolean(true)->isEqualTo(false); } catch (\Exception $e) { $this->assert->variable($header)->isNull(); } } }