first commit

This commit is contained in:
Dave Umrysh
2021-02-26 12:53:56 -07:00
commit dc8b19aade
2373 changed files with 374265 additions and 0 deletions

View File

@@ -0,0 +1,61 @@
<?php
namespace Common\Tests\Adapter\Zip;
use PhpOffice\Common\Tests\TestHelperZip;
abstract class AbstractZipAdapterTest extends \PHPUnit\Framework\TestCase
{
protected $zipTest;
/**
* Returns a new instance of the adapter to test
* @return \PhpOffice\Common\Adapter\Zip\ZipInterface
*/
abstract protected function createAdapter();
public function setUp()
{
parent::setUp();
$pathResources = PHPOFFICE_COMMON_TESTS_BASE_DIR.DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR.'files'.DIRECTORY_SEPARATOR;
$this->zipTest = tempnam(sys_get_temp_dir(), 'PhpOfficeCommon');
copy($pathResources.'Sample_01_Simple.pptx', $this->zipTest);
}
public function tearDown()
{
parent::tearDown();
if (is_file($this->zipTest)) {
unlink($this->zipTest);
}
}
public function testOpen()
{
$adapter = $this->createAdapter();
$this->assertSame($adapter, $adapter->open($this->zipTest));
}
public function testClose()
{
$adapter = $this->createAdapter();
$adapter->open($this->zipTest);
$this->assertSame($adapter, $adapter->close());
}
public function testAddFromString()
{
$expectedPath = 'file.test';
$expectedContent = 'Content';
$adapter = $this->createAdapter();
$adapter->open($this->zipTest);
$this->assertSame($adapter, $adapter->addFromString($expectedPath, $expectedContent));
$adapter->close();
$this->assertTrue(TestHelperZip::assertFileExists($this->zipTest, $expectedPath));
$this->assertTrue(TestHelperZip::assertFileContent($this->zipTest, $expectedPath, $expectedContent));
}
}

View File

@@ -0,0 +1,14 @@
<?php
namespace Common\Tests\Adapter\Zip;
use PhpOffice\Common\Adapter\Zip\PclZipAdapter;
use PhpOffice\Common\Tests\TestHelperZip;
class PclZipAdapterTest extends AbstractZipAdapterTest
{
protected function createAdapter()
{
return new PclZipAdapter();
}
}

View File

@@ -0,0 +1,14 @@
<?php
namespace Common\Tests\Adapter\Zip;
use PhpOffice\Common\Adapter\Zip\ZipArchiveAdapter;
use PhpOffice\Common\Tests\TestHelperZip;
class ZipArchiveAdapterTest extends AbstractZipAdapterTest
{
protected function createAdapter()
{
return new ZipArchiveAdapter();
}
}

View File

@@ -0,0 +1,53 @@
<?php
/**
* This file is part of PHPOffice Common
*
* PHPOffice Common is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/Common/contributors.
*
* @link https://github.com/PHPOffice/Common
* @copyright 2009-2016 PHPOffice Common contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\Common\Tests;
use PhpOffice\Common\Autoloader;
/**
* Test class for Autoloader
*/
class AutoloaderTest extends \PHPUnit\Framework\TestCase
{
/**
* Register
*/
public function testRegister()
{
Autoloader::register();
$this->assertContains(
array('PhpOffice\\Common\\Autoloader', 'autoload'),
spl_autoload_functions()
);
}
/**
* Autoload
*/
public function testAutoload()
{
$declared = get_declared_classes();
$declaredCount = count($declared);
Autoloader::autoload('Foo');
$this->assertEquals(
$declaredCount,
count(get_declared_classes()),
'PhpOffice\\Common\\Autoloader::autoload() is trying to load ' .
'classes outside of the PhpOffice\\Common namespace'
);
}
}

View File

@@ -0,0 +1,125 @@
<?php
/**
* This file is part of PHPOffice Common
*
* PHPOffice Common is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/Common/contributors.
*
* @link https://github.com/PHPOffice/Common
* @copyright 2009-2016 PHPOffice Common contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\Common\Tests;
use PhpOffice\Common\Drawing;
/**
* Test class for IOFactory
*
* @coversDefaultClass PhpOffice\Common\IOFactory
*/
class DrawingTest extends \PHPUnit\Framework\TestCase
{
/**
*/
public function testDegreesAngle()
{
$value = rand(1, 100);
$this->assertEquals(0, Drawing::degreesToAngle());
$this->assertEquals((int) round($value * 60000), Drawing::degreesToAngle($value));
$this->assertEquals(0, Drawing::angleToDegrees());
$this->assertEquals(round($value / 60000), Drawing::angleToDegrees($value));
}
/**
*/
public function testPixelsCentimeters()
{
$value = rand(1, 100);
$this->assertEquals(0, Drawing::pixelsToCentimeters());
$this->assertEquals($value / Drawing::DPI_96 * 2.54, Drawing::pixelsToCentimeters($value));
$this->assertEquals(0, Drawing::centimetersToPixels());
$this->assertEquals($value / 2.54 * Drawing::DPI_96, Drawing::centimetersToPixels($value));
}
/**
*/
public function testPixelsEMU()
{
$value = rand(1, 100);
$this->assertEquals(0, Drawing::pixelsToEmu());
$this->assertEquals(round($value*9525), Drawing::pixelsToEmu($value));
$this->assertEquals(0, Drawing::emuToPixels());
$this->assertEquals(round($value/9525), Drawing::emuToPixels($value));
}
/**
*/
public function testPixelsPoints()
{
$value = rand(1, 100);
$this->assertEquals(0, Drawing::pixelsToPoints());
$this->assertEquals($value*0.67777777, Drawing::pixelsToPoints($value));
$this->assertEquals(0, Drawing::pointsToPixels());
$this->assertEquals($value* 1.333333333, Drawing::pointsToPixels($value));
}
/**
*/
public function testPointsCentimeters()
{
$value = rand(1, 100);
$this->assertEquals(0, Drawing::pointsToCentimeters());
$this->assertEquals($value * 1.333333333 / Drawing::DPI_96 * 2.54, Drawing::pointsToCentimeters($value));
}
/**
*/
public function testTwips()
{
$value = rand(1, 100);
// Centimeters
$this->assertEquals(0, Drawing::centimetersToTwips());
$this->assertEquals($value * 566.928, Drawing::centimetersToTwips($value));
$this->assertEquals(0, Drawing::twipsToCentimeters());
$this->assertEquals($value / 566.928, Drawing::twipsToCentimeters($value));
// Inches
$this->assertEquals(0, Drawing::inchesToTwips());
$this->assertEquals($value * 1440, Drawing::inchesToTwips($value));
$this->assertEquals(0, Drawing::twipsToInches());
$this->assertEquals($value / 1440, Drawing::twipsToInches($value));
// Pixels
$this->assertEquals(0, Drawing::twipsToPixels());
$this->assertEquals(round($value / 15.873984), Drawing::twipsToPixels($value));
}
public function testHTML()
{
$this->assertFalse(Drawing::htmlToRGB('0'));
$this->assertFalse(Drawing::htmlToRGB('00'));
$this->assertFalse(Drawing::htmlToRGB('0000'));
$this->assertFalse(Drawing::htmlToRGB('00000'));
$this->assertInternalType('array', Drawing::htmlToRGB('ABCDEF'));
$this->assertCount(3, Drawing::htmlToRGB('ABCDEF'));
$this->assertEquals(array(0xAB, 0xCD, 0xEF), Drawing::htmlToRGB('ABCDEF'));
$this->assertEquals(array(0xAB, 0xCD, 0xEF), Drawing::htmlToRGB('#ABCDEF'));
$this->assertEquals(array(0xAA, 0xBB, 0xCC), Drawing::htmlToRGB('ABC'));
$this->assertEquals(array(0xAA, 0xBB, 0xCC), Drawing::htmlToRGB('#ABC'));
}
}

View File

@@ -0,0 +1,62 @@
<?php
/**
* This file is part of PHPOffice Common
*
* PHPOffice Common is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/Common/contributors.
*
* @link https://github.com/PHPOffice/Common
* @copyright 2009-2016 PHPOffice Common contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\Common\Tests;
use PhpOffice\Common\File;
/**
* Test class for File
*
* @coversDefaultClass PhpOffice\Common\File
*/
class FileTest extends \PHPUnit\Framework\TestCase
{
/**
*/
public function testFileExists()
{
$pathResources = PHPOFFICE_COMMON_TESTS_BASE_DIR.DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR;
$this->assertTrue(File::fileExists($pathResources.'images'.DIRECTORY_SEPARATOR.'PHPPowerPointLogo.png'));
$this->assertFalse(File::fileExists($pathResources.'images'.DIRECTORY_SEPARATOR.'PHPPowerPointLogo_404.png'));
$this->assertTrue(File::fileExists('zip://'.$pathResources.'files'.DIRECTORY_SEPARATOR.'Sample_01_Simple.pptx#[Content_Types].xml'));
$this->assertFalse(File::fileExists('zip://'.$pathResources.'files'.DIRECTORY_SEPARATOR.'Sample_01_Simple.pptx#404.xml'));
$this->assertFalse(File::fileExists('zip://'.$pathResources.'files'.DIRECTORY_SEPARATOR.'404.pptx#404.xml'));
}
/**
*/
public function testGetFileContents()
{
$pathResources = PHPOFFICE_COMMON_TESTS_BASE_DIR.DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR;
$this->assertInternalType('string', File::fileGetContents($pathResources.'images'.DIRECTORY_SEPARATOR.'PHPPowerPointLogo.png'));
$this->assertFalse(File::fileGetContents($pathResources.'images'.DIRECTORY_SEPARATOR.'PHPPowerPointLogo_404.png'));
$this->assertInternalType('string', File::fileGetContents('zip://'.$pathResources.'files'.DIRECTORY_SEPARATOR.'Sample_01_Simple.pptx#[Content_Types].xml'));
$this->assertFalse(File::fileGetContents('zip://'.$pathResources.'files'.DIRECTORY_SEPARATOR.'Sample_01_Simple.pptx#404.xml'));
$this->assertFalse(File::fileGetContents('zip://'.$pathResources.'files'.DIRECTORY_SEPARATOR.'404.pptx#404.xml'));
}
/**
*/
public function testRealPath()
{
$pathFiles = PHPOFFICE_COMMON_TESTS_BASE_DIR.DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR.'files'.DIRECTORY_SEPARATOR;
$this->assertEquals($pathFiles.'Sample_01_Simple.pptx', File::realpath($pathFiles.'Sample_01_Simple.pptx'));
$this->assertEquals('zip://'.$pathFiles.'Sample_01_Simple.pptx#[Content_Types].xml', File::realpath('zip://'.$pathFiles.'Sample_01_Simple.pptx#[Content_Types].xml'));
$this->assertEquals('zip://'.$pathFiles.'Sample_01_Simple.pptx#/[Content_Types].xml', File::realpath('zip://'.$pathFiles.'Sample_01_Simple.pptx#/rels/../[Content_Types].xml'));
}
}

View File

@@ -0,0 +1,44 @@
<?php
/**
* This file is part of PHPOffice Common
*
* PHPOffice Common is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/Common/contributors.
*
* @link https://github.com/PHPOffice/Common
* @copyright 2009-2016 PHPOffice Common contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\Common\Tests;
use PhpOffice\Common\Font;
/**
* Test class for Font
*
* @coversDefaultClass PhpOffice\Common\Font
*/
class FontTest extends \PHPUnit\Framework\TestCase
{
/**
*/
public function testMath()
{
$value = rand(1, 100);
$this->assertEquals(16, Font::fontSizeToPixels());
$this->assertEquals((16 / 12) * $value, Font::fontSizeToPixels($value));
$this->assertEquals(96, Font::inchSizeToPixels());
$this->assertEquals(96 * $value, Font::inchSizeToPixels($value));
$this->assertEquals(37.795275591, Font::centimeterSizeToPixels());
$this->assertEquals(37.795275591 * $value, Font::centimeterSizeToPixels($value));
$this->assertEquals($value / 2.54 * 1440, Font::centimeterSizeToTwips($value));
$this->assertEquals($value * 1440, Font::inchSizeToTwips($value));
$this->assertEquals($value / 96 * 1440, Font::pixelSizeToTwips($value));
$this->assertEquals($value / 72 * 1440, Font::pointSizeToTwips($value));
}
}

View File

@@ -0,0 +1,89 @@
<?php
/**
* This file is part of PHPOffice Common
*
* PHPOffice Common is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/Common/contributors.
*
* @link https://github.com/PHPOffice/Common
* @copyright 2009-2016 PHPOffice Common contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\Common\Tests\Microsoft;
use PhpOffice\Common\Microsoft\PasswordEncoder;
/**
* Test class for PhpOffice\Common\PasswordEncoder
* @coversDefaultClass \PhpOffice\Common\PasswordEncoder
*/
class PasswordEncoderTest extends \PHPUnit\Framework\TestCase
{
/**
* Test that a password can be hashed without specifying any additional parameters
*/
public function testEncodePassword()
{
//given
$password = 'test';
//when
$hashPassword = PasswordEncoder::hashPassword($password);
//then
$this->assertEquals('M795/MAlmGU8RIsY9Q9uDLHC7bk=', $hashPassword);
}
/**
* Test that a password can be hashed with a custom salt
*/
public function testEncodePasswordWithSalt()
{
//given
$password = 'test';
$salt = base64_decode('uq81pJRRGFIY5U+E9gt8tA==');
//when
$hashPassword = PasswordEncoder::hashPassword($password, PasswordEncoder::ALGORITHM_SHA_1, $salt);
//then
$this->assertEquals('QiDOcpia1YzSVJPiKPwWebl9p/0=', $hashPassword);
}
/**
* Test that the encoder falls back on SHA-1 if a non supported algorithm is given
*/
public function testDefaultsToSha1IfUnsupportedAlgorithm()
{
//given
$password = 'test';
$salt = base64_decode('uq81pJRRGFIY5U+E9gt8tA==');
//when
$hashPassword = PasswordEncoder::hashPassword($password, PasswordEncoder::ALGORITHM_MAC, $salt);
//then
$this->assertEquals('QiDOcpia1YzSVJPiKPwWebl9p/0=', $hashPassword);
}
/**
* Test that the encoder falls back on SHA-1 if a non supported algorithm is given
*/
public function testEncodePasswordWithNullAsciiCodeInPassword()
{
//given
$password = 'test' . chr(0);
$salt = base64_decode('uq81pJRRGFIY5U+E9gt8tA==');
//when
$hashPassword = PasswordEncoder::hashPassword($password, PasswordEncoder::ALGORITHM_MAC, $salt, 1);
//then
$this->assertEquals('rDV9sgdDsztoCQlvRCb1lF2wxNg=', $hashPassword);
}
}

View File

@@ -0,0 +1,89 @@
<?php
/**
* This file is part of PHPOffice Common
*
* PHPOffice Common is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/Common/contributors.
*
* @link https://github.com/PHPOffice/Common
* @copyright 2009-2016 PHPOffice Common contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\Common\Tests;
use PhpOffice\Common\Text;
/**
* Test class for Text
*
* @coversDefaultClass PhpOffice\Common\Text
*/
class TextTest extends \PHPUnit\Framework\TestCase
{
/**
*/
public function testControlCharacters()
{
$this->assertEquals('', Text::controlCharacterPHP2OOXML());
$this->assertEquals('aeiou', Text::controlCharacterPHP2OOXML('aeiou'));
$this->assertEquals('àéîöù', Text::controlCharacterPHP2OOXML('àéîöù'));
$value = rand(0, 8);
$this->assertEquals('_x'.sprintf('%04s', strtoupper(dechex($value))).'_', Text::controlCharacterPHP2OOXML(chr($value)));
$this->assertEquals('', Text::controlCharacterOOXML2PHP(''));
$this->assertEquals(chr(0x08), Text::controlCharacterOOXML2PHP('_x0008_'));
}
public function testNumberFormat()
{
$this->assertEquals('2.1', Text::numberFormat('2.06', 1));
$this->assertEquals('2.1', Text::numberFormat('2.12', 1));
$this->assertEquals('1234.0', Text::numberFormat(1234, 1));
}
public function testChr()
{
$this->assertEquals('A', Text::chr(65));
$this->assertEquals('A', Text::chr(0x41));
$this->assertEquals('é', Text::chr(233));
$this->assertEquals('é', Text::chr(0xE9));
$this->assertEquals('⼳', Text::chr(12083));
$this->assertEquals('⼳', Text::chr(0x2F33));
$this->assertEquals('🌃', Text::chr(127747));
$this->assertEquals('🌃', Text::chr(0x1F303));
$this->assertEquals('', Text::chr(2097152));
}
/**
* Is UTF8
*/
public function testIsUTF8()
{
$this->assertTrue(Text::isUTF8(''));
$this->assertTrue(Text::isUTF8('éééé'));
$this->assertFalse(Text::isUTF8(utf8_decode('éééé')));
}
/**
* Test unicode conversion
*/
public function testToUnicode()
{
$this->assertEquals('a', Text::toUnicode('a'));
$this->assertEquals('\uc0{\u8364}', Text::toUnicode('€'));
$this->assertEquals('\uc0{\u233}', Text::toUnicode('é'));
}
/**
* Test remove underscore prefix
*/
public function testRemoveUnderscorePrefix()
{
$this->assertEquals('item', Text::removeUnderscorePrefix('_item'));
}
}

View File

@@ -0,0 +1,135 @@
<?php
/**
* This file is part of PHPOffice Common
*
* PHPOffice Common is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/Common/contributors.
*
* @link https://github.com/PHPOffice/Common
* @copyright 2009-2017 PHPOffice Common contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\Common\Tests;
use PhpOffice\Common\XMLReader;
/**
* Test class for XMLReader
*
* @coversDefaultClass PhpOffice\Common\XMLReader
*/
class XMLReaderTest extends \PHPUnit\Framework\TestCase
{
/**
* Test reading XML from string
*/
public function testDomFromString()
{
$reader = new XMLReader();
$reader->getDomFromString('<element attr="test"><child attr="subtest">AAA</child></element>');
$this->assertTrue($reader->elementExists('/element/child'));
$this->assertEquals('AAA', $reader->getElement('/element/child')->textContent);
$this->assertEquals('AAA', $reader->getValue('/element/child'));
$this->assertEquals('test', $reader->getAttribute('attr', $reader->getElement('/element')));
$this->assertEquals('subtest', $reader->getAttribute('attr', $reader->getElement('/element'), 'child'));
}
/**
* Test reading XML from zip
*/
public function testDomFromZip()
{
$pathResources = PHPOFFICE_COMMON_TESTS_BASE_DIR.DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR.'files'.DIRECTORY_SEPARATOR;
$reader = new XMLReader();
$reader->getDomFromZip($pathResources. 'reader.zip', 'test.xml');
$this->assertTrue($reader->elementExists('/element/child'));
$this->assertFalse($reader->getDomFromZip($pathResources. 'reader.zip', 'non_existing_xml_file.xml'));
}
/**
* Test that read from non existing archive throws exception
*
* @expectedException Exception
*/
public function testThrowsExceptionOnNonExistingArchive()
{
$pathResources = PHPOFFICE_COMMON_TESTS_BASE_DIR.DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR.'files'.DIRECTORY_SEPARATOR;
$reader = new XMLReader();
$reader->getDomFromZip($pathResources. 'readers.zip', 'test.xml');
}
/**
* Test elements count
*/
public function testCountElements()
{
$reader = new XMLReader();
$reader->getDomFromString('<element attr="test"><child>AAA</child><child>BBB</child></element>');
$this->assertEquals(2, $reader->countElements('/element/child'));
}
/**
* Test read non existing elements
*/
public function testReturnNullOnNonExistingNode()
{
$reader = new XMLReader();
$this->assertEmpty($reader->getElements('/element/children'));
$reader->getDomFromString('<element><child>AAA</child></element>');
$this->assertNull($reader->getElement('/element/children'));
$this->assertNull($reader->getValue('/element/children'));
}
/**
* Test that xpath fails if custom namespace is not registered
*/
public function testShouldThrowExceptionIfNamespaceIsNotKnown()
{
try {
$reader = new XMLReader();
$reader->getDomFromString('<element><test:child xmlns:test="http://phpword.com/my/custom/namespace">AAA</test:child></element>');
$this->assertTrue($reader->elementExists('/element/test:child'));
$this->assertEquals('AAA', $reader->getElement('/element/test:child')->textContent);
$this->fail();
} catch (\Exception $e) {
$this->assertTrue(true);
}
}
/**
* Test reading XML with manually registered namespace
*/
public function testShouldParseXmlWithCustomNamespace()
{
$reader = new XMLReader();
$reader->getDomFromString('<element><test:child xmlns:test="http://phpword.com/my/custom/namespace">AAA</test:child></element>');
$reader->registerNamespace('test', 'http://phpword.com/my/custom/namespace');
$this->assertTrue($reader->elementExists('/element/test:child'));
$this->assertEquals('AAA', $reader->getElement('/element/test:child')->textContent);
}
/**
* Test that xpath fails if custom namespace is not registered
*
* @expectedException InvalidArgumentException
*/
public function testShouldThowExceptionIfTryingToRegisterNamespaceBeforeReadingDoc()
{
$reader = new XMLReader();
$reader->registerNamespace('test', 'http://phpword.com/my/custom/namespace');
}
}

View File

@@ -0,0 +1,71 @@
<?php
/**
* This file is part of PHPOffice Common
*
* PHPOffice Common is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/Common/contributors.
*
* @link https://github.com/PHPOffice/Common
* @copyright 2009-2016 PHPOffice Common contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\Common\Tests;
use PhpOffice\Common\XMLWriter;
/**
* Test class for XMLWriter
*
* @coversDefaultClass PhpOffice\Common\XMLWriter
*/
class XMLWriterTest extends \PHPUnit\Framework\TestCase
{
/**
*/
public function testConstruct()
{
// Memory
$object = new XMLWriter();
$object->startElement('element');
$object->text('AAA');
$object->endElement();
$this->assertEquals('<element>AAA</element>'.chr(10), $object->getData());
// Disk
$object = new XMLWriter(XMLWriter::STORAGE_DISK);
$object->startElement('element');
$object->text('BBB');
$object->endElement();
$this->assertEquals('<element>BBB</element>'.chr(10), $object->getData());
}
public function testWriteAttribute()
{
$xmlWriter = new XMLWriter();
$xmlWriter->startElement('element');
$xmlWriter->writeAttribute('name', 'value');
$xmlWriter->endElement();
$this->assertSame('<element name="value"/>' . chr(10), $xmlWriter->getData());
}
public function testWriteAttributeShouldWriteFloatValueLocaleIndependent()
{
$value = 1.2;
$xmlWriter = new XMLWriter();
$xmlWriter->startElement('element');
$xmlWriter->writeAttribute('name', $value);
$xmlWriter->endElement();
setlocale(LC_NUMERIC, 'de_DE.UTF-8', 'de');
$this->assertSame('1,2', (string)$value);
$this->assertSame('<element name="1.2"/>' . chr(10), $xmlWriter->getData());
}
}

View File

@@ -0,0 +1,33 @@
<?php
namespace PhpOffice\Common\Tests;
class TestHelperZip
{
public static function assertFileExists($fileZip, $path)
{
$oZip = new \ZipArchive;
if ($oZip->open($fileZip) !== true) {
return false;
}
if ($oZip->statName($path) === false) {
return false;
}
return true;
}
public static function assertFileContent($fileZip, $path, $content)
{
$oZip = new \ZipArchive;
if ($oZip->open($fileZip) !== true) {
return false;
}
$zipFileContent = $oZip->getFromName($path);
if ($zipFileContent === false) {
return false;
}
if ($zipFileContent != $content) {
return false;
}
return true;
}
}

View File

@@ -0,0 +1,175 @@
<?php
/**
* This file is part of PHPPowerPoint - A pure PHP library for reading and writing
* word processing documents.
*
* PHPPowerPoint is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPPowerPoint/contributors.
*
* @link https://github.com/PHPOffice/PHPPowerPoint
* @copyright 2010-2016 PHPPowerPoint contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpPowerpoint\Tests;
/**
* DOM wrapper class
*/
class XmlDocument
{
/**
* Path
*
* @var string $path
*/
private $path;
/**
* DOMDocument object
*
* @var \DOMDocument
*/
private $dom;
/**
* DOMXpath object
*
* @var \DOMXpath
*/
private $xpath;
/**
* File name
*
* @var string
*/
private $file;
/**
* Create new instance
*
* @param string $path
*/
public function __construct($path)
{
$this->path = realpath($path);
}
/**
* Get DOM from file
*
* @param string $file
* @return \DOMDocument
*/
public function getFileDom($file = 'word/document.xml')
{
if (null !== $this->dom && $file === $this->file) {
return $this->dom;
}
$this->xpath = null;
$this->file = $file;
$file = $this->path . '/' . $file;
$this->dom = new \DOMDocument();
$this->dom->load($file);
return $this->dom;
}
/**
* Get node list
*
* @param string $path
* @param string $file
* @return \DOMNodeList
*/
public function getNodeList($path, $file = 'word/document.xml')
{
if ($this->dom === null || $file !== $this->file) {
$this->getFileDom($file);
}
if (null === $this->xpath) {
$this->xpath = new \DOMXpath($this->dom);
}
return $this->xpath->query($path);
}
/**
* Get element
*
* @param string $path
* @param string $file
* @return \DOMElement
*/
public function getElement($path, $file = 'word/document.xml')
{
$elements = $this->getNodeList($path, $file);
return $elements->item(0);
}
/**
* Get file name
*
* @return string
*/
public function getFile()
{
return $this->file;
}
/**
* Get path
*
* @return string
*/
public function getPath()
{
return $this->path;
}
/**
* Get element attribute
*
* @param string $path
* @param string $attribute
* @param string $file
* @return string
*/
public function getElementAttribute($path, $attribute, $file = 'word/document.xml')
{
return $this->getElement($path, $file)->getAttribute($attribute);
}
/**
* Get element attribute
*
* @param string $path
* @param string $attribute
* @param string $file
* @return string
*/
public function attributeElementExists($path, $attribute, $file = 'word/document.xml')
{
return $this->getElement($path, $file)->hasAttribute($attribute);
}
/**
* Check if element exists
*
* @param string $path
* @param string $file
* @return string
*/
public function elementExists($path, $file = 'word/document.xml')
{
$nodeList = $this->getNodeList($path, $file);
return !($nodeList->length == 0);
}
}

View File

@@ -0,0 +1,52 @@
<?php
/**
* This file is part of PHPOffice Common
*
* PHPOffice Common is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/Common/contributors.
*
* @copyright 2010-2016 PHPOffice Common contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
* @link https://github.com/PHPOffice/Common
*/
date_default_timezone_set('UTC');
// defining base dir for tests
if (!defined('PHPOFFICE_COMMON_TESTS_BASE_DIR')) {
define('PHPOFFICE_COMMON_TESTS_BASE_DIR', realpath(__DIR__));
}
$vendor = realpath(__DIR__ . '/../vendor');
if (file_exists($vendor . "/autoload.php")) {
require $vendor . "/autoload.php";
} else {
$vendor = realpath(__DIR__ . '/../../../');
if (file_exists($vendor . "/autoload.php")) {
require $vendor . "/autoload.php";
} else {
throw new Exception("Unable to load dependencies");
}
}
spl_autoload_register(function ($class) {
$class = ltrim($class, '\\');
$prefix = 'PhpOffice\\Common\\Tests';
if (strpos($class, $prefix) === 0) {
$class = str_replace('\\', DIRECTORY_SEPARATOR, $class);
$class = join(DIRECTORY_SEPARATOR, array('Common', 'Tests', '_includes')) .
substr($class, strlen($prefix));
$file = __DIR__ . DIRECTORY_SEPARATOR . $class . '.php';
if (file_exists($file)) {
require_once $file;
}
}
});
require_once __DIR__ . "/../src/Common/Autoloader.php";
\PhpOffice\Common\Autoloader::register();

Binary file not shown.

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.7 KiB