Changeset 1029

Show
Ignore:
Timestamp:
11/12/07 14:49:11 (8 months ago)
Author:
mikey
Message:

implemented enhancement #96 for net.stubbles.ipo.request.filters.stubHTTPURLFilter

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/php/net/stubbles/ipo/request/filters/stubHTTPURLFilter.php

    r731 r1029  
    1919{ 
    2020    /** 
     21     * default error id if URL is not correct 
     22     */ 
     23    const URL_INCORRECT     = 'URL_INCORRECT'; 
     24    /** 
     25     * default error id if URL is not available 
     26     */ 
     27    const URL_NOT_AVAILABLE = 'URL_NOT_AVAILABLE'; 
     28    /** 
    2129     * switch whether DNS should be checked or not 
    2230     * 
    2331     * @var  bool 
    2432     */ 
    25     protected $checkDNS = false; 
     33    protected $checkDNS     = false; 
     34    /** 
     35     * list of error ids to be used by this filter 
     36     * 
     37     * @var  array<string,string> 
     38     */ 
     39    protected $errorIds     = array(self::URL_INCORRECT     => self::URL_INCORRECT, 
     40                                    self::URL_NOT_AVAILABLE => self::URL_NOT_AVAILABLE 
     41                              ); 
    2642 
    2743    /** 
     
    5672 
    5773    /** 
     74     * sets the error id to be used in case the filter finds an error in the value 
     75     * 
     76     * @param  string  $type     type of error, one of the stubHTTPURLFilter::URL_* constants 
     77     * @param  string  $errorId  the id of the error object to use 
     78     */ 
     79    public function setErrorId($type, $errorId) 
     80    { 
     81        if (isset($this->errorIds[$type]) === false) { 
     82            throw new stubIllegalArgumentException('Illegal type ' . $type . ' given. Allowed types are ' . join(', ', array_keys($this->errorIds)) . '.'); 
     83        } 
     84         
     85        $this->errorIds[$type] = $errorId; 
     86    } 
     87 
     88    /** 
    5889     * check if value is a valid HTTP URL 
    5990     * 
     
    6798            $http = stubHTTPURL::fromString($value); 
    6899        } catch (stubMalformedURLException $murle) { 
    69             throw new stubFilterException($this->rveFactory->create('URL_INCORRECT')); 
     100            throw new stubFilterException($this->rveFactory->create($this->errorIds[self::URL_INCORRECT])); 
    70101        } 
    71102         
    72103        if (null === $http) { 
    73104            if (true === $this->isRequired) { 
    74                 throw new stubFilterException($this->rveFactory->create('URL_INCORRECT')); 
     105                throw new stubFilterException($this->rveFactory->create($this->errorIds[self::URL_INCORRECT])); 
    75106            } 
    76107             
     
    79110         
    80111        if (true === $this->checkDNS && $http->checkDNS() == false) { 
    81             throw new stubFilterException($this->rveFactory->create('URL_NOT_AVAILABLE')); 
     112            throw new stubFilterException($this->rveFactory->create($this->errorIds[self::URL_NOT_AVAILABLE])); 
    82113        } 
    83114     
  • trunk/src/test/php/net/stubbles/ipo/request/filters/stubHTTPURLFilterTestCase.php

    r719 r1029  
    5858        $this->assertNull($this->httpURLFilter->execute(null)); 
    5959        $this->httpURLFilter->setRequired(true); 
    60         $this->mockStubRequestValueErrorFactory->expect('URL_INCORRECT'); 
     60        $this->mockStubRequestValueErrorFactory->expect('create', array('URL_INCORRECT')); 
     61        $this->expectException('stubFilterException'); 
     62        $this->httpURLFilter->execute(null); 
     63    } 
     64 
     65    /** 
     66     * assure correct behaviour when a null value is passed 
     67     */ 
     68    public function testNullValueWithDifferentErrorId() 
     69    { 
     70        $this->httpURLFilter->setErrorId(stubHTTPURLFilter::URL_INCORRECT, 'URL_INCORRECT_TEST'); 
     71        $this->httpURLFilter->setRequired(true); 
     72        $this->mockStubRequestValueErrorFactory->expect('create', array('URL_INCORRECT_TEST')); 
    6173        $this->expectException('stubFilterException'); 
    6274        $this->httpURLFilter->execute(null); 
     
    7183        $this->assertNull($this->httpURLFilter->execute('')); 
    7284        $this->httpURLFilter->setRequired(true); 
    73         $this->mockStubRequestValueErrorFactory->expect('URL_INCORRECT'); 
     85        $this->mockStubRequestValueErrorFactory->expect('create', array('URL_INCORRECT')); 
    7486        $this->expectException('stubFilterException'); 
    7587        $this->httpURLFilter->execute(''); 
     88    } 
     89 
     90    /** 
     91     * assure correct behaviour when a null value is passed 
     92     */ 
     93    public function testEmptyValueWithDifferentErrorId() 
     94    { 
     95        $this->httpURLFilter->setErrorId(stubHTTPURLFilter::URL_INCORRECT, 'URL_INCORRECT_TEST'); 
     96        $this->httpURLFilter->setRequired(true); 
     97        $this->mockStubRequestValueErrorFactory->expect('create', array('URL_INCORRECT_TEST')); 
     98        $this->expectException('stubFilterException'); 
     99        $this->httpURLFilter->execute(null); 
    76100    } 
    77101 
     
    81105    public function testWrongScheme() 
    82106    { 
    83         $this->mockStubRequestValueErrorFactory->expect('URL_INCORRECT'); 
     107        $this->mockStubRequestValueErrorFactory->expect('create', array('URL_INCORRECT')); 
    84108        $this->expectException('stubFilterException'); 
    85109        $this->httpURLFilter->execute('ftp://foobar.de/'); 
     
    91115    public function testWrongValue() 
    92116    { 
    93         $this->mockStubRequestValueErrorFactory->expect('URL_INCORRECT'); 
     117        $this->mockStubRequestValueErrorFactory->expect('create', array('URL_INCORRECT')); 
    94118        $this->expectException('stubFilterException'); 
    95119        $this->httpURLFilter->execute('http://wrong example!'); 
    96120    } 
     121 
     122    /** 
     123     * assure that setting a wrong error type causes an exception 
     124     */ 
     125    public function testSetErrorIdWithWrongType() 
     126    { 
     127        $this->expectException('stubIllegalArgumentException'); 
     128        $this->httpURLFilter->setErrorId('blub', 'dummy'); 
     129    } 
    97130} 
    98131?>