Changeset 169

Show
Ignore:
Timestamp:
01/29/07 00:13:51 (2 years ago)
Author:
mikey
Message:

added more stuff from rss specification available at http://rssboard.org/rss-specification

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/php/net/stubbles/xml/rss/stubRSSFeedGenerator.php

    r166 r169  
    1212/** 
    1313 * Class for generating a rss 2.0 feed. 
     14 *  
     15 * The implementation follows the rss specification available at 
     16 * http://rssboard.org/rss-specification. However some of the elements are  
     17 * not implemented: 
     18 * pubDate     
     19 * category   Why categorize a whole feed when the items can be categorized? 
     20 * cloud      This implies security and spamming dangers. 
     21 * rating 
     22 * textInput  Most aggregators ignore it. 
     23 * skipHours  Usage relies on behaviour of aggregators. 
     24 * skipDays   Usage relies on behaviour of aggregators. 
    1425 * 
    1526 * @package     stubbles 
    1627 * @subpackage  xml_rss 
     28 * @see         http://rssboard.org/rss-specification 
    1729 */ 
    1830class stubRSSFeedGenerator extends stubBaseObject 
    1931{ 
    2032    /** 
    21      * title of rss feed 
    22      * 
    23      * @var  string 
    24      */ 
    25     private $title       = ''; 
    26     /** 
    27      * source of rss feed 
    28      * 
    29      * @var  string 
    30      */ 
    31     private $link        = null
    32     /** 
    33      * source description 
    34      * 
    35      * @var  string 
    36      */ 
    37     private $description = null
     33     * name of the channel 
     34     * 
     35     * @var  string 
     36     */ 
     37    protected $title          = ''; 
     38    /** 
     39     * URL to the HTML website corresponding to the channel 
     40     * 
     41     * @var  string 
     42     */ 
     43    protected $link           = ''
     44    /** 
     45     * phrase or sentence describing the channel 
     46     * 
     47     * @var  string 
     48     */ 
     49    protected $description    = ''
    3850    /** 
    3951     * list of items in feed 
    4052     * 
    41      * @var  array<RssFeedItem> 
    42      */ 
    43     private $items       = array(); 
     53     * @var  array<stubRssFeedItem> 
     54     */ 
     55    protected $items          = array(); 
    4456    /** 
    4557     * the generator of this rss feed 
     
    4759     * @var  string 
    4860     */ 
    49     private $generator   = 'Stubbles RSSFeedGenerator'; 
     61    protected $generator      = 'Stubbles RSSFeedGenerator'; 
    5062    /** 
    5163     * list of stylesheets to append as processing instructions 
     
    5365     * @var  array<string> 
    5466     */ 
    55     private $stylesheets = array(); 
     67    protected $stylesheets    = array(); 
     68    /** 
     69     * the language the channel is written in 
     70     * 
     71     * @var  string 
     72     * @see  http://rssboard.org/rss-language-codes 
     73     */ 
     74    protected $language       = null; 
     75    /** 
     76     * copyright notice for content in the channel 
     77     * 
     78     * @var  string 
     79     */ 
     80    protected $copyright      = null; 
     81    /** 
     82     * email address for person responsible for editorial content 
     83     * 
     84     * @var  string 
     85     */ 
     86    protected $managingEditor = null; 
     87    /** 
     88     * email address for person responsible for technical issues relating to channel 
     89     * 
     90     * @var  string 
     91     */ 
     92    protected $webMaster      = null; 
     93    /** 
     94     * last time the content of the channel changed 
     95     * 
     96     * @var  string 
     97     */ 
     98    protected $lastBuildDate  = null; 
     99    /** 
     100     * A URL that points to the documentation for the format used in the RSS  
     101     * file. It's probably a pointer to this page. It's for people who might  
     102     * stumble across an RSS file on a Web server 25 years from now and wonder  
     103     * what it is. 
     104     * 
     105     * @var  string 
     106     */ 
     107    protected $docs           = 'http://rssboard.org/rss-specification'; 
     108    /** 
     109     * number of minutes that indicates how long a channel can be cached  
     110     * before refreshing from the source 
     111     * 
     112     * @var  int 
     113     */ 
     114    protected $ttl            = null; 
     115    /** 
     116     * specifies a GIF, JPEG or PNG image that can be displayed with the channel 
     117     * 
     118     * @var  array 
     119     */ 
     120    protected $image          = array('url'         => '', 
     121                                      'description' => '', 
     122                                      'width'       => 88, 
     123                                      'height'      => 31 
     124                                ); 
    56125 
    57126    /** 
     
    73142     * add an item to the feed 
    74143     * 
    75      * @param   string           $title  title of rss feed item 
     144     * @param   string           $title        title of the item 
     145     * @param   string           $link         URL of the item 
     146     * @param   string           $description  item synopsis 
    76147     * @return  stubRssFeedItem  the added item 
    77148     */ 
    78     public function addItem($title
    79     { 
    80         $rssFeedItem = stubRssFeedItem::create($title); 
     149    public function addItem($title, $link, $description
     150    { 
     151        $rssFeedItem = stubRssFeedItem::create($title, $link, $description); 
    81152        array_push($this->items, $rssFeedItem); 
    82153        return $rssFeedItem; 
     
    101172    { 
    102173        $this->stylesheets[] = $stylesheet; 
     174    } 
     175     
     176    /** 
     177     * set the language the channel is written in 
     178     * 
     179     * @param  string  $language 
     180     */ 
     181    public function setLanguage($language) 
     182    { 
     183        $this->language = $language; 
     184    } 
     185     
     186    /** 
     187     * set copyright notice for content in the channel 
     188     * 
     189     * @param  string  $copyright 
     190     */ 
     191    public function setCopyright($copyright) 
     192    { 
     193        $this->copyright = $copyright; 
     194    } 
     195     
     196    /** 
     197     * set email address for person responsible for editorial content 
     198     * 
     199     * @param  string  $managingEditor 
     200     */ 
     201    public function setManagingEditor($managingEditor) 
     202    { 
     203        if (strstr($managingEditor, '@') === false) { 
     204            $this->managingEditor = 'nospam@example.com (' . $managingEditor . ')'; 
     205        } else { 
     206            $this->managingEditor = $managingEditor; 
     207        } 
     208    } 
     209     
     210    /** 
     211     * set email address for person responsible for technical issues relating to channel 
     212     * 
     213     * @param  string  $webMaster 
     214     */ 
     215    public function setWebMaster($webMaster) 
     216    { 
     217        if (strstr($webMaster, '@') === false) { 
     218            $this->webMaster = 'nospam@example.com (' . $webMaster . ')'; 
     219        } else { 
     220            $this->webMaster = $webMaster; 
     221        } 
     222    } 
     223     
     224    /** 
     225     * set the last time when the content of the channel changed 
     226     * 
     227     * @param   string|int   $lastBuildDate  last time the content of the channel changed 
     228     */ 
     229    public function setLastBuildDate($lastBuildDate) 
     230    { 
     231        if (is_int($lastBuildDate) == false) { 
     232            $lastBuildDate = strtotime($lastBuildDate); 
     233            if (false === $lastBuildDate) { 
     234                throw new stubXMLException('Argument must be a unix timestamp or a valid string representation of a time.'); 
     235            } 
     236        } 
     237         
     238        $this->lastBuildDate = date('D d M Y H:i:s O', $lastBuildDate); 
     239    } 
     240     
     241    /** 
     242     * set number of minutes that indicates how long a channel can be cached  
     243     * before refreshing from the source 
     244     * 
     245     * @param  int  $ttl 
     246     */ 
     247    public function setTimeToLive($ttl) 
     248    { 
     249        $this->ttl = $ttl; 
     250    } 
     251     
     252    /** 
     253     * specify a GIF, JPEG or PNG image to be displayed with the channel 
     254     * 
     255     * @param   string  $url          URL of a GIF, JPEG or PNG image that represents the channel 
     256     * @param   string  $description  contains text that is included in the TITLE attribute of the link formed around the image in the HTML rendering 
     257     * @param   int     $width        indicating the width of the image in pixels, must be 0 < $width <= 144, default 88 
     258     * @param   int     $height       indicating the height of the image in pixels, must be 0 < $height <= 400, default 31 
     259     * @throws  stubXMLException  in case $width or $height have invalid values 
     260     */ 
     261    public function setImage($url, $description, $width = 88, $height = 31) 
     262    { 
     263        if (144 < $width || 0 > $width) { 
     264            throw new stubXMLException('Width must be a value between 0 and 144.'); 
     265        } 
     266         
     267        if (400 < $height || 0 > $height) { 
     268            throw new stubXMLException('Height must be a value between 0 and 400.'); 
     269        } 
     270         
     271        $this->image = array('url'         => $url, 
     272                             'description' => $description, 
     273                             'width'       => $width, 
     274                             'height'      => $height 
     275                       ); 
    103276    } 
    104277 
     
    124297        $xmlStreamWriter->writeElement('description', array(), $this->description); 
    125298        $xmlStreamWriter->writeElement('generator', array(), $this->generator); 
     299         
     300        if (null !== $this->language) { 
     301            $xmlStreamWriter->writeElement('language', array(), $this->language); 
     302        } 
     303         
     304        if (null !== $this->copyright) { 
     305            $xmlStreamWriter->writeElement('copyright', array(), $this->copyright); 
     306        } 
     307         
     308        if (null !== $this->managingEditor) { 
     309            $xmlStreamWriter->writeElement('managingEditor', array(), $this->managingEditor); 
     310        } 
     311         
     312        if (null !== $this->webMaster) { 
     313            $xmlStreamWriter->writeElement('webMaster', array(), $this->webMaster); 
     314        } 
     315         
     316        if (null !== $this->lastBuildDate) { 
     317            $xmlStreamWriter->writeElement('lastBuildDate', array(), $this->lastBuildDate); 
     318        } 
     319         
     320        if (null !== $this->ttl) { 
     321            $xmlStreamWriter->writeElement('ttl', array(), $this->ttl); 
     322        } 
     323         
     324        if (strlen($this->image['url']) > 0) { 
     325            $xmlStreamWriter->writeStartElement('image'); 
     326            $xmlStreamWriter->writeElement('url', array(), $this->image['url']); 
     327            $xmlStreamWriter->writeElement('title', array(), $this->title); 
     328            $xmlStreamWriter->writeElement('link', array(), $this->link); 
     329            $xmlStreamWriter->writeElement('width', array(), $this->image['width']); 
     330            $xmlStreamWriter->writeElement('height', array(), $this->image['height']); 
     331            $xmlStreamWriter->writeElement('description', array(), $this->image['description']); 
     332            $xmlStreamWriter->writeEndElement(); 
     333        } 
     334         
    126335        $xmlStreamWriter->writeEndElement(); // end channel element 
    127336        foreach ($this->items as $item) { 
  • trunk/src/main/php/net/stubbles/xml/rss/stubRSSFeedItem.php

    r166 r169  
    1313 * @package     stubbles 
    1414 * @subpackage  xml_rss 
     15 * @see         http://rssboard.org/rss-specification 
    1516 */ 
    1617class stubRSSFeedItem extends stubBaseObject 
    1718{ 
    1819    /** 
    19      * id of item 
    20      * 
    21      * @var  string 
    22      */ 
    23     private $id      = null; 
    24     /** 
    25      * title of rss feed item 
    26      * 
    27      * @var  string 
    28      */ 
    29     private $title   = ''; 
    30     /** 
    31      * source of rss feed item 
    32      * 
    33      * @var  string 
    34      */ 
    35     private $link    = null; 
    36     /** 
    37      * author of rss feed item 
    38      * 
    39      * @var  string 
    40      */ 
    41     private $author  = 'nospam@example.com (unknown)'; 
     20     * title of the item 
     21     * 
     22     * @var  string 
     23     */ 
     24    protected $title       = ''; 
     25    /** 
     26     * URL of the item 
     27     * 
     28     * @var  string 
     29     */ 
     30    protected $link        = ''; 
     31    /** 
     32     * item synopsis 
     33     * 
     34     * @var  string 
     35     */ 
     36    protected $description = ''; 
     37    /** 
     38     * email address of the author of the item 
     39     * 
     40     * @var  string 
     41     */ 
     42    protected $author      = null; 
     43    /** 
     44     * categories where the item is included 
     45     * 
     46     * @var  array 
     47     */ 
     48    protected $categories  = array(); 
     49    /** 
     50     * URL of a page for comments relating to the item 
     51     * 
     52     * @var  string 
     53     */ 
     54    protected $comments    = null; 
     55    /** 
     56     * describes a media object that is attached to the item 
     57     * 
     58     * @var  array 
     59     */ 
     60    protected $enclosures  = array(); 
     61    /** 
     62     * unique identifier for the item 
     63     * 
     64     * @var  string 
     65     */ 
     66    protected $guid        = null; 
     67    /** 
     68     * whether the id may be interpreted as a permanent link or not 
     69     * 
     70     * @var  bool 
     71     */ 
     72    protected $isPermaLink = true; 
     73    /** 
     74     * indicates when the item was published 
     75     * 
     76     * @var  string 
     77     */ 
     78    protected $pubDate     = null; 
     79    /** 
     80     * where that the item came from 
     81     * 
     82     * @var  array 
     83     */ 
     84    protected $sources     = array(); 
    4285    /** 
    4386     * content of rss feed item 
     
    4588     * @var  string 
    4689     */ 
    47     private $content = ''; 
    48     /** 
    49      * publishing date of rss feed item 
    50      * 
    51      * @var  string 
    52      */ 
    53     private $pubDate = null; 
     90    protected $content     = null; 
    5491 
    5592    /** 
    5693     * constructor 
    5794     * 
    58      * @param  string  $title  title of rss feed item 
    59      */ 
    60     private function __construct($title) 
    61     { 
    62         $this->title = $title; 
    63     } 
    64  
    65     /** 
    66      * create a new RssFeedItem 
    67      * 
    68      * @param   string           $title  title of rss feed item 
    69      * @return  stubRSSFeedItem 
    70      */ 
    71     public static function create($title) 
    72     { 
    73         $item = new self($title); 
     95     * @param  string  $title        title of the item 
     96     * @param  string  $link         URL of the item 
     97     * @param  string  $description  item synopsis 
     98     */ 
     99    private function __construct($title, $link, $description) 
     100    { 
     101        $this->title       = $title; 
     102        $this->link        = $link; 
     103        $this->description = $description; 
     104    } 
     105 
     106    /** 
     107     * create a new stubRssFeedItem 
     108     * 
     109     * @param   string           $title        title of the item 
     110     * @param   string           $link         URL of the item 
     111     * @param   string           $description  item synopsis 
     112     * @return  stubRSSFeedItem 
     113     */ 
     114    public static function create($title, $link, $description) 
     115    { 
     116        $item = new self($title, $link, $description); 
    74117        return $item; 
    75118    } 
    76119 
    77120    /** 
    78      * set id of rss feed item 
    79      * 
    80      * @param   string           $id  the id of the item 
    81      * @return  stubRSSFeedItem 
    82      */ 
    83     public function withId($id) 
    84     { 
    85         $this->id = $id; 
    86         return $this; 
    87     } 
    88  
    89     /** 
    90      * set the link for the rss feed item 
    91      * 
    92      * @param   string           $link  source of rss feed item 
    93      * @return  stubRSSFeedItem 
    94      */ 
    95     public function fromLink($link) 
    96     { 
    97         $this->link = $link; 
    98         return $this; 
    99     } 
    100  
    101     /** 
    102      * set the author who created the item 
     121     * set the email address of the author of the item who created the item 
    103122     * 
    104123     * @param   string           $author  author of rss feed item 
     
    115134        return $this; 
    116135    } 
    117  
    118     /** 
    119      * set the content of the item 
    120      * 
    121      * @param   string           $content  content of rss feed item 
    122      * @return  stubRSSFeedItem 
    123      */ 
    124     public function withContent($content) 
    125     { 
    126         $this->content = $content; 
     136     
     137    /** 
     138     * set one or more categories where the item is included into 
     139     * 
     140     * @param   string  $category  category where the item is included 
     141     * @param   string  $domain    optional  categorization taxonomy 
     142     * @return  stubRSSFeedItem 
     143     */ 
     144    public function inCategory($category, $domain = '') 
     145    { 
     146        $this->categories[] = array('category' => $category, 
     147                                    'domain'   => $domain 
     148                              ); 
     149        return $this; 
     150    } 
     151     
     152    /** 
     153     * set the URL of a page for comments relating to the item 
     154     * 
     155     * @param   string  $comments 
     156     * @return  stubRSSFeedItem 
     157     */ 
     158    public function addCommentsAt($comments) 
     159    { 
     160        $this->comments = $comments; 
     161        return $this; 
     162    } 
     163     
     164    /** 
     165     * add an enclosure to the item 
     166     * 
     167     * @param   string           $url     location of enclosure 
     168     * @param   int              $length  length of enclosure in bytes 
     169     * @param   string           $type    MIME type of enclosure 
     170     * @return  stubRSSFeedItem 
     171     */ 
     172    public function deliveringEnclosure($url, $length, $type) 
     173    { 
     174        $this->enclosures[] = array('url'    => $url, 
     175                                    'length' => $length, 
     176                                    'type'   => $type 
     177                              ); 
     178        return $this; 
     179    } 
     180 
     181    /** 
     182     * set id of rss feed item 
     183     * 
     184     * @param   string           $guid         the id of the item 
     185     * @param   bool             $isPermaLink  optional 
     186     * @return  stubRSSFeedItem 
     187     */ 
     188    public function withGuid($guid, $isPermaLink = true) 
     189    { 
     190        $this->guid        = $guid; 
     191        $this->isPermaLink = $isPermaLink; 
    127192        return $this; 
    128193    } 
     
    146211        return $this; 
    147212    } 
    148  
    149     /** 
    150      * return the id of the item 
    151      * 
    152      * @return  string 
    153      */ 
    154     public function getId() 
    155     { 
    156         return $this->id; 
    157     } 
    158  
    159     /** 
    160      * return the title of the item 
     213     
     214    /** 
     215     * set the source where that the item came from 
     216     * 
     217     * @param   string           $name  name of the source 
     218     * @param   string           $url   url of the source 
     219     * @return  stubRSSFeedItem 
     220     */ 
     221    public function inspiredBySource($name, $url) 
     222    { 
     223        $this->sources[] = array('name' => $name, 'url' => $url); 
     224        return $this; 
     225    } 
     226 
     227    /** 
     228     * set the content of the item 
     229     * 
     230     * @param   string           $content  content of rss feed item 
     231     * @return  stubRSSFeedItem 
     232     */ 
     233    public function withContent($content) 
     234    { 
     235        $this->content = $content; 
     236        return $this; 
     237    } 
     238 
     239    /** 
     240     * returns the title of the item 
    161241     * 
    162242     * @return  string 
     
    168248 
    169249    /** 
    170      * return the source of the item 
     250     * returns the URL of the item 
    171251     * 
    172252     * @return  string 
     
    176256        return $this->link; 
    177257    } 
    178  
    179     /** 
    180      * return the author of the item 
     258     
     259    /** 
     260     * returns the item synopsis 
     261     * 
     262     * @return  string 
     263     */ 
     264    public function getDescription() 
     265    { 
     266        return $this->description; 
     267    } 
     268 
     269    /** 
     270     * returns the email address of the author of the item 
    181271     * 
    182272     * @return  string 
     
    186276        return $this->author; 
    187277    } 
    188  
     278     
     279    /** 
     280     * returns one or more categories where the item is included into 
     281     * 
     282     * @return  array 
     283     */ 
     284    public function getCategories() 
     285    { 
     286        return $this->categories; 
     287    } 
     288     
     289    /** 
     290     * returns the URL of a page for comments relating to the item 
     291     * 
     292     * @return  string 
     293     */ 
     294    public function getComments() 
     295    { 
     296        return $this->comments; 
     297    } 
     298     
     299    /** 
     300     * returns the description of a media object that is attached to the item 
     301     * 
     302     * @return  array 
     303     */ 
     304    public function getEnclosures() 
     305    { 
     306        return $this->enclosures; 
     307    } 
     308 
     309    /** 
     310     * returns the unique identifier for the item 
     311     * 
     312     * @return  string 
     313     */ 
     314    public function getGuid() 
     315    { 
     316        return $this->guid; 
     317    } 
     318     
     319    /** 
     320     * checks whether the guid represents a perma link or not 
     321     * 
     322     * @return  bool 
     323     */ 
     324    public function isGuidPermaLink() 
     325    { 
     326        return $this->isPermaLink; 
     327    } 
     328 
     329    /** 
     330     * return the publishing date of the item 
     331     * 
     332     * @return  string 
     333     */ 
     334    public function getPubDate() 
     335    { 
     336        return $this->pubDate; 
     337    } 
     338     
     339    /** 
     340     * returns where that the item came from 
     341     * 
     342     * @return  array 
     343     */ 
     344    public function getSources() 
     345    { 
     346        return $this->sources; 
     347    } 
     348     
    189349    /** 
    190350     * return the content of the item 
     
    198358 
    199359    /** 
    200      * return the publishing date of the item 
    201      * 
    202      * @return  string 
    203      */ 
    204     public function getPubDate() 
    205     { 
    206         return $this->pubDate; 
    207     } 
    208  
    209     /** 
    210360     * serialize the item to xml 
    211361     * 
     
    215365    { 
    216366        $xmlStreamWriter->writeStartElement('item'); 
    217         $xmlStreamWriter->writeElement('title', array(), $this->getTitle()); 
    218         $xmlStreamWriter->writeElement('author', array(), $this->getAuthor()); 
    219         $xmlStreamWriter->writeElement('content:encoded', array(), htmlspecialchars($this->getContent())); 
     367        $xmlStreamWriter->writeElement('title', array(), $this->title); 
     368        $xmlStreamWriter->writeElement('link', array(), $this->link); 
     369        $xmlStreamWriter->writeElement('description', array(), $this->description); 
     370         
     371        if (null !== $this->author) { 
     372            $xmlStreamWriter->writeElement('author', array(), $this->author); 
     373        } 
     374         
     375        foreach ($this->categories as $category) { 
     376            $attributes = array(); 
     377            if (strlen($category['domain']) > 0) { 
     378                $attributes['domain'] = $category['domain']; 
     379            } 
     380            $xmlStreamWriter->writeElement('category', $attributes, $category['category']); 
     381        } 
     382         
     383        if (null !== $this->comments) { 
     384            $xmlStreamWriter->writeElement('comments', array(), $this->comments); 
     385        } 
     386         
     387        foreach ($this->enclosures as $enclosure) { 
     388            $xmlStreamWriter->writeElement('enclosure', array('url'    => $enclosure['url'], 
     389                                                              'length' => $enclosure['length'], 
     390                                                              'type'   => $enclosure['type'] 
     391                                                        ) 
     392            ); 
     393        } 
     394         
     395        if (null !== $this->guid) { 
     396            $xmlStreamWriter->writeElement('guid', array('isPermaLink' => ((true == $this->isPermaLink) ? ('true') : ('false'))), $this->guid); 
     397        } 
     398         
    220399        if (null !== $this->pubDate) { 
    221             $xmlStreamWriter->writeElement('pubDate', array(), $this->getPubDate()); 
    222         } 
    223          
    224         if (null !== $this->id) { 
    225             $xmlStreamWriter->writeElement('guid', array('isPermaLink' => 'false'), $this->getId()); 
    226         } 
    227          
    228         if (null !== $this->link) { 
    229             $xmlStreamWriter->writeElement('link', array(), $this->getLink()); 
    230         } 
     400            $xmlStreamWriter->writeElement('pubDate', array(), $this->pubDate); 
     401        } 
     402         
     403        foreach ($this->sources as $source) { 
     404            $xmlStreamWriter->writeElement('source', array('url' => $source['url']), $source['name']); 
     405        } 
     406 
     407        $xmlStreamWriter->writeElement('content:encoded', array(), htmlspecialchars($this->content)); 
    231408         
    232409        $xmlStreamWriter->writeEndElement(); // end item 
  • trunk/src/test/php/net/stubbles/xml/rss/stubRSSFeedGeneratorTestCase.php

    r164 r169  
    6767    { 
    6868        $this->assertEqual($this->rssFeedGenerator->countItems(), 0); 
    69         $this->rssFeedGenerator->addItem('foo'); 
     69        $this->rssFeedGenerator->addItem('foo', 'bar', 'baz'); 
    7070        $this->assertEqual($this->rssFeedGenerator->countItems(), 1); 
    7171        $mockXmlStreamWriter = new MockstubXMLStreamWriter(); 
     
    7676        $this->rssFeedGenerator->serialize($mockXmlStreamWriter); 
    7777    } 
     78     
     79    /** 
     80     * test that optional channel elements are handled as expected 
     81     */ 
     82    public function testWithAllChannelElements() 
     83    { 
     84        $this->rssFeedGenerator->setLanguage('en_EN'); 
     85        $this->rssFeedGenerator->setCopyright('© 2007 Stubbles Development Team'); 
     86        $this->rssFeedGenerator->setManagingEditor('mikey'); 
     87        $this->rssFeedGenerator->setWebMaster('schst'); 
     88        $this->rssFeedGenerator->setLastBuildDate(50); 
     89        $this->rssFeedGenerator->setTimeToLive(60); 
     90        $this->rssFeedGenerator->setImage('http://example.org/example.gif', 'foo'); 
     91         
     92        $mockXmlStreamWriter = new MockstubXMLStreamWriter(); 
     93        $mockXmlStreamWriter->expectNever('writeProcessingInstruction'); 
     94        $mockXmlStreamWriter->expectCallCount('writeStartElement', 3); 
     95        $mockXmlStreamWriter->expectCallCount('writeEndElement', 3); 
     96        $mockXmlStreamWriter->expectCallCount('writeElement', 16); 
     97        $this->rssFeedGenerator->serialize($mockXmlStreamWriter); 
     98    } 
     99     
     100    /** 
     101     * assure that invalid dates throw a stubXMLException 
     102     */ 
     103    public function testInvalidLastBuildDate() 
     104    { 
     105        $this->expectException('stubXMLException'); 
     106        $this->rssFeedGenerator->setLastBuildDate('foo'); 
     107    } 
     108     
     109    /** 
     110     * assure that an invalid width throws a stubXMLException 
     111     */ 
     112    public function testImageWidthTooSmall() 
     113    { 
     114        $this->expectException('stubXMLException'); 
     115        $this->rssFeedGenerator->setImage('http://example.org/example.gif', 'foo', -1); 
     116    } 
     117     
     118    /** 
     119     * assure that an invalid width throws a stubXMLException 
     120     */ 
     121    public function testImageWidthTooGreat() 
     122    { 
     123        $this->expectException('stubXMLException'); 
     124        $this->rssFeedGenerator->setImage('http://example.org/example.gif', 'foo', 145); 
     125    } 
     126     
     127    /** 
     128     * assure that an invalid height throws a stubXMLException 
     129     */ 
     130    public function testImageHeightTooSmall() 
     131    { 
     132        $this->expectException('stubXMLException'); 
     133        $this->rssFeedGenerator->setImage('http://example.org/example.gif', 'foo', -1); 
     134    } 
     135     
     136    /** 
     137     * assure that an invalid height throws a stubXMLException 
     138     */ 
     139    public function testImageHeightTooGreat() 
     140    { 
     141        $this->expectException('stubXMLException'); 
     142        $this->rssFeedGenerator->setImage('http://example.org/example.gif', 'foo', 401); 
     143    } 
    78144} 
    79145?> 
  • trunk/src/test/php/net/stubbles/xml/rss/stubRSSFeedItemTestCase.php

    r164 r169  
    3535    public function setUp() 
    3636    { 
    37         $this->rssFeedItem1 = stubRSSFeedItem::create('test1'
     37        $this->rssFeedItem1 = stubRSSFeedItem::create('test1', 'http://stubbles.net/', 'description'
    3838                                             ->byAuthor('mikey') 
    39                                              ->fromLink('http://stubbles.net/') 
     39                                             ->inCategory('cat1') 
     40                                             ->inCategory('cat2', 'domain') 
     41                                             ->addCommentsAt('http://stubbles.net/comments/') 
     42                                             ->deliveringEnclosure('http://stubbles.net/enclosure.mp3', 50, 'audio/mpeg') 
     43                                             ->withGuid('dummy') 
    4044                                             ->publishedOn(50) 
     45                                             ->inspiredBySource('stubbles', 'http://stubbles.net/source/') 
    4146                                             ->withContent('<foo>bar</foo><baz/>') 
    42                                              ->withId('dummy') 
    43                               ; 
    44         $this->rssFeedItem2 = stubRSSFeedItem::create('test2'); 
     47                               ; 
     48        $this->rssFeedItem2 = stubRSSFeedItem::create('test2', 'http://stubbles.net/', 'description2'); 
    4549    } 
    4650 
     
    5054    public function testValues() 
    5155    { 
    52         $this->assertEqual($this->rssFeedItem1->getId(), 'dummy'); 
    5356        $this->assertEqual($this->rssFeedItem1->getTitle(), 'test1'); 
    5457        $this->assertEqual($this->rssFeedItem1->getLink(), 'http://stubbles.net/'); 
     58        $this->assertEqual($this->rssFeedItem1->getDescription(), 'description'); 
    5559        $this->assertEqual($this->rssFeedItem1->getAuthor(), 'nospam@example.com (mikey)'); 
    56         $this->assertEqual($this->rssFeedItem1->getContent(), '<foo>bar</foo><baz/>'); 
     60        $this->assertEqual($this->rssFeedItem1->getCategories(), array(array('category' => 'cat1', 
     61                                                                             'domain'   => '' 
     62                                                                       ), 
     63                                                                       array('category' => 'cat2', 
     64                                                                             'domain'   => 'domain' 
     65                                                                       ) 
     66                                                                 ) 
     67        ); 
     68        $this->assertEqual($this->rssFeedItem1->getComments(), 'http://stubbles.net/comments/'); 
     69        $this->assertEqual($this->rssFeedItem1->getEnclosures(), array(array('url'    => 'http://stubbles.net/enclosure.mp3', 
     70                                                                             'length' => 50, 
     71                                                                             'type' => 'audio/mpeg' 
     72                                                                       ) 
     73                                                                 ) 
     74        ); 
     75        $this->assertEqual($this->rssFeedItem1->getGuid(), 'dummy'); 
     76        $this->assertTrue($this->rssFeedItem1->isGuidPermaLink()); 
    5777        $this->assertEqual($this->rssFeedItem1->getPubDate(), 'Thu 01 Jan 1970 01:00:50 +0100'); 
     78        $this->assertEqual($this->rssFeedItem1->getSources(), array(array('name' => 'stubbles', 
     79                                                                          'url'  => 'http://stubbles.net/source/' 
     80                                                                    ) 
     81                                                              ) 
     82        ); 
    5883         
    5984        $this->rssFeedItem1->byAuthor('test@example.net (mikey)'); 
    6085        $this->assertEqual($this->rssFeedItem1->getAuthor(), 'test@example.net (mikey)'); 
     86         
     87        $this->rssFeedItem1->withGuid('dummy2', false); 
     88        $this->assertEqual($this->rssFeedItem1->getGuid(), 'dummy2'); 
     89        $this->assertFalse($this->rssFeedItem1->isGuidPermaLink()); 
    6190    } 
    6291     
     
    6695    public function testEmptyValues() 
    6796    { 
    68         $this->assertNull($this->rssFeedItem2->getId()); 
    6997        $this->assertEqual($this->rssFeedItem2->getTitle(), 'test2'); 
    70         $this->assertNull($this->rssFeedItem2->getLink()); 
    71         $this->assertEqual($this->rssFeedItem2->getAuthor(), 'nospam@example.com (unknown)'); 
     98        $this->assertEqual($this->rssFeedItem2->getLink(), 'http://stubbles.net/'); 
     99        $this->assertEqual($this->rssFeedItem2->getDescription(), 'description2'); 
     100        $this->assertNull($this->rssFeedItem2->getAuthor()); 
     101        $this->assertEqual($this->rssFeedItem2->getCategories(), array()); 
     102        $this->assertNull($this->rssFeedItem2->getComments()); 
     103        $this->assertEqual($this->rssFeedItem2->getEnclosures(), array()); 
     104        $this->assertNull($this->rssFeedItem2->getGuid()); 
     105        $this->assertNull($this->rssFeedItem2->getPubDate()); 
     106        $this->assertEqual($this->rssFeedItem2->getSources(), array()); 
    72107        $this->assertEqual($this->rssFeedItem2->getContent(), ''); 
    73         $this->assertNull($this->rssFeedItem2->getPubDate()); 
    74108    } 
    75109     
     
    81115        $mockXmlStreamWriter = new MockstubXMLStreamWriter(); 
    82116        $mockXmlStreamWriter->expectOnce('writeStartElement', array('item')); 
    83         $mockXmlStreamWriter->expectCallCount('writeElement', 6); 
    84         /* 
    85         The following stuff could be done with argument depending mock expectations. 
    86         See https://sourceforge.net/tracker/index.php?func=detail&aid=1646450&group_id=76550&atid=547458 
    87         $mockXmlStreamWriter->expectOnce('writeElement', array('title', array(), 'test1')); 
    88         $mockXmlStreamWriter->expectOnce('writeElement', array('author', array(), 'nospam@example.com (mikey)')); 
    89         $mockXmlStreamWriter->expectOnce('writeElement', array('content:encoded', array(), '&lt;foo&gt;bar&lt;/foo&gt;&lt;baz/&gt;')); 
    90         $mockXmlStreamWriter->expectOnce('writeElement', array('pubDate', array(), 'Thu 01 Jan 1970 01:00:50 +0100')); 
    91         $mockXmlStreamWriter->expectOnce('writeElement', array('guid', array('isPermaLink' => 'false'), 'dummy')); 
    92         $mockXmlStreamWriter->expectOnce('writeElement', array('link', array(), 'http://stubbles.net/')); 
    93         */ 
     117        $mockXmlStreamWriter->expectCallCount('writeElement', 12); 
    94118        $this->rssFeedItem1->serialize($mockXmlStreamWriter); 
    95119    } 
     
    102126        $mockXmlStreamWriter = new MockstubXMLStreamWriter(); 
    103127        $mockXmlStreamWriter->expectOnce('writeStartElement', array('item')); 
    104         $mockXmlStreamWriter->expectCallCount('writeElement', 3); 
    105         /* 
    106         The following stuff could be done with argument depending mock expectations. 
    107         See https://sourceforge.net/tracker/index.php?func=detail&aid=1646450&group_id=76550&atid=547458 
    108         $mockXmlStreamWriter->expectOnce('writeElement', array('title', array(), 'test1')); 
    109         $mockXmlStreamWriter->expectOnce('writeElement', array('author', array(), 'nospam@example.com (mikey)')); 
    110         $mockXmlStreamWriter->expectOnce('writeElement', array('content:encoded', array(), '&lt;foo&gt;bar&lt;/foo&gt;&lt;baz/&gt;')); 
    111         */ 
     128        $mockXmlStreamWriter->expectCallCount('writeElement', 4); 
    112129        $this->rssFeedItem2->serialize($mockXmlStreamWriter); 
    113130    }