Author: as
Date: Mon Oct  8 14:21:46 2007
New Revision: 6385

Log:
- Added comments for ezcFeed.
- Added tests for ezcFeed.

Added:
    trunk/Feed/tests/test.php   (with props)
Modified:
    trunk/Feed/src/feed.php
    trunk/Feed/tests/feed_test.php
    trunk/Feed/tests/rss2/rss2_test.php

Modified: trunk/Feed/src/feed.php
==============================================================================
--- trunk/Feed/src/feed.php [iso-8859-1] (original)
+++ trunk/Feed/src/feed.php [iso-8859-1] Mon Oct  8 14:21:46 2007
@@ -10,9 +10,102 @@
  */
 
 /**
- * ezcFeed.
- *
- * @property-read array(int=>ezcFeedItem) $items The items belonging to the 
feed.
+ * Class defining a feed.
+ *
+ * A feed has a type (eg. RSS1, RSS2 or ATOM) and one or more cores (eg.
+ * Content, DublinCore).
+ *
+ * The feed type defines which processor is used to parse and generate that 
type.
+ * The following feed processors are supported by the Feed component:
+ *  - RSS1 ([EMAIL PROTECTED] ezcFeedRss1})
+ *  - RSS2 ([EMAIL PROTECTED] ezcFeedRss1})
+ *  - ATOM ([EMAIL PROTECTED] ezcFeedAtom})
+ *
+ * A new processor can be defined by creating a class which extends the class
+ * [EMAIL PROTECTED] ezcFeedProcessor} and implements the interface [EMAIL 
PROTECTED] ezcFeedParser},
+ * and adding it to the [EMAIL PROTECTED] self::$supportedFeedTypes} array.
+ *
+ * A module is a part of a feed. The following modules are supported by the 
Feed
+ * component:
+ *  - Content ([EMAIL PROTECTED] ezcFeedModuleContent})
+ *  - DublinCore ([EMAIL PROTECTED] ezcFeedModuleDublinCore})
+ *
+ * A new module can be defined by creating a class which implements the 
interface
+ * [EMAIL PROTECTED] ezcFeedModule}, and adding it to the [EMAIL PROTECTED] 
self::$supportedModules}
+ * array.
+ *
+ * A feed object can be created in different ways:
+ *  - by calling the constructor with the required feed type. Example:
+ *      <code>
+ *        // create an RSS2 feed
+ *        $feed = new ezcFeed( 'rss2' );
+ *      </code>
+ *  - by parsing an existing XML file or uri. The feed type of the resulting
+ *    ezcFeed object will be autodetected. Example:
+ *      <code>
+ *        // create an RSS2 feed from the XML file at 
http://www.example.com/rss2.xml
+ *        $feed = ezcFeed::parse( 'http://www.example.com/rss2.xml' );
+ *      </code>
+ *  - by parsing an XML document stored in a string variable. The feed type of
+ *    the resulting ezcFeed object will be autodetected. Example:
+ *      <code>
+ *        // create an RSS2 feed from the XML document stored in $xmlString
+ *        $feed = ezcFeed::parseContent( $xmlString );
+ *      </code>
+ *
+ * Operations possible upon ezcFeed objects (in the following examples $feed is
+ * an existing ezcFeed object):
+ *  - add a module to the feed. Example:
+ *      <code>
+ *        $feed->addModule( 'ezcFeedModuleDublinCore' );
+ *      </code>
+ *  - set/get a value from the feed document. Example:
+ *      <code>
+ *        $feed->title = 'News';
+ *        $title = $feed->title;
+ *      </code>
+ *  - set/get a value from a module in the feed document. Example:
+ *      <code>
+ *        $feed->DublinCore->description = 'Detailed description';
+ *        $title = $feed->DublinCore->description;
+ *      </code>
+ *  - generate an XML document from the ezcFeed object. Example:
+ *      <code>
+ *        $xml = $feed->generate();
+ *      </code>
+ *
+ * @property string $title
+ *           Required in RSS1, RSS2, ATOM.
+ * @property string $subtitle
+ *           ATOM only.
+ * @property string $link
+ *           Required in RSS2, rdf:about AND link in RSS1.
+ * @property string $description
+ *           Required in RSS1, RSS2.
+ * @property string $language
+ *           Language string.
+ * @property string $copyright
+ *           Rights in ATOM.
+ * @property string $author
+ *           Same as managingEditor in RSS2, required in ATOM.
+ * @property string $webMaster
+ *           RSS2 only.
+ * @property string $published
+ *           Same as pubDate in RSS2.
+ * @property string $updated
+ *           Same as lastBuildDate in RSS2, required in ATOM.
+ * @property string $category
+ *           Category string.
+ * @property string $generator
+ *           Generator string.
+ * @property string $ttl
+ *           Time-to-live.
+ * @property string $image
+ *           Same as icon in ATOM.
+ * @property string $id
+ *           ATOM only, required in ATOM.
+ * @property-read array(int=>ezcFeedItem) $items
+ *                The items belonging to the feed.
  *
  * @package Feed
  * @version //autogentag//
@@ -21,121 +114,105 @@
 class ezcFeed implements Iterator
 {
     /**
-     * A list of all supported feed types
+     * A list of all supported feed types.
      *
      * @var array(string=>string)
      */
-    static private $supportedFeedTypes = array(
+    protected static $supportedFeedTypes = array(
         'rss1' => 'ezcFeedRss1',
         'rss2' => 'ezcFeedRss2',
         'atom' => 'ezcFeedAtom',
     );
 
     /**
-     * A list of all supported feed modules
+     * A list of all supported feed modules.
      *
      * @var array(string=>string)
      */
-    static private $supportedModules = array(
+    protected static $supportedModules = array(
         'Content'    => 'ezcFeedModuleContent',
         'DublinCore' => 'ezcFeedModuleDublinCore',
     );
 
     /**
-     * The feed processor
+     * The feed processor.
      *
      * @var ezcFeedProcessor
-     */
-    private $feedProcessor;
-
-    private $feedType;
-
-    private $iteratorItems = array();
-    private $iteratorElementCount = 0;
-    private $iteratorPosition = 0;
-
-
-    static public function parse( $uri )
-    {
-        $xml = new DomDocument;
-        $retval = @$xml->load( $uri );
-        if ( $retval === false )
-        {
-            throw new ezcBaseFileNotFoundException( $uri );
-        }
-        return self::dispatchXml( $xml );
-    }
-
-    static public function parseContent( $content )
-    {
-        $xml = new DomDocument;
-        $retval = @$xml->loadXML( $content );
-        if ( $retval === false )
-        {
-            throw new ezcFeedParseErrorException( "Content is no valid XML." );
-        }
-        return self::dispatchXml( $xml );
-    }
-
-    protected static function dispatchXml( DOMDocument $xml )
-    {
-        foreach ( self::$supportedFeedTypes as $feedType => $feedClass )
-        {
-            $canParse = call_user_func( array( $feedClass, 'canParse' ), $xml 
);
-            if ( $canParse === true )
-            {
-                $processor = new $feedClass;
-                return $processor->parse( $xml );
-            }
-        }
-
-        throw new ezcFeedCanNotParseException( $xml->documentURI, 'Feed type 
not recognised' );
-    }
-
-    static public function getSupportedTypes()
-    {
-        return array_keys( self::$supportedFeedTypes );
-    }
-
-    static public function getSupportedModules()
-    {
-        return self::$supportedModules;
-    }
-
-    static public function getModule( $moduleName, $feedType )
-    {
-        return new self::$supportedModules[$moduleName]( $feedType );
-    }
-
-    /**
-     * Creates a new feed
+     * @ignore
+     */
+    protected $feedProcessor;
+
+    /**
+     * The feed type.
+     *
+     * @var string
+     * @ignore
+     */
+    protected $feedType;
+
+    /**
+     * The feed items to be iterated.
+     *
+     * @var array(ezcFeedItem)
+     * @ignore
+     */
+    protected $iteratorItems = array();
+
+    /**
+     * The number of feed items.
+     *
+     * @var int
+     * @ignore
+     */
+    protected $iteratorElementCount = 0;
+
+    /**
+     * The current feed item.
+     *
+     * @var int
+     * @ignore
+     */
+    protected $iteratorPosition = 0;
+
+    /**
+     * Creates a new feed of type $type.
+     *
+     * Example:
+     * <code>
+     * // create an RSS2 feed
+     * $feed = new ezcFeed( 'rss2' );
+     * </code>
      *
      * @throws ezcFeedUnsupportedTypeException
-     *         If the passed $type is an unsupported feed type
-     *
-     * @param string $type
+     *         If the passed $type is an unsupported feed type.
+     *
+     * @param string $type The feed type
      */
     public function __construct( $type )
     {
-        if ( !in_array( $type, array_keys( self::$supportedFeedTypes ) ) )
+        $type = strtolower( $type );
+
+        if ( !isset( self::$supportedFeedTypes[$type] ) )
         {
             throw new ezcFeedUnsupportedTypeException( $type );
         }
+
         $this->feedType = $type;
         $this->feedProcessor = new self::$supportedFeedTypes[$type];
     }
 
-    public function addModule( $className )
-    {
-        $moduleObj = new $className( $this->feedType );
-        if ( !$moduleObj instanceof ezcFeedModule )
-        {
-            throw new ezcFeedUnsupportedModuleException( $className );
-        }
-        $moduleName = $moduleObj->getModuleName();
-        $this->$moduleName = $this->feedProcessor->addModule( $moduleName, 
$moduleObj );
-    }
-
+    /**
+     * Sets the property $property to $value.
+     *
+     * @throws ezcBasePropertyPermissionException
+     *         If $property is a read-only property.
+     * @throws ezcBaseValueException
+     *         If trying to assign a wrong value to the property $property.
+     *
+     * @param string $property The property name
+     * @param mixed $value The property value
+     * @ignore
+     */
     public function __set( $property, $value )
     {
         switch ( $property )
@@ -157,6 +234,9 @@
             case 'id': // ATOM only, required in ATOM
                 $this->feedProcessor->setFeedElement( $property, $value );
                 break;
+
+            case 'items':
+                throw new ezcBasePropertyPermissionException( $property, 
ezcBasePropertyPermissionException::READ );
         }
 
         $modules = $this->feedProcessor->getModules();
@@ -169,9 +249,19 @@
         }
     }
 
-    public function __get( $propertyName )
-    {
-        switch ( $propertyName )
+    /**
+     * Returns the value of property $property.
+     *
+     * @throws ezcBasePropertyNotFoundException
+     *         If the property $property does not exist.
+     *
+     * @param string $property The property name
+     * @return mixed
+     * @ignore
+     */
+    public function __get( $property )
+    {
+        switch ( $property )
         {
             case 'title': // required in RSS1, RSS2, ATOM
             case 'subtitle': // ATOM only
@@ -188,16 +278,35 @@
             case 'ttl':
             case 'image': // icon in ATOM
             case 'id': // ATOM only, required in ATOM
-                return $this->feedProcessor->getFeedElement( $propertyName );
+                return $this->feedProcessor->getFeedElement( $property );
 
             case 'items':
                 return (array) $this->feedProcessor->getItems();
-        }
-        throw new Exception( "OH OH: {$propertyName}" );
-    }
-
-    /**
-     * Returns new item for this feed
+
+            default:
+                throw new ezcBasePropertyNotFoundException( $property );
+        }
+    }
+
+    /**
+     * Adds a new module to the feed.
+     *
+     * @var string $className The type of the module
+     */
+    public function addModule( $className )
+    {
+        $moduleObj = new $className( $this->feedType );
+        if ( !$moduleObj instanceof ezcFeedModule )
+        {
+            throw new ezcFeedUnsupportedModuleException( $className );
+        }
+
+        $moduleName = $moduleObj->getModuleName();
+        $this->$moduleName = $this->feedProcessor->addModule( $moduleName, 
$moduleObj );
+    }
+
+    /**
+     * Creates and returns a new feed item for this feed.
      *
      * @return ezcFeedItem
      */
@@ -208,11 +317,19 @@
         return $item;
     }
 
+    /**
+     * Generates and returns an XML document from the current object.
+     *
+     * @return string
+     */
     public function generate()
     {
         return $this->feedProcessor->generate();
     }
 
+    /**
+     * Rewinds the cursor in the items array. Required by the Iterator 
interface.
+     */
     public function rewind()
     {
         $this->iteratorItems = $this->feedProcessor->getItems();
@@ -220,24 +337,146 @@
         $this->iteratorPosition = 0;
     }
 
+    /**
+     * Returns the current item in the items array. Required by the Iterator
+     * interface.
+     *
+     * @return ezcFeedItem
+     */
     public function current()
     {
         return $this->iteratorItems[$this->iteratorPosition];
     }
 
+    /**
+     * Returns the current key in the items array. Required by the Iterator
+     * interface.
+     *
+     * @return int
+     */
     public function key()
     {
         return $this->iteratorPosition;
     }
 
+    /**
+     * Returns if the current item in the items array is not in the last 
position.
+     * Required by the Iterator interface.
+     *
+     * @return bool
+     */
     public function valid()
     {
         return $this->iteratorPosition < $this->iteratorElementCount;
     }
 
+    /**
+     * Advances the current item in the items array. Required by the Iterator
+     * interface.
+     */
     public function next()
     {
         $this->iteratorPosition++;
+    }
+
+    /**
+     * Parses the XML document in the $uri and returns an ezcFeed object with
+     * the type autodetected from the XML document.
+     *
+     * @throws ezcBaseFileNotFoundException
+     *         If the XML file at $uri could not be found.
+     *
+     * @param string $uri An URI which stores an XML document
+     * @return ezcFeed
+     */
+    public static function parse( $uri )
+    {
+        $xml = new DomDocument;
+        $retval = @$xml->load( $uri );
+        if ( $retval === false )
+        {
+            throw new ezcBaseFileNotFoundException( $uri );
+        }
+        return self::dispatchXml( $xml );
+    }
+
+    /**
+     * Parses the XML document stored in $content and returns an ezcFeed object
+     * with the type autodetected from the XML document.
+     *
+     * @throws ezcFeedParseErrorException
+     *         If $content is not a valid XML document.
+     *
+     * @param string $content A string variable which stores an XML document
+     * @return ezcFeed
+     */
+    public static function parseContent( $content )
+    {
+        $xml = new DomDocument;
+        $retval = @$xml->loadXML( $content );
+        if ( $retval === false )
+        {
+            throw new ezcFeedParseErrorException( "Content is no valid XML" );
+        }
+        return self::dispatchXml( $xml );
+    }
+
+    /**
+     * Returns the supported feed types (the keys of the
+     * [EMAIL PROTECTED] self::$supportedFeedTypes} array).
+     *
+     * @return array(string)
+     */
+    public static function getSupportedTypes()
+    {
+        return array_keys( self::$supportedFeedTypes );
+    }
+
+    /**
+     * Returns the supported modules (the [EMAIL PROTECTED] 
self::$supportedModules} array).
+     *
+     * @return array(string)
+     */
+    public static function getSupportedModules()
+    {
+        return self::$supportedModules;
+    }
+
+    /**
+     * Returns a new $moduleName module object of feed type $feedType.
+     *
+     * @param string $moduleName A module name, for example 'DublinCore'
+     * @param string $feedType A feed type, for example 'rss2'
+     * @return ezcFeedModule
+     */
+    public static function getModule( $moduleName, $feedType )
+    {
+        return new self::$supportedModules[$moduleName]( $feedType );
+    }
+
+    /**
+     * Parses the $xml object by dispatching it to the processor that can
+     * handle it.
+     *
+     * @throws ezcFeedCanNotParseException
+     *         If the $xml document could not be parsed by any available 
processor.
+     *
+     * @param DOMDocument $xml The XML object to parse
+     * @return ezcFeed
+     */
+    protected static function dispatchXml( DOMDocument $xml )
+    {
+        foreach ( self::$supportedFeedTypes as $feedType => $feedClass )
+        {
+            $canParse = call_user_func( array( $feedClass, 'canParse' ), $xml 
);
+            if ( $canParse === true )
+            {
+                $processor = new $feedClass;
+                return $processor->parse( $xml );
+            }
+        }
+
+        throw new ezcFeedCanNotParseException( $xml->documentURI, 'Feed type 
not recognized' );
     }
 }
 ?>

Modified: trunk/Feed/tests/feed_test.php
==============================================================================
--- trunk/Feed/tests/feed_test.php [iso-8859-1] (original)
+++ trunk/Feed/tests/feed_test.php [iso-8859-1] Mon Oct  8 14:21:46 2007
@@ -8,23 +8,25 @@
  * @subpackage Tests
  */
 
+include_once( 'Feed/tests/test.php' );
+
 /**
  * @package Feed
  * @subpackage Tests
  */
-class ezcFeedTest extends ezcTestCase
+class ezcFeedTest extends ezcFeedTestCase
 {
     public function testGetSupportedTypes()
     {
         $types = ezcFeed::getSupportedTypes();
         $expected = array( 'rss1', 'rss2', 'atom' );
-        self::assertEquals( $expected, $types );
+        $this->assertEquals( $expected, $types );
     }
 
     public function testCreateFeedSupported()
     {
         $feed = new ezcFeed( 'rss1' );
-        self::assertEquals( 'ezcFeed', get_class( $feed ) );
+        $this->assertEquals( 'ezcFeed', get_class( $feed ) );
     }
 
     public function testCreateFeedNotSupported()
@@ -32,18 +34,34 @@
         try
         {
             $feed = new ezcFeed( 'molecule' );
-            self::fail( 'Expected exception not thrown' );
+            $this->fail( 'Expected exception not thrown' );
         }
         catch ( ezcFeedUnsupportedTypeException $e )
         {
-            self::assertEquals( "The feed type 'molecule' is not supported.", 
$e->getMessage() );
+            $this->assertEquals( "The feed type 'molecule' is not supported.", 
$e->getMessage() );
         }
     }
 
     public function testAddModuleSupported()
     {
         $feed = new ezcFeed( 'rss2' );
+        $this->assertEquals( false, isset( $feed->DublinCore ) );
         $feed->addModule( 'ezcFeedModuleDublinCore' );
+        $this->assertEquals( true, isset( $feed->DublinCore ) );
+    }
+
+    public function testAddModuleNotSupported()
+    {
+        $feed = new ezcFeed( 'rss2' );
+        try
+        {
+            $feed->addModule( 'stdClass' );
+            $this->fail( 'Expected exception not thrown' );
+        }
+        catch ( ezcFeedUnsupportedModuleException $e )
+        {
+            $this->assertEquals( "The module 'stdClass' is not supported.", 
$e->getMessage() );
+        }
     }
 
     public function testFeedNonExistentLocal()
@@ -51,11 +69,11 @@
         try
         {
             $feed = ezcFeed::parse( 'not-here.xml' );
-            self::fail( 'Expected exception not thrown' );
+            $this->fail( 'Expected exception not thrown' );
         }
         catch ( ezcBaseFileNotFoundException $e )
         {
-            self::assertEquals( "The file 'not-here.xml' could not be found.", 
$e->getMessage() );
+            $this->assertEquals( "The file 'not-here.xml' could not be 
found.", $e->getMessage() );
         }
     }
 
@@ -64,17 +82,62 @@
         try
         {
             $feed = ezcFeed::parse( 'http://ez.no/not-here.xml' );
-            self::fail( 'Expected exception not thrown' );
+            $this->fail( 'Expected exception not thrown' );
         }
         catch ( ezcBaseFileNotFoundException $e )
         {
-            self::assertEquals( "The file 'http://ez.no/not-here.xml' could 
not be found.", $e->getMessage() );
+            $this->assertEquals( "The file 'http://ez.no/not-here.xml' could 
not be found.", $e->getMessage() );
         }
     }
 
     public function testFeedExistsRemote()
     {
         $feed = ezcFeed::parse( 'http://ez.no/rss/feed/communitynews' );
+    }
+
+    public function testParseContentBroken()
+    {
+        try
+        {
+            $feed = ezcFeed::parseContent( 'bad XML document' );
+            $this->fail( 'Expected exception not thrown' );
+        }
+        catch ( ezcFeedParseErrorException $e )
+        {
+            $this->assertEquals( "Parse error while parsing feed: Content is 
no valid XML.", $e->getMessage() );
+        }
+    }
+
+    public function testParseContentNotRecognized()
+    {
+        try
+        {
+            $feed = ezcFeed::parseContent( '<?xml version="1.0" 
encoding="utf-8"?><xxx>Content</xxx>' );
+            $this->fail( 'Expected exception not thrown' );
+        }
+        catch ( ezcFeedCanNotParseException $e )
+        {
+            $expected = "' could not be parsed: Feed type not recognized.";
+            $result = substr( $e->getMessage(), strlen( $e->getMessage() ) - 
48 );
+            $this->assertEquals( $expected, $result );
+        }
+    }
+
+    public function testFeedProperties()
+    {
+        $feed = new ezcFeed( 'rss2' );
+
+        $this->readonlyPropertyTest( $feed, 'items' );
+
+        try
+        {
+            $value = $feed->no_such_property;
+            $this->fail( "Expected exception was not thrown." );
+        }
+        catch ( ezcBasePropertyNotFoundException $e )
+        {
+            $this->assertEquals( "No such property name 'no_such_property'.", 
$e->getMessage() );
+        }
     }
 
     public static function suite()

Modified: trunk/Feed/tests/rss2/rss2_test.php
==============================================================================
--- trunk/Feed/tests/rss2/rss2_test.php [iso-8859-1] (original)
+++ trunk/Feed/tests/rss2/rss2_test.php [iso-8859-1] Mon Oct  8 14:21:46 2007
@@ -188,6 +188,7 @@
         foreach ( $feed as $item )
         {
             $returned[] = array( $item->title, $item->link, $item->description 
);
+            $keys[] = $feed->key();
         }
         $expected = array (
   0 =>
@@ -242,6 +243,7 @@
   ),
 );
         self::assertEquals( $expected, $returned );
+        self::assertEquals( array_keys( $expected ), $keys );
     }
 
     public function testWithModule1()

Added: trunk/Feed/tests/test.php
==============================================================================
--- trunk/Feed/tests/test.php (added)
+++ trunk/Feed/tests/test.php [iso-8859-1] Mon Oct  8 14:21:46 2007
@@ -1,0 +1,301 @@
+<?php
+/**
+ * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ * @filesource
+ * @package Feed
+ * @version //autogen//
+ * @subpackage Tests
+ */
+
+/**
+ * @package Feed
+ * @version //autogen//
+ * @subpackage Tests
+ * @access private
+ */
+class ezcFeedTestCase extends ezcTestCase
+{
+    /**
+     * Tests assigning an invalid value to a property.
+     *
+     * Expects that an ezcBaseValueException is raised by the invalid value.
+     *
+     * @param object $properties An object which implements properties access
+     * @param string $property The property of the $properties object to test
+     * @param mixed $value The value to try to assign to $property
+     * @param string $allowedValue The values which are allowed for $property
+     */
+    protected function invalidPropertyTest( $properties, $property, $value, 
$allowedValue )
+    {
+        try
+        {
+            $properties->$property = $value;
+            $this->fail( "Expected exception was not thrown." );
+        }
+        catch ( ezcBaseValueException $e )
+        {
+            $value = is_array( $value ) ? serialize( $value ) : $value;
+            $this->assertEquals( "The value '{$value}' that you were trying to 
assign to setting '{$property}' is invalid. Allowed values are: 
{$allowedValue}.", $e->getMessage() );
+        }
+    }
+
+    /**
+     * Tests assigning a read-only property.
+     *
+     * Expects that an ezcBasePropertyPermissionException is raised.
+     *
+     * @param object $properties An object which implements properties access
+     * @param string $property The property of the $properties object to test
+     */
+    protected function readonlyPropertyTest( $properties, $property )
+    {
+        try
+        {
+            $properties->$property = null;
+            $this->fail( "Expected exception was not thrown." );
+        }
+        catch ( ezcBasePropertyPermissionException $e )
+        {
+            $this->assertEquals( "The property '{$property}' is read-only.", 
$e->getMessage() );
+        }
+    }
+
+    /**
+     * Tests assigning a value to a missing property.
+     *
+     * Expects that an ezcBasePropertyNotFoundException is raised by the 
missing
+     * property.
+     *
+     * @param object $properties An object which implements properties access
+     * @param string $property The property of the $properties object to test
+     */
+    protected function missingPropertyTest( $properties, $property )
+    {
+        try
+        {
+            $properties->$property = null;
+            $this->fail( "Expected exception was not thrown." );
+        }
+        catch ( ezcBasePropertyNotFoundException $e )
+        {
+            $this->assertEquals( "No such property name '{$property}'.", 
$e->getMessage() );
+        }
+
+        // workaround around a bug (?) - __isset() in ezcBaseOptions complains 
and warns
+        // that the second parameter for array_exists() must be an array or an 
object
+        if ( !$properties instanceof ezcBaseOptions )
+        {
+            try
+            {
+                $value = $properties->$property;
+                $this->fail( "Expected exception was not thrown." );
+            }
+            catch ( ezcBasePropertyNotFoundException $e )
+            {
+                $this->assertEquals( "No such property name '{$property}'.", 
$e->getMessage() );
+            }
+        }
+    }
+
+    /**
+     * Tests if a property is set.
+     *
+     * Compares the result of isset() with $value.
+     *
+     * @param object $properties An object which implements properties access
+     * @param string $property The property of the $properties object to test
+     * @param bool $value True if expecting that $property is set, false 
otherwise
+     */
+    protected function issetPropertyTest( $properties, $property, $value )
+    {
+        $this->assertEquals( $value, isset( $properties->$property ) );
+    }
+
+    /**
+     * Tests assigning a non-existent path to a property.
+     *
+     * Expects that an ezcBaseFileNotFoundException is raised by the missing
+     * path.
+     *
+     * @param object $properties An object which implements properties access
+     * @param string $property The property of the $properties object to test
+     * @param string $value A path which does not exist
+     */
+    protected function missingFileTest( $properties, $property, $value )
+    {
+        try
+        {
+            $properties->$property = $value;
+            $this->fail( "Expected exception was not thrown" );
+        }
+        catch ( ezcBaseFileNotFoundException $e )
+        {
+            $this->assertEquals( "The file '{$value}' could not be found.", 
$e->getMessage() );
+        }
+    }
+
+    /**
+     * Tests assigning an unreadable path to a property.
+     *
+     * Expects that an ezcBaseFilePermissionException is raised by the 
unreadable
+     * path.
+     *
+     * This function creates a temporary file and makes it unreadable.
+     *
+     * @param object $properties An object which implements properties access
+     * @param string $property The property of the $properties object to test
+     * @param string $value A filename without paths or slashes
+     */
+    protected function unreadableFileTest( $properties, $property, $value )
+    {
+        $tempDir = $this->createTempDir( get_class( $this ) );
+        $path = $tempDir . DIRECTORY_SEPARATOR . $value;
+        $fh = fopen( $path, "wb" );
+        fwrite( $fh, "some values" );
+        fclose( $fh );
+        chmod( $path, 0 );
+
+        try
+        {
+            $properties->$property = $path;
+            $this->removeTempDir();
+            $this->fail( "Expected exception was not thrown." );
+        }
+        catch ( ezcBaseFilePermissionException $e )
+        {
+            $this->assertEquals( "The file '{$path}' can not be opened for 
reading.", $e->getMessage() );
+        }
+
+        $this->removeTempDir();
+    }
+
+    /**
+     * Tests assigning an unwritable path to a property.
+     *
+     * Expects that an ezcBaseFilePermissionException is raised by the 
unwritable
+     * path.
+     *
+     * This function creates a temporary file and makes it unwritable.
+     *
+     * @param object $properties An object which implements properties access
+     * @param string $property The property of the $properties object to test
+     * @param string $value A filename without paths or slashes
+     */
+    protected function unwritableFileTest( $properties, $property, $value )
+    {
+        $tempDir = $this->createTempDir( get_class( $this ) );
+        $path = $tempDir . DIRECTORY_SEPARATOR . $value;
+        $fh = fopen( $path, "wb" );
+        fwrite( $fh, "some values" );
+        fclose( $fh );
+        chmod( $path, 0 );
+
+        try
+        {
+            $properties->$property = $path;
+            $this->removeTempDir();
+            $this->fail( "Expected exception was not thrown." );
+        }
+        catch ( ezcBaseFilePermissionException $e )
+        {
+            $this->assertEquals( "The file '{$path}' can not be opened for 
writing.", $e->getMessage() );
+        }
+
+        $this->removeTempDir();
+    }
+
+    /**
+     * Tests assigning a non-existent directory to a property.
+     *
+     * Expects that an ezcBaseFileNotFoundException is raised by the missing
+     * directory.
+     *
+     * @param object $properties An object which implements properties access
+     * @param string $property The property of the $properties object to test
+     * @param string $value A directory which does not exist
+     */
+    protected function missingDirTest( $properties, $property, $value )
+    {
+        try
+        {
+            $properties->$property = $value;
+            $this->fail( "Expected exception was not thrown" );
+        }
+        catch ( ezcBaseFileNotFoundException $e )
+        {
+            $this->assertEquals( "The file '{$value}' could not be found.", 
$e->getMessage() );
+        }
+    }
+
+    /**
+     * Tests assigning an unreadable directory to a property.
+     *
+     * Expects that an ezcBaseFilePermissionException is raised by the 
unreadable
+     * path.
+     *
+     * This function creates a temporary directory and makes it unreadable.
+     *
+     * @param object $properties An object which implements properties access
+     * @param string $property The property of the $properties object to test
+     * @param string $value A directory name without paths or slashes
+     */
+    protected function unreadableDirTest( $properties, $property, $value )
+    {
+        $tempDir = $this->createTempDir( get_class( $this ) );
+        $path = $tempDir . DIRECTORY_SEPARATOR . $value;
+        mkdir( $path );
+        chmod( $path, 0 );
+
+        try
+        {
+            $properties->$property = $path;
+            chmod( $path, 0777 );
+            $this->removeTempDir();
+            $this->fail( "Expected exception was not thrown." );
+        }
+        catch ( ezcBaseFilePermissionException $e )
+        {
+            $this->assertEquals( "The file '{$path}' can not be opened for 
reading.", $e->getMessage() );
+        }
+
+        chmod( $path, 0777 );
+        $this->removeTempDir();
+    }
+
+    /**
+     * Tests assigning an unwritable directory to a property.
+     *
+     * Expects that an ezcBaseFilePermissionException is raised by the 
unwritable
+     * path.
+     *
+     * This function creates a temporary directory and makes it unwritable.
+     *
+     * @param object $properties An object which implements properties access
+     * @param string $property The property of the $properties object to test
+     * @param string $value A directory name without paths or slashes
+     */
+    protected function unwritableDirTest( $properties, $property, $value )
+    {
+        $tempDir = $this->createTempDir( get_class( $this ) );
+        $path = $tempDir . DIRECTORY_SEPARATOR . $value;
+        mkdir( $path );
+        chmod( $path, 0444 );
+
+        try
+        {
+            $properties->$property = $path;
+            chmod( $path, 0777 );
+            $this->removeTempDir();
+            $this->fail( "Expected exception was not thrown." );
+        }
+        catch ( ezcBaseFilePermissionException $e )
+        {
+            $this->assertEquals( "The file '{$path}' can not be opened for 
writing.", $e->getMessage() );
+        }
+
+        chmod( $path, 0777 );
+        $this->removeTempDir();
+    }
+}
+?>

Propchange: trunk/Feed/tests/test.php
------------------------------------------------------------------------------
    svn:eol-style = native


-- 
svn-components mailing list
[email protected]
http://lists.ez.no/mailman/listinfo/svn-components

Reply via email to