Author: kn Date: Wed Feb 6 15:14:49 2008 New Revision: 7312 Log: - Added tests for creating XML documents. - Added exeptions - Added access to document options
Added: experimental/Document/src/exceptions/errnous_xml.php (with props) experimental/Document/src/exceptions/exception.php (with props) experimental/Document/src/options/document_base.php (with props) experimental/Document/tests/document_xml_base_test.php (with props) experimental/Document/tests/files/xhtml_sample_errnous.xml Modified: experimental/Document/design/class_diagram.png experimental/Document/src/document/rst.php experimental/Document/src/document/xml/docbook.php experimental/Document/src/document/xml/ezp3.php experimental/Document/src/document/xml/ezp4.php experimental/Document/src/document/xml/xhtml.php experimental/Document/src/document/xml_base.php experimental/Document/src/document_autoload.php experimental/Document/src/interfaces/document.php experimental/Document/src/options/document_xml_base.php experimental/Document/tests/document_options_xml_base_test.php experimental/Document/tests/suite.php Modified: experimental/Document/design/class_diagram.png ============================================================================== Binary files - no diff available. Modified: experimental/Document/src/document/rst.php ============================================================================== --- experimental/Document/src/document/rst.php [iso-8859-1] (original) +++ experimental/Document/src/document/rst.php [iso-8859-1] Wed Feb 6 15:14:49 2008 @@ -19,21 +19,16 @@ class ezcDocumentRst extends ezcDocument { /** - * XML document base options. - * - * @var ezcDocumentXmlBaseOptions - */ - protected $options; - - /** - * Construct document xml base. + * Construct RST document. * * @ignore * @return void */ - public function __construct() + public function __construct( ezcDocumentRstOptions $options = null ) { - $this->options = new ezcDocumentRstOptions(); + parent::__construct( $options === null ? + new ezcDocumentRstOptions() : + $options ); } /** Modified: experimental/Document/src/document/xml/docbook.php ============================================================================== --- experimental/Document/src/document/xml/docbook.php [iso-8859-1] (original) +++ experimental/Document/src/document/xml/docbook.php [iso-8859-1] Wed Feb 6 15:14:49 2008 @@ -24,9 +24,11 @@ * @ignore * @return void */ - public function __construct() + public function __construct( ezcDocumentDocbookOptions $options = null ) { - $this->options = new ezcDocumentDocbookOptions(); + parent::__construct( $options === null ? + new ezcDocumentDocbookOptions() : + $options ); } /** Modified: experimental/Document/src/document/xml/ezp3.php ============================================================================== --- experimental/Document/src/document/xml/ezp3.php [iso-8859-1] (original) +++ experimental/Document/src/document/xml/ezp3.php [iso-8859-1] Wed Feb 6 15:14:49 2008 @@ -24,9 +24,11 @@ * @ignore * @return void */ - public function __construct() + public function __construct( ezcDocumentEzp3XmlOptions $options = null ) { - $this->options = new ezcDocumentEzp3XmlOptions(); + parent::__construct( $options === null ? + new ezcDocumentEzp3XmlOptions() : + $options ); } /** Modified: experimental/Document/src/document/xml/ezp4.php ============================================================================== --- experimental/Document/src/document/xml/ezp4.php [iso-8859-1] (original) +++ experimental/Document/src/document/xml/ezp4.php [iso-8859-1] Wed Feb 6 15:14:49 2008 @@ -24,9 +24,11 @@ * @ignore * @return void */ - public function __construct() + public function __construct( ezcDocumentEzp4XmlOptions $options = null ) { - $this->options = new ezcDocumentEzp4XmlOptions(); + parent::__construct( $options === null ? + new ezcDocumentEzp4XmlOptions() : + $options ); } /** Modified: experimental/Document/src/document/xml/xhtml.php ============================================================================== --- experimental/Document/src/document/xml/xhtml.php [iso-8859-1] (original) +++ experimental/Document/src/document/xml/xhtml.php [iso-8859-1] Wed Feb 6 15:14:49 2008 @@ -24,9 +24,11 @@ * @ignore * @return void */ - public function __construct() + public function __construct( ezcDocumentXhtmlOptions $options = null ) { - $this->options = new ezcDocumentXhtmlOptions(); + parent::__construct( $options === null ? + new ezcDocumentXhtmlOptions() : + $options ); } /** Modified: experimental/Document/src/document/xml_base.php ============================================================================== --- experimental/Document/src/document/xml_base.php [iso-8859-1] (original) +++ experimental/Document/src/document/xml_base.php [iso-8859-1] Wed Feb 6 15:14:49 2008 @@ -24,24 +24,6 @@ * @var DOMDocument */ protected $document; - - /** - * XML document base options. - * - * @var ezcDocumentXmlBaseOptions - */ - protected $options; - - /** - * Construct document xml base. - * - * @ignore - * @return void - */ - public function __construct() - { - $this->options = new ezcDocumentXmlBaseOptions(); - } /** * Create document from input string @@ -75,6 +57,19 @@ { throw new ezcDocumentErrnousXmlException( $errors ); } + } + + /** + * Return internal document + * + * Return internally used structure, which is an instance of DOMDocument in + * case of XML based formats. + * + * @return DOMDocument + */ + public function getDomDocument() + { + return $this->document; } /** Modified: experimental/Document/src/document_autoload.php ============================================================================== --- experimental/Document/src/document_autoload.php [iso-8859-1] (original) +++ experimental/Document/src/document_autoload.php [iso-8859-1] Wed Feb 6 15:14:49 2008 @@ -10,12 +10,15 @@ */ return array( + 'ezcDocumentException' => 'Document/exceptions/exception.php', + 'ezcDocumentErrnousXmlException' => 'Document/exceptions/errnous_xml.php', 'ezcDocument' => 'Document/interfaces/document.php', 'ezcDocumentConverter' => 'Document/interfaces/converter.php', 'ezcDocumentValidation' => 'Document/interfaces/validation.php', 'ezcDocumentXmlBase' => 'Document/document/xml_base.php', 'ezcDocumentXmlBaseOptions' => 'Document/options/document_xml_base.php', 'ezcDocumentXsltConverter' => 'Document/converters/xslt.php', + 'ezcDocumentBaseOptions' => 'Document/options/document_base.php', 'ezcDocumentDocbook' => 'Document/document/xml/docbook.php', 'ezcDocumentDocbookOptions' => 'Document/options/document_docbook.php', 'ezcDocumentEzp3ToEzp4Converter' => 'Document/converters/xslt/ezp3_ezp4.php', Added: experimental/Document/src/exceptions/errnous_xml.php ============================================================================== --- experimental/Document/src/exceptions/errnous_xml.php (added) +++ experimental/Document/src/exceptions/errnous_xml.php [iso-8859-1] Wed Feb 6 15:14:49 2008 @@ -1,0 +1,49 @@ +<?php +/** + * Base exception for the Document package. + * + * @package Document + * @version //autogentag// + * @copyright Copyright (C) 2005-2008 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + */ + +/** + * General exception container for the Document component. + * + * @package Document + * @version //autogentag// + */ +class ezcDocumentErrnousXmlException extends ezcDocumentException +{ + /** + * Errors occured during parsing process + * + * @var array + */ + protected $errors; + + /** + * Construct exception from array with XML errors + * + * @param array $errors + * @return void + */ + public function __construct( array $errors ) + { + $this->errors = $errors; + parent::__construct( "Errors occured while parsing the XML." ); + } + + /** + * Return array with XML errors + * + * @return array + */ + public function getXmlErrors() + { + return $this->errors; + } +} + +?> Propchange: experimental/Document/src/exceptions/errnous_xml.php ------------------------------------------------------------------------------ svn:eol-style = native Added: experimental/Document/src/exceptions/exception.php ============================================================================== --- experimental/Document/src/exceptions/exception.php (added) +++ experimental/Document/src/exceptions/exception.php [iso-8859-1] Wed Feb 6 15:14:49 2008 @@ -1,0 +1,21 @@ +<?php +/** + * Base exception for the Document package. + * + * @package Document + * @version //autogentag// + * @copyright Copyright (C) 2005-2008 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + */ + +/** + * General exception container for the Document component. + * + * @package Document + * @version //autogentag// + */ +abstract class ezcDocumentException extends ezcBaseException +{ +} + +?> Propchange: experimental/Document/src/exceptions/exception.php ------------------------------------------------------------------------------ svn:eol-style = native Modified: experimental/Document/src/interfaces/document.php ============================================================================== --- experimental/Document/src/interfaces/document.php [iso-8859-1] (original) +++ experimental/Document/src/interfaces/document.php [iso-8859-1] Wed Feb 6 15:14:49 2008 @@ -18,6 +18,25 @@ */ abstract class ezcDocument { + /** + * XML document base options. + * + * @var ezcDocumentXmlBaseOptions + */ + protected $options; + + /** + * Construct new document + * + * @param ezcFooBarOptions $options + */ + public function __construct( ezcDocumentBaseOptions $options = null ) + { + $this->options = ( $options === null ? + new ezcDocumentBaseOptions() : + $options ); + } + /** * Create document from input string * @@ -83,6 +102,74 @@ * @return void */ abstract public function createFromDocbook( ezcDocumentDocbook $document ); + + /** + * Returns the value of the property $name. + * + * @throws ezcBasePropertyNotFoundException + * if the property $name does not exist + * @param string $name + * @ignore + */ + public function __get( $name ) + { + switch ( $name ) + { + case 'options': + return $this->options; + } + + throw new ezcBasePropertyNotFoundException( $name ); + } + + /** + * Sets the property $name to $value. + * + * @throws ezcBasePropertyNotFoundException + * if the property $name does not exist + * @throws ezcBaseValueException + * if $value is not accepted for the property $name + * @param string $name + * @param mixed $value + * @ignore + */ + public function __set( $name, $value ) + { + switch ( $name ) + { + case 'options': + if ( !( $value instanceof ezcDocumentOptions ) ) + { + throw new ezcBaseValueException( 'options', $value, 'instanceof ezcDocumentOptions' ); + } + + $this->options = $value; + break; + + default: + throw new ezcBasePropertyNotFoundException( $name ); + } + } + + /** + * Returns true if the property $name is set, otherwise false. + * + * @param string $name + * @return bool + * @ignore + */ + public function __isset( $name ) + { + switch ( $name ) + { + case 'options': + return true; + + default: + return false; + } + } + } ?> Added: experimental/Document/src/options/document_base.php ============================================================================== --- experimental/Document/src/options/document_base.php (added) +++ experimental/Document/src/options/document_base.php [iso-8859-1] Wed Feb 6 15:14:49 2008 @@ -1,0 +1,37 @@ +<?php +/** + * File containing the options class for the ezcDocument class + * + * @package Document + * @version //autogen// + * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + */ + +/** + * Class containing the basic options for the ezcDocument + * + * @package Document + * @version //autogen// + */ +class ezcDocumentBaseOptions extends ezcBaseOptions +{ + + /** + * Sets the option $name to $value. + * + * @throws ezcBasePropertyNotFoundException + * if the property $name is not defined + * @throws ezcBaseValueException + * if $value is not correct for the property $name + * @param string $name + * @param mixed $value + * @ignore + */ + public function __set( $name, $value ) + { + throw new ezcBasePropertyNotFoundException( $name ); + } +} + +?> Propchange: experimental/Document/src/options/document_base.php ------------------------------------------------------------------------------ svn:eol-style = native Modified: experimental/Document/src/options/document_xml_base.php ============================================================================== --- experimental/Document/src/options/document_xml_base.php [iso-8859-1] (original) +++ experimental/Document/src/options/document_xml_base.php [iso-8859-1] Wed Feb 6 15:14:49 2008 @@ -13,11 +13,13 @@ * * @property bool $indentXml * Indent XML on output + * @property bool $failOnError + * Fail with an exception, if the XML document contains errors. * * @package Document * @version //autogen// */ -class ezcDocumentXmlBaseOptions extends ezcBaseOptions +class ezcDocumentXmlBaseOptions extends ezcDocumentBaseOptions { /** * Constructs an object with the specified values. @@ -31,6 +33,7 @@ public function __construct( array $options = array() ) { $this->indentXml = false; + $this->failOnError = true; parent::__construct( $options ); } @@ -51,6 +54,7 @@ switch ( $name ) { case 'indentXml': + case 'failOnError': if ( !is_bool( $value ) ) { throw new ezcBaseValueException( $name, $value, 'bool' ); @@ -60,7 +64,7 @@ break; default: - throw new ezcBasePropertyNotFoundException( $name ); + parent::__set( $name, $value ); } } } Modified: experimental/Document/tests/document_options_xml_base_test.php ============================================================================== --- experimental/Document/tests/document_options_xml_base_test.php [iso-8859-1] (original) +++ experimental/Document/tests/document_options_xml_base_test.php [iso-8859-1] Wed Feb 6 15:14:49 2008 @@ -33,8 +33,10 @@ { return array( array( - 'indentXml', - false, + 'indentXml', false, + ), + array( + 'failOnError', true, ), ); } @@ -44,6 +46,10 @@ return array( array( 'indentXml', + array( true, false ), + ), + array( + 'failOnError', array( true, false ), ), ); @@ -56,6 +62,10 @@ 'indentXml', array( 1, 'foo', .5, new StdClass(), array() ), ), + array( + 'failOnError', + array( 1, 'foo', .5, new StdClass(), array() ), + ), ); } } Added: experimental/Document/tests/document_xml_base_test.php ============================================================================== --- experimental/Document/tests/document_xml_base_test.php (added) +++ experimental/Document/tests/document_xml_base_test.php [iso-8859-1] Wed Feb 6 15:14:49 2008 @@ -1,0 +1,95 @@ +<?php +/** + * ezcDocTestConvertXhtmlDocbook + * + * @package Document + * @version //autogen// + * @subpackage Tests + * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + */ + +/** + * Test suite for class. + * + * @package Document + * @subpackage Tests + */ +class ezcDocumentXmlBaseTests extends ezcTestCase +{ + public static function suite() + { + return new PHPUnit_Framework_TestSuite( __CLASS__ ); + } + + public function testLoadXmlDocumentFromFile() + { + $doc = new ezcDocumentXhtml(); + $doc->loadFile( + dirname( __FILE__ ) . '/files/xhtml_sample_basic.xml' + ); + + $this->assertTrue( + $doc->getDomDocument() instanceof DOMDocument, + 'DOMDocument not created properly' + ); + } + + public function testLoadXmlDocumentFromString() + { + $string = file_get_contents( + dirname( __FILE__ ) . '/files/xhtml_sample_basic.xml' + ); + + $doc = new ezcDocumentXhtml(); + $doc->loadString( $string ); + + $this->assertTrue( + $doc->getDomDocument() instanceof DOMDocument, + 'DOMDocument not created properly' + ); + } + + public function testLoadErrnousXmlDocument() + { + $doc = new ezcDocumentXhtml(); + + try + { + $doc->loadFile( + dirname( __FILE__ ) . '/files/xhtml_sample_errnous.xml' + ); + } + catch ( ezcDocumentErrnousXmlException $e ) + { + $errors = $e->getXmlErrors(); + + $this->assertSame( + 2, + count( $errors ), + 'Expected 2 XML errors.' + ); + } + + $this->assertTrue( + $doc->getDomDocument() instanceof DOMDocument, + 'DOMDocument not created properly' + ); + } + + public function testLoadErrnousXmlDocumentSilent() + { + $doc = new ezcDocumentXhtml(); + $doc->options->failOnError = false; + $doc->loadFile( + dirname( __FILE__ ) . '/files/xhtml_sample_errnous.xml' + ); + + $this->assertTrue( + $doc->getDomDocument() instanceof DOMDocument, + 'DOMDocument not created properly' + ); + } +} + +?> Propchange: experimental/Document/tests/document_xml_base_test.php ------------------------------------------------------------------------------ svn:eol-style = native Added: experimental/Document/tests/files/xhtml_sample_errnous.xml ============================================================================== --- experimental/Document/tests/files/xhtml_sample_errnous.xml (added) +++ experimental/Document/tests/files/xhtml_sample_errnous.xml [iso-8859-1] Wed Feb 6 15:14:49 2008 @@ -1,0 +1,39 @@ +<?xml version="1.0"?> +<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> +<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> + <title>Test document</title> +</head> +<body> +<h1>Header 1</h1> +<p> +Para 1 +</p> +<h2>Header 1.1</h2> +<p> +Para 2 +</p> +<h1>Header 2</h1> +<p> +Inline <a href="http://example.com">example</a> link. +</p> +<pre> +Some literal &text +</pre> +<p> +Inline Image: <img src="http://www.google.com/intl/en_ALL/images/logo.gif"/> image +</p> +<img src="http://www.google.com/intl/en_ALL/images/logo.gif"/> +<p> +Inline tags: +<strong>storng text</strong> +<em>emphasized text</em> +<tt>literal</tt> +<sub>subscript</sub> +<sup>superscript</sup> +<q>quote</q> +<cite>cite title</cite> +<acronym>FBI</acronym> +<code>a = b</code> +</p> +</bod> +</html> Modified: experimental/Document/tests/suite.php ============================================================================== --- experimental/Document/tests/suite.php [iso-8859-1] (original) +++ experimental/Document/tests/suite.php [iso-8859-1] Wed Feb 6 15:14:49 2008 @@ -18,6 +18,7 @@ // require_once 'convert_xhtml_test.php'; require_once 'document_options_xml_base_test.php'; +require_once 'document_xml_base_test.php'; class ezcDocumentSuite extends PHPUnit_Framework_TestSuite @@ -36,6 +37,7 @@ // $this->addTest( ezcDocumentConvertEzp3Test::suite() ); $this->addTest( ezcDocumentOptionsXmlBaseTests::suite() ); + $this->addTest( ezcDocumentXmlBaseTests::suite() ); } } -- svn-components mailing list svn-components@lists.ez.no http://lists.ez.no/mailman/listinfo/svn-components