Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release 0.2.8 #22

Merged
merged 32 commits into from
Jul 12, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
d464b88
Assert zip adapters return values
PowerKiKi Feb 26, 2017
5aaa2f6
Merge pull request #14 from PowerKiKi/zip
Progi1984 Mar 13, 2017
ebdfee3
Utility to get an Office compatible hash of a password
troosan Nov 24, 2017
93e2de8
Scrutinizer fixes
troosan Nov 24, 2017
024bace
Assert zip adapters return values
PowerKiKi Feb 26, 2017
3deecee
Utility to get an Office compatible hash of a password
troosan Nov 24, 2017
90551b1
Scrutinizer fixes
troosan Nov 24, 2017
2e2120e
Merge branch 'add_password_hash' of https://github.com/troosan/Common…
troosan Nov 25, 2017
99f3853
formatting
troosan Nov 25, 2017
9a47eda
modified method to take string as input instead of algorithmSid
troosan Nov 25, 2017
9fbcbe3
Unit tests on PHP 7.1 & PHP 7.2
Progi1984 Dec 1, 2017
39209e6
Unit tests on PHP 7.1 & PHP 7.2
Progi1984 Dec 1, 2017
b5198e9
Merge pull request #16 from PHPOffice/testsVersionPhp
Progi1984 Dec 1, 2017
cf94f10
Merge branch 'develop' into add_password_hash
troosan Dec 1, 2017
b388d31
Merge branch 'develop' into add_password_hash
troosan Dec 1, 2017
291e2e0
add possibility to register namespaces to DOMXpath
troosan Dec 13, 2017
7b8d18a
code formatting
troosan Dec 13, 2017
bbc5874
Merge branch 'add_password_hash' of https://github.com/troosan/Common…
troosan Dec 13, 2017
6241c5b
add method to get algorithm id from name
troosan Dec 13, 2017
67bc52a
Clean elses
carusogabriel Dec 14, 2017
f60875f
Add tests for the XMLReader class
troosan Dec 16, 2017
75b7a6f
format code
troosan Dec 16, 2017
bb41d13
Merge pull request #18 from carusogabriel/clean-elses
Progi1984 Dec 18, 2017
5d35fcf
Merge pull request #17 from troosan/add_register_namespace_to_xml_reader
Progi1984 Dec 18, 2017
89e2fd3
restore the isUTF8 method
troosan Feb 17, 2018
ed83841
copy/paste issue
troosan Feb 17, 2018
3a4c839
Merge pull request #19 from troosan/un-deprecate_is_utf8
Progi1984 Feb 19, 2018
2648b3c
Write attribute's value of type float independently of locale
Trainmaster Apr 10, 2018
47b3183
Try different locale
Trainmaster Apr 10, 2018
2cf7240
Merge pull request #21 from Trainmaster/fix-write-attribute-with-floa…
Progi1984 May 23, 2018
b77fa41
fix typos
troosan May 23, 2018
d489595
Merge pull request #15 from troosan/add_password_hash
Progi1984 May 24, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add tests for the XMLReader class
  • Loading branch information
troosan committed Dec 16, 2017
commit f60875fe4a87215903be2188a89e4bbce2199112
126 changes: 126 additions & 0 deletions tests/Common/Tests/XMLReaderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?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');
}

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'));
}

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
*
* @expectedException Exception
*/
public function testShouldThrowExceptionIfNamespaceIsNotKnown()
{
$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);
}

/**
* 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');
}
}
Binary file added tests/resources/files/reader.zip
Binary file not shown.