Changeset 1274

Show
Ignore:
Timestamp:
01/22/08 12:04:12 (1 year ago)
Author:
mikey
Message:

continued refactoring #118: transformed tests for net::stubbles::websites::ipo::request::filters

Files:

Legend:

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

    r1223 r1274  
    2525{ 
    2626    /** 
     27     * registry key under which the amount of decimals is stored 
     28     */ 
     29    const DECIMALS_REGISTRY_KEY = 'net.stubbles.number.decimals'; 
     30 
     31    /** 
    2732     * constructor 
    2833     * 
     
    4954        } 
    5055         
    51         $value = parent::doExecute($value); 
    52         return (int) ($value * pow(10, stubRegistry::getConfig('net.stubbles.number.decimals'))); 
     56        $value    = parent::doExecute($value); 
     57        $decimals = stubRegistry::getConfig(self::DECIMALS_REGISTRY_KEY); 
     58        if (null == $decimals) { 
     59            return $value; 
     60        } 
     61         
     62        return (int) ($value * pow(10, $decimals)); 
    5363    } 
    5464} 
  • trunk/src/main/php/net/stubbles/ipo/request/filters/stubRegexFilterDecorator.php

    r1223 r1274  
    88 * @subpackage  ipo_request_filters 
    99 */ 
    10 stubClassLoader::load('net::stubbles::ipo::request::filters::stubAbstractFilterDecorator'); 
     10stubClassLoader::load('net::stubbles::ipo::request::filters::stubAbstractFilterDecorator', 
     11                      'net::stubbles::lang::exceptions::stubIllegalArgumentException', 
     12                      'net::stubbles::lang::exceptions::stubIllegalStateException' 
     13); 
    1114/** 
    1215 * Decorator which adds regex filtering (input value may be changed due to regex) 
     
    2629     */ 
    2730    const STRATEGY_BEFORE = -1; 
    28  
    2931    /** 
    3032     * indicates the decorator behavior: 
     
    3537     */ 
    3638    const STRATEGY_AFTER  = 1; 
    37  
    3839    /** 
    3940     * Decorator Strategy: 
     
    4647     */ 
    4748    protected $strategy; 
    48  
    4949    /** 
    5050     * regex 
     
    5757     * constructor 
    5858     * 
    59      * @param  stubFilter  $filter      decorated filter 
    60      * @param  string      $regex       regex to apply 
    61      * @param  int         $strategy    decrorator strategy 
     59     * @param   stubFilter  $filter      decorated filter 
     60     * @param   string      $regex       regex to apply 
     61     * @param   int         $strategy    decrorator strategy 
     62     * @throws  stubIllegalArgumentException 
    6263     */ 
    6364    function __construct(stubFilter $filter, $regex, $strategy) 
    6465    { 
     66        if (in_array($strategy, array(self::STRATEGY_BEFORE, self::STRATEGY_AFTER)) === false) { 
     67            throw new stubIllegalArgumentException('Invalid strategy type ' . $strategy); 
     68        } 
     69         
    6570        $this->setDecoratedFilter($filter); 
    6671        $this->strategy = $strategy; 
     
    7378     * @param   string                         $value 
    7479     * @throws  stubRuntimeException 
    75      * @throws  stubIllegalArgumentException 
     80     * @throws  stubIllegalStateException 
    7681     */ 
    7782    public function execute($value) 
    7883    { 
    79         if ($value != null) {    
     84        if ($value != null) { 
     85            $result = null; 
    8086            switch ($this->strategy) { 
    8187                case self::STRATEGY_BEFORE: 
    82                     $matchAmount = @preg_match($this->regex, $value, $matches); 
    83                     if ($matchAmount === false) { 
     88                    if (@preg_match($this->regex, $value, $matches) === false) { 
    8489                        throw new stubRuntimeException('Invalid regular expression ' . $this->regex); 
    85                     } else
    86                         $result = ($matchAmount !== 0)? $this->getDecoratedFilter()->execute($matches[0]): null
     90                    } elseif (isset($matches[0]) === true)
     91                        $result = $this->getDecoratedFilter()->execute($matches[0])
    8792                    } 
    8893                    break; 
     
    9095                case self::STRATEGY_AFTER: 
    9196                    $filtered = $this->getDecoratedFilter()->execute($value); 
    92                     $matchAmount = @preg_match($this->regex, $filtered, $matches); 
    93                     if ($matchAmount === false) { 
     97                    if (@preg_match($this->regex, $filtered, $matches) === false) { 
    9498                        throw new stubRuntimeException('Invalid regular expression ' . $this->regex); 
    95                     } else
     99                    } elseif (isset($matches[0]) === true)
    96100                        $result = $matches[0]; 
    97101                    } 
     
    99103 
    100104                default: 
    101                     throw new stubIllegalArgumentException('Invalid strategy type' . $this->strategy); 
    102                     break; 
     105                    throw new stubIllegalStateException('Invalid strategy type ' . $this->strategy); 
    103106            } 
     107             
    104108            return $result; 
    105109        } 
     110         
    106111        return $value; 
    107112    } 
  • trunk/src/test/php/net/stubbles/ipo/IPOTestSuite.php

    r1273 r1274  
    4141        #$suite->addTestFile($dir . '/request/broker/annotations/stubTextFilterAnnotationTestCase.php'); 
    4242 
    43         #$suite->addTestFile($dir . '/request/filters/stubFloatFilterTestCase.php'); 
    44         #$suite->addTestFile($dir . '/request/filters/stubHTTPURLFilterTestCase.php'); 
    45         #$suite->addTestFile($dir . '/request/filters/stubIntegerFilterTestCase.php'); 
    46         #$suite->addTestFile($dir . '/request/filters/stubMailFilterTestCase.php'); 
    47         #$suite->addTestFile($dir . '/request/filters/stubMD5FilterTestCase.php'); 
    48         #$suite->addTestFile($dir . '/request/filters/stubPassThruFilterTestCase.php'); 
    49         #$suite->addTestFile($dir . '/request/filters/stubPasswordFilterTestCase.php'); 
    50         #$suite->addTestFile($dir . '/request/filters/stubRegexFilterDecoratorTestCase.php'); 
    51         #$suite->addTestFile($dir . '/request/filters/stubStringFilterTestCase.php'); 
    52         #$suite->addTestFile($dir . '/request/filters/stubTextFilterTestCase.php'); 
     43        $suite->addTestFile($dir . '/request/filters/stubFloatFilterTestCase.php'); 
     44        $suite->addTestFile($dir . '/request/filters/stubHTTPURLFilterTestCase.php'); 
     45        $suite->addTestFile($dir . '/request/filters/stubIntegerFilterTestCase.php'); 
     46        $suite->addTestFile($dir . '/request/filters/stubMailFilterTestCase.php'); 
     47        $suite->addTestFile($dir . '/request/filters/stubMD5FilterTestCase.php'); 
     48        $suite->addTestFile($dir . '/request/filters/stubPassThruFilterTestCase.php'); 
     49        $suite->addTestFile($dir . '/request/filters/stubPasswordFilterTestCase.php'); 
     50        $suite->addTestFile($dir . '/request/filters/stubRegexFilterDecoratorTestCase.php'); 
     51        $suite->addTestFile($dir . '/request/filters/stubStringFilterTestCase.php'); 
     52        $suite->addTestFile($dir . '/request/filters/stubTextFilterTestCase.php'); 
    5353 
    5454        $suite->addTestFile($dir . '/response/stubCookieTestCase.php'); 
  • trunk/src/test/php/net/stubbles/ipo/request/filters/stubFloatFilterTestCase.php

    r1223 r1274  
    11<?php 
    22/** 
    3  * Tests for net::stubbles::ipo::request::filters::stubDoubleFilter. 
     3 * Tests for net::stubbles::ipo::request::filters::stubFloatFilter. 
    44 * 
    5  * @author      Frank Kleine <frank@kl-s.com
     5 * @author      Frank Kleine <mikey@stubbles.net
    66 * @package     stubbles 
    77 * @subpackage  ipo_request_filters_test 
    88 */ 
    99stubClassLoader::load('net::stubbles::ipo::request::filters::stubFloatFilter'); 
    10 Mock::generate('stubRequestValueErrorFactory'); 
    11 Mock::generate('stubValidator'); 
    1210/** 
    13  * Tests for net::stubbles::ipo::request::filters::stubDoubleFilter. 
     11 * Tests for net::stubbles::ipo::request::filters::stubFloatFilter. 
    1412 * 
    1513 * @package     stubbles 
    1614 * @subpackage  ipo_request_filters_test 
    1715 */ 
    18 class stubFloatFilterTestCase extends UnitTestCase 
     16class stubFloatFilterTestCase extends PHPUnit_Framework_TestCase 
    1917{ 
    2018    /** 
    2119     * a mock to be used for the rveFactory 
    2220     * 
    23      * @var  stubRequestValueErrorFactory 
    24      */ 
    25     protected $mockStubRequestValueErrorFactory; 
     21     * @var  PHPUnit_Framework_MockObject_MockObject 
     22     */ 
     23    protected $mockRequestValueErrorFactory; 
    2624    /** 
    2725     * a mock to be used for the minimum validator 
    2826     * 
    29      * @var  stubValidator 
    30      */ 
    31     protected $mockStubValidatorMin; 
     27     * @var  PHPUnit_Framework_MockObject_MockObject 
     28     */ 
     29    protected $mockValidatorMin; 
    3230    /** 
    3331     * a mock to be used for the maximum validator 
    3432     * 
    35      * @var  stubValidator 
    36      */ 
    37     protected $mockStubValidatorMax; 
     33     * @var  PHPUnit_Framework_MockObject_MockObject 
     34     */ 
     35    protected $mockValidatorMax; 
    3836     
    3937    /** 
     
    4240    public function setUp() 
    4341    { 
    44         stubRegistry::setConfig('net.stubbles.number.decimals', 3); 
    45         $this->mockStubRequestValueErrorFactory = new MockStubRequestValueErrorFactory(); 
    46         $this->mockStubValidatorMin             = new MockStubValidator(); 
    47         $this->mockStubValidatorMax             = new MockStubValidator(); 
    48         $this->mockStubRequestValueErrorFactory->setReturnValue('create', new stubRequestValueError('foo', array('en_EN' => 'Something wrent wrong.'))); 
     42        stubRegistry::setConfig(stubFloatFilter::DECIMALS_REGISTRY_KEY, 3); 
     43        $this->mockRequestValueErrorFactory = $this->getMock('stubRequestValueErrorFactory'); 
     44        $this->mockValidatorMin             = $this->getMock('stubValidator'); 
     45        $this->mockValidatorMax             = $this->getMock('stubValidator'); 
    4946    } 
    5047 
     
    5451    public function tearDown() 
    5552    { 
    56         stubRegistry::removeConfig('net.stubbles.number.decimals'); 
     53        stubRegistry::removeConfig(stubFloatFilter::DECIMALS_REGISTRY_KEY); 
    5754    } 
    5855 
    5956    /** 
    6057     * assure that values are returned the expected way 
    61      */ 
    62     public function testValue() 
    63     { 
    64         $floatFilter = new stubFloatFilter($this->mockStubRequestValueErrorFactory); 
    65         $this->assertEqual($floatFilter->execute('8.4533'), 8453); 
    66         $this->assertEqual($floatFilter->execute('8.4538'), 8453); 
    67         $this->assertEqual($floatFilter->execute('8.45'), 8450); 
    68         $this->assertEqual($floatFilter->execute('8'), 8000); 
    69         $this->assertEqual($floatFilter->execute(8.4533), 8453); 
    70         $this->assertEqual($floatFilter->execute(8.4538), 8453); 
    71         $this->assertEqual($floatFilter->execute(8.45), 8450); 
    72         $this->assertEqual($floatFilter->execute(8), 8000); 
     58     * 
     59     * @test 
     60     */ 
     61    public function value() 
     62    { 
     63        $floatFilter = new stubFloatFilter($this->mockRequestValueErrorFactory); 
     64        $this->assertEquals(8453, $floatFilter->execute('8.4533')); 
     65        $this->assertEquals(8453, $floatFilter->execute('8.4538')); 
     66        $this->assertEquals(8450, $floatFilter->execute('8.45')); 
     67        $this->assertEquals(8000, $floatFilter->execute('8')); 
     68        $this->assertEquals(8453, $floatFilter->execute(8.4533)); 
     69        $this->assertEquals(8453, $floatFilter->execute(8.4538)); 
     70        $this->assertEquals(8450, $floatFilter->execute(8.45)); 
     71        $this->assertEquals(8000, $floatFilter->execute(8)); 
    7372    } 
    7473 
     
    7675     * assure that an exceptiom is thrown when a value is 
    7776     * required but not passed 
    78      */ 
    79     public function testWithUnsetWhenRequired() 
    80     { 
    81         $this->mockStubRequestValueErrorFactory->expect('create', array('FIELD_EMPTY')); 
    82         $floatFilter = new stubFloatFilter($this->mockStubRequestValueErrorFactory); 
     77     * 
     78     * @test 
     79     * @expectedException  stubFilterException 
     80     */ 
     81    public function withUnsetWhenRequired() 
     82    { 
     83        $floatFilter = new stubFloatFilter($this->mockRequestValueErrorFactory); 
    8384        $floatFilter->setRequired(true); 
    84         $this->assertEqual($floatFilter->execute(true), 1000); 
    85         $this->assertEqual($floatFilter->execute(false), 0); 
    86         $this->expectException('stubFilterException'); 
     85        $this->assertEquals(1000, $floatFilter->execute(true)); 
     86        $this->assertEquals(0, $floatFilter->execute(false)); 
     87        $this->mockRequestValueErrorFactory->expects($this->once()) 
     88                                           ->method('create') 
     89                                           ->with($this->equalTo('FIELD_EMPTY')) 
     90                                           ->will($this->returnValue(new stubRequestValueError('foo', array('en_EN' => 'Something wrent wrong.')))); 
    8791        $floatFilter->execute(null); 
    8892    } 
     
    9195     * assure that an exceptiom is thrown when a value is 
    9296     * required but not passed 
    93      */ 
    94     public function testWithUnsetEmptyStringWhenRequired() 
    95     { 
    96         $this->mockStubRequestValueErrorFactory->expect('create', array('FIELD_EMPTY')); 
    97         $floatFilter = new stubFloatFilter($this->mockStubRequestValueErrorFactory); 
     97     * 
     98     * @test 
     99     * @expectedException  stubFilterException 
     100     */ 
     101    public function withUnsetEmptyStringWhenRequired() 
     102    { 
     103        $floatFilter = new stubFloatFilter($this->mockRequestValueErrorFactory); 
    98104        $floatFilter->setRequired(true); 
    99         $this->expectException('stubFilterException'); 
     105        $this->mockRequestValueErrorFactory->expects($this->once()) 
     106                                           ->method('create') 
     107                                           ->with($this->equalTo('FIELD_EMPTY')) 
     108                                           ->will($this->returnValue(new stubRequestValueError('foo', array('en_EN' => 'Something wrent wrong.')))); 
    100109        $floatFilter->execute(''); 
    101110    } 
     
    104113     * assure that 0 is returned when value not set or empty when no value 
    105114     * is required 
    106      */ 
    107     public function testUnsetWhenNotRequired() 
    108     { 
    109         $floatFilter = new stubFloatFilter($this->mockStubRequestValueErrorFactory); 
     115     * 
     116     * @test 
     117     */ 
     118    public function unsetWhenNotRequired() 
     119    { 
     120        $floatFilter = new stubFloatFilter($this->mockRequestValueErrorFactory); 
    110121        $floatFilter->setRequired(false); 
    111         $this->assertEqual($floatFilter->execute(null), 0); 
    112         $this->assertEqual($floatFilter->execute(''), 0); 
    113         $this->assertEqual($floatFilter->execute(true), 1000); 
    114         $this->assertEqual($floatFilter->execute(false), 0); 
     122        $this->assertEquals(0, $floatFilter->execute(null)); 
     123        $this->assertEquals(0, $floatFilter->execute('')); 
     124        $this->assertEquals(1000, $floatFilter->execute(true)); 
     125        $this->assertEquals(0, $floatFilter->execute(false)); 
    115126    } 
    116127 
    117128    /** 
    118129     * assure that an FilterException is thrown when value smaller then $min 
    119      */ 
    120     public function testWithMinValidator() 
    121     { 
    122         $this->mockStubValidatorMin->setReturnValueAt(0, 'validate', true); 
    123         $this->mockStubValidatorMin->setReturnValueAt(1, 'validate', false); 
    124         $this->mockStubValidatorMin->setReturnValue('getCriteria', array()); 
    125         $this->mockStubRequestValueErrorFactory->expect('create', array('VALUE_TOO_SMALL')); 
    126          
    127         $floatFilter = new stubFloatFilter($this->mockStubRequestValueErrorFactory, $this->mockStubValidatorMin); 
    128         $this->assertEqual($floatFilter->execute(-10), -10000); 
    129         $this->expectException('stubFilterException'); 
     130     * 
     131     * @test 
     132     */ 
     133    public function withMinValidator() 
     134    { 
     135        $this->mockValidatorMin->expects($this->once()) 
     136                               ->method('validate') 
     137                               ->will($this->returnValue(true)); 
     138        $this->mockValidatorMin->expects($this->never()) 
     139                               ->method('getCriteria'); 
     140        $floatFilter = new stubFloatFilter($this->mockRequestValueErrorFactory, $this->mockValidatorMin); 
     141        $this->assertEquals(-10000, $floatFilter->execute(-10)); 
     142    } 
     143 
     144    /** 
     145     * assure that an FilterException is thrown when value smaller then $min 
     146     * 
     147     * @test 
     148     * @expectedException  stubFilterException 
     149     */ 
     150    public function withMinValidatorFails() 
     151    { 
     152        $this->mockValidatorMin->expects($this->once()) 
     153                               ->method('validate') 
     154                               ->will($this->returnValue(false)); 
     155        $this->mockValidatorMin->expects($this->once()) 
     156                               ->method('getCriteria') 
     157                               ->will($this->returnValue(array())); 
     158        $floatFilter = new stubFloatFilter($this->mockRequestValueErrorFactory, $this->mockValidatorMin); 
     159        $this->mockRequestValueErrorFactory->expects($this->once()) 
     160                                           ->method('create') 
     161                                           ->with($this->equalTo('VALUE_TOO_SMALL')) 
     162                                           ->will($this->returnValue(new stubRequestValueError('foo', array('en_EN' => 'Something wrent wrong.')))); 
    130163        $floatFilter->execute(-11); 
    131164    } 
     
    133166    /** 
    134167     * assure that an FilterException is thrown when value greater then $max 
    135      */ 
    136     public function testWithMaxValidator() 
    137     { 
    138         $this->mockStubValidatorMax->setReturnValueAt(0, 'validate', true); 
    139         $this->mockStubValidatorMax->setReturnValueAt(1, 'validate', false); 
    140         $this->mockStubValidatorMax->setReturnValue('getCriteria', array()); 
    141         $this->mockStubRequestValueErrorFactory->expect('create', array('VALUE_TOO_GREAT')); 
    142          
    143         $floatFilter = new stubFloatFilter($this->mockStubRequestValueErrorFactory, null, $this->mockStubValidatorMax); 
    144         $this->assertEqual($floatFilter->execute(10), 10000); 
    145         $this->expectException('stubFilterException'); 
     168     * 
     169     * @test 
     170     */ 
     171    public function withMaxValidator() 
     172    { 
     173        $this->mockValidatorMax->expects($this->once()) 
     174                               ->method('validate') 
     175                               ->will($this->returnValue(true)); 
     176        $this->mockValidatorMax->expects($this->never()) 
     177                               ->method('getCriteria'); 
     178        $floatFilter = new stubFloatFilter($this->mockRequestValueErrorFactory, null, $this->mockValidatorMax); 
     179        $this->assertEquals($floatFilter->execute(10), 10000); 
     180    } 
     181 
     182    /** 
     183     * assure that an FilterException is thrown when value greater then $max 
     184     * 
     185     * @test 
     186     * @expectedException  stubFilterException 
     187     */ 
     188    public function withMaxValidatorFails() 
     189    { 
     190        $this->mockValidatorMax->expects($this->once()) 
     191                               ->method('validate') 
     192                               ->will($this->returnValue(false)); 
     193        $this->mockValidatorMax->expects($this->once()) 
     194                               ->method('getCriteria') 
     195                               ->will($this->returnValue(array())); 
     196        $floatFilter = new stubFloatFilter($this->mockRequestValueErrorFactory, null, $this->mockValidatorMax); 
     197        $this->mockRequestValueErrorFactory->expects($this->once()) 
     198                                           ->method('create') 
     199                                           ->with($this->equalTo('VALUE_TOO_GREAT')) 
     200                                           ->will($this->returnValue(new stubRequestValueError('foo', array('en_EN' => 'Something wrent wrong.')))); 
    146201        $floatFilter->execute(11); 
    147202    } 
     
    149204    /** 
    150205     * assure that the correct value depending on $decimal_places is returned 
    151      */ 
    152     public function testFloat() 
    153     { 
    154         stubRegistry::setConfig('net.stubbles.number.decimals', 2); 
    155         $floatFilter = new stubFloatFilter($this->mockStubRequestValueErrorFactory); 
    156         $this->assertEqual($floatFilter->execute('1.564'), 156); 
     206     * 
     207     * @test 
     208     */ 
     209    public function float() 
     210    { 
     211        stubRegistry::setConfig(stubFloatFilter::DECIMALS_REGISTRY_KEY, 2); 
     212        $floatFilter = new stubFloatFilter($this->mockRequestValueErrorFactory); 
     213        $this->assertEquals(156, $floatFilter->execute('1.564')); 
     214    } 
     215 
     216    /** 
     217     * assure that the correct value depending on $decimal_places is returned 
     218     * 
     219     * @test 
     220     */ 
     221    public function decimals0() 
     222    { 
     223        stubRegistry::setConfig(stubFloatFilter::DECIMALS_REGISTRY_KEY, 0); 
     224        $floatFilter = new stubFloatFilter($this->mockRequestValueErrorFactory); 
     225        $this->assertEquals(1.564, $floatFilter->execute('1.564')); 
     226    } 
     227 
     228    /** 
     229     * assure that the correct value depending on $decimal_places is returned 
     230     * 
     231     * @test 
     232     */ 
     233    public function registryKeyMissing() 
     234    { 
     235        stubRegistry::removeConfig(stubFloatFilter::DECIMALS_REGISTRY_KEY); 
     236        $floatFilter = new stubFloatFilter($this->mockRequestValueErrorFactory); 
     237        $this->assertEquals(1.564, $floatFilter->execute('1.564')); 
    157238    } 
    158239} 
  • trunk/src/test/php/net/stubbles/ipo/request/filters/stubHTTPURLFilterTestCase.php

    r1223 r1274  
    88 */ 
    99stubClassLoader::load('net::stubbles::ipo::request::filters::stubHTTPURLFilter'); 
    10 Mock::generate('stubRequestValueErrorFactory'); 
    1110/** 
    1211 * Tests for net::stubbles::ipo::request::filters::stubHTTPURLFilter. 
     
    1514 * @subpackage  ipo_request_filters_test 
    1615 */ 
    17 class stubHTTPURLFilterTestCase extends UnitTestCase 
     16class stubHTTPURLFilterTestCase extends PHPUnit_Framework_TestCase 
    1817{ 
    1918    /** 
     
    2625     * a mock to be used for the rveFactory 
    2726     * 
    28      * @var  stubRequestValueErrorFactory 
     27     * @var  PHPUnit_Framework_MockObject_MockObject 
    2928     */ 
    30     protected $mockStubRequestValueErrorFactory; 
     29    protected $mockRequestValueErrorFactory; 
    3130 
    3231    /** 
     
    3635    public function setUp() 
    3736    { 
    38         $this->mockStubRequestValueErrorFactory = new MockStubRequestValueErrorFactory(); 
    39         $this->mockStubRequestValueErrorFactory->setReturnValue('create', new stubRequestValueError('foo', array('en_EN' => 'Something wrent wrong.'))); 
    40         $this->httpURLFilter = new stubHTTPURLFilter($this->mockStubRequestValueErrorFactory); 
     37        $this->mockRequestValueErrorFactory = $this->getMock('stubRequestValueErrorFactory'); 
     38        $this->httpURLFilter = new stubHTTPURLFilter($this->mockRequestValueErrorFactory); 
    4139    } 
    4240 
    4341    /** 
    4442     * assure that values are returned the expected way 
     43     * 
     44     * @test 
    4545     */ 
    46     public function testValue() 
     46    public function value() 
    4747    { 
    48         $this->assertEqual($this->httpURLFilter->execute('http://example.org'), 'http://example.org/'); 
    49         $this->assertEqual($this->httpURLFilter->execute('http://example.org:45'), 'http://example.org:45/'); 
     48        $this->assertEquals('http://example.org/', $this->httpURLFilter->execute('http://example.org')); 
     49        $this->assertEquals('http://example.org:45/', $this->httpURLFilter->execute('http://example.org:45')); 
    5050    } 
    5151 
    5252    /** 
    5353     * assure correct behaviour when a null value is passed 
     54     * 
     55     * @test 
    5456     */ 
    55     public function testNullValue() 
     57    public function nullValue() 
    5658    { 
    5759        $this->httpURLFilter->setRequired(false); 
    5860        $this->assertNull($this->httpURLFilter->execute(null)); 
    59         $this->httpURLFilter->setRequired(true); 
    60         $this->mockStubRequestValueErrorFactory->expect('create', array('URL_INCORRECT')); 
    61         $this->expectException('stubFilterException'); 
    62         $this->httpURLFilter->execute(null); 
    6361    } 
    6462 
    6563    /** 
    6664     * assure correct behaviour when a null value is passed 
     65     * 
     66     * @test 
     67     * @expectedException  stubFilterException 
    6768     */ 
    68     public function testNullValueWithDifferentErrorId() 
     69    public function nullValueFails() 
    6970    { 
    7071        $this->httpURLFilter->setRequired(true); 
    71         $this->mockStubRequestValueErrorFactory->expect('create', array('URL_INCORRECT')); 
    72         $this->expectException('stubFilterException'); 
     72        $this->mockRequestValueErrorFactory->expects($this->once()) 
     73                                           ->method('create') 
     74                                           ->with($this->equalTo('URL_INCORRECT')) 
     75                                           ->will($this->returnValue(new stubRequestValueError('foo', array('en_EN' => 'Something wrent wrong.')))); 
    7376        $this->httpURLFilter->execute(null); 
    7477    } 
     
    7679    /** 
    7780     * assure correct behaviour when an empty value is passed 
     81     * 
     82     * @test 
    7883     */ 
    79     public function testEmptyValue() 
     84    public function emptyValue() 
    8085    { 
    8186        $this->httpURLFilter->setRequired(false); 
    8287        $this->assertNull($this->httpURLFilter->execute('')); 
     88    } 
     89 
     90    /** 
     91     * assure correct behaviour when an empty value is passed 
     92     * 
     93     * @test 
     94     * @expectedException  stubFilterException 
     95     */ 
     96    public function emptyValueFails() 
     97    { 
    8398        $this->httpURLFilter->setRequired(true); 
    84         $this->mockStubRequestValueErrorFactory->expect('create', array('URL_INCORRECT')); 
    85         $this->expectException('stubFilterException'); 
     99        $this->mockRequestValueErrorFactory->expects($this->once()) 
     100                                           ->method('create') 
     101                                           ->with($this->equalTo('URL_INCORRECT')) 
     102                                           ->will($this->returnValue(new stubRequestValueError('foo', array('en_EN' => 'Something wrent wrong.')))); 
    86103        $this->httpURLFilter->execute(''); 
    87104    } 
     
    89106    /** 
    90107     * assure that an exception is thrown when a wrong scheme is passed 
     108     * 
     109     * @test 
     110     * @expectedException  stubFilterException 
    91111     */ 
    92     public function testWrongScheme() 
     112    public function wrongScheme() 
    93113    { 
    94         $this->mockStubRequestValueErrorFactory->expect('create', array('URL_INCORRECT')); 
    95         $this->expectException('stubFilterException'); 
     114        $this->mockRequestValueErrorFactory->expects($this->once()) 
     115                                           ->method('create') 
     116                                           ->with($this->equalTo('URL_INCORRECT')) 
     117                                           ->will($this->returnValue(new stubRequestValueError('foo', array('en_EN' => 'Something wrent wrong.')))); 
    96118        $this->httpURLFilter->execute('ftp://foobar.de/'); 
    97119    } 
     
    99121    /** 
    100122     * assure that an exception is thrown when a wrong value is passed 
     123     * 
     124     * @test 
     125     * @expectedException  stubFilterException 
    101126     */ 
    102     public function testWrongValue() 
     127    public function wrongValue() 
    103128    { 
    104         $this->mockStubRequestValueErrorFactory->expect('create', array('URL_INCORRECT')); 
    105         $this->expectException('stubFilterException'); 
     129        $this->mockRequestValueErrorFactory->expects($this->once()) 
     130                                           ->method('create') 
     131                                           ->with($this->equalTo('URL_INCORRECT')) 
     132                                           ->will($this->returnValue(new stubRequestValueError('foo', array('en_EN' => 'Something wrent wrong.')))); 
    106133        $this->httpURLFilter->execute('http://wrong example!'); 
    107134    } 
     
    109136    /** 
    110137     * assert that a non-existing but correct URL is treated correct 
     138     * 
     139     * @test 
    111140     */ 
    112     public function testURLNotAvailable() 
     141    public function urlCheckDisabled() 
    113142    { 
    114143        $this->assertFalse($this->httpURLFilter->isDNSCheckEnabled()); 
    115         $this->assertEqual($this->httpURLFilter->execute('http://doesnotexist.1und1.de/'), 'http://doesnotexist.1und1.de/'); 
     144        $this->assertEquals('http://doesnotexist.1und1.de/', $this->httpURLFilter->execute('http://doesnotexist.1und1.de/')); 
     145    } 
     146 
     147    /** 
     148     * assert that a non-existing but correct URL is treated correct 
     149     * 
     150     * @test 
     151     */ 
     152    public function urlNotAvailable() 
     153    { 
    116154        $this->httpURLFilter->setCheckDNS(true); 
    117155        $this->assertTrue($this->httpURLFilter->isDNSCheckEnabled()); 
    118156        if (DIRECTORY_SEPARATOR === '\\') { 
    119157            // Windows does not support dns checks, filter will always return ok 
    120             $this->assertEqual($this->httpURLFilter->execute('http://doesnotexist.1und1.de/'), 'http://doesnotexist.1und1.de/'); 
     158            $this->assertEquals('http://doesnotexist.1und1.de/', $this->httpURLFilter->execute('http://doesnotexist.1und1.de/')); 
    121159        } else { 
    122             $this->mockStubRequestValueErrorFactory->expect('create', array('URL_NOT_AVAILABLE')); 
    123             $this->expectException('stubFilterException'); 
     160            $this->setExpectedException('stubFilterException'); 
     161            $this->mockRequestValueErrorFactory->expects($this->once()) 
     162                                               ->method('create') 
     163                                               ->with($this->equalTo('URL_NOT_AVAILABLE')) 
     164                                               ->will($this->returnValue(new stubRequestValueError('foo', array('en_EN' => 'Something wrent wrong.')))); 
    124165            $this->httpURLFilter->execute('http://doesnotexist.1und1.de/'); 
    125166        } 
  • trunk/src/test/php/net/stubbles/ipo/request/filters/stubIntegerFilterTestCase.php

    r1223 r1274  
    88 */ 
    99stubClassLoader::load('net::stubbles::ipo::request::filters::stubIntegerFilter'); 
    10 Mock::generate('stubRequestValueErrorFactory'); 
    11 Mock::generate('stubValidator'); 
    1210/** 
    1311 * Tests for net::stubbles::ipo::request::filters::stubIntegerFilter. 
     
    1614 * @subpackage  ipo_test 
    1715 */ 
    18 class stubIntegerFilterTestCase extends UnitTestCase 
     16class stubIntegerFilterTestCase extends PHPUnit_Framework_TestCase 
    1917{ 
    2018    /** 
    2119     * a mock to be used for the rveFactory 
    2220     * 
    23      * @var  stubRequestValueErrorFactory 
    24      */ 
    25     protected $mockStubRequestValueErrorFactory; 
     21     * @var  PHPUnit_Framework_MockObject_MockObject 
     22     */ 
     23    protected $mockRequestValueErrorFactory; 
    2624    /** 
    2725     * a mock to be used for the minimum validator 
    2826     * 
    29      * @var  stubValidator 
    30      */ 
    31     protected $mockStubValidatorMin; 
     27     * @var  PHPUnit_Framework_MockObject_MockObject 
     28     */ 
     29    protected $mockValidatorMin; 
    3230    /** 
    3331     * a mock to be used for the maximum validator 
    3432     * 
    35      * @var  stubValidator 
    36      */ 
    37     protected $mockStubValidatorMax; 
     33     * @var  PHPUnit_Framework_MockObject_MockObject 
     34     */ 
     35    protected $mockValidatorMax; 
    3836     
    3937    /** 
     
    4341    public function setUp() 
    4442    { 
    45         $this->mockStubRequestValueErrorFactory = new MockStubRequestValueErrorFactory(); 
    46         $this->mockStubValidatorMin             = new MockStubValidator(); 
    47         $this->mockStubValidatorMax             = new MockStubValidator(); 
    48         $this->mockStubRequestValueErrorFactory->setReturnValue('create', new stubRequestValueError('foo', array('en_EN' => 'Something wrent wrong.'))); 
     43        $this->mockRequestValueErrorFactory = $this->getMock('stubRequestValueErrorFactory'); 
     44        $this->mockValidatorMin             = $this->getMock('stubValidator'); 
     45        $this->mockValidatorMax             = $this->getMock('stubValidator'); 
    4946    } 
    5047     
    5148    /** 
    5249     * assure that values are returned the expected way 
    53      */ 
    54     public function testValue() 
    55     { 
    56         $integerFilter = new stubIntegerFilter($this->mockStubRequestValueErrorFactory); 
    57         $this->assertEqual($integerFilter->execute(8), 8); 
     50     * 
     51     * @test 
     52     */ 
     53    public function value() 
     54    { 
     55        $integerFilter = new stubIntegerFilter($this->mockRequestValueErrorFactory); 
     56        $this->assertEquals(8, $integerFilter->execute(8)); 
    5857    } 
    5958     
     
    6160     * assure that an exceptiom is thrown when a value is 
    6261     * required but not passed 
    63      */ 
    64     public function testWithUnsetWhenRequired() 
    65     { 
    66         $this->mockStubRequestValueErrorFactory->expect('create', array('FIELD_EMPTY')); 
    67         $integerFilter = new stubIntegerFilter($this->mockStubRequestValueErrorFactory); 
     62     * 
     63     * @test 
     64     * @expectedException  stubFilterException 
     65     */ 
     66    public function withUnsetWhenRequired() 
     67    { 
     68        $integerFilter = new stubIntegerFilter($this->mockRequestValueErrorFactory); 
    6869        $integerFilter->setRequired(true); 
    69         $this->assertEqual($integerFilter->execute(true), 1); 
    70         $this->assertEqual($integerFilter->execute(false), 0); 
    71         $this->expectException('stubFilterException'); 
     70        $this->assertEquals(1, $integerFilter->execute(true)); 
     71        $this->assertEquals(0, $integerFilter->execute(false)); 
     72        $this->mockRequestValueErrorFactory->expects($this->once()) 
     73                                           ->method('create') 
     74                                           ->with($this->equalTo('FIELD_EMPTY')) 
     75                                           ->will($this->returnValue(new stubRequestValueError('foo', array('en_EN' => 'Something wrent wrong.')))); 
    7276        $value = $integerFilter->execute(null); 
    7377    } 
     
    7680     * assure that an exceptiom is thrown when a value is 
    7781     * required but not passed 
    78      */ 
    79     public function testWithUnsetEmptyStringWhenRequired() 
    80     { 
    81         $this->mockStubRequestValueErrorFactory->expect('create', array('FIELD_EMPTY')); 
    82         $integerFilter = new stubIntegerFilter($this->mockStubRequestValueErrorFactory); 
     82     * 
     83     * @test 
     84     * @expectedException  stubFilterException 
     85     */ 
     86    public function withUnsetEmptyStringWhenRequired() 
     87    { 
     88        $integerFilter = new stubIntegerFilter($this->mockRequestValueErrorFactory); 
    8389        $integerFilter->setRequired(true); 
    84         $this->expectException('stubFilterException'); 
     90        $this->mockRequestValueErrorFactory->expects($this->once()) 
     91                                           ->method('create') 
     92                                           ->with($this->equalTo('FIELD_EMPTY')) 
     93                                           ->will($this->returnValue(new stubRequestValueError('foo', array('en_EN' => 'Something wrent wrong.')))); 
    8594        $integerFilter->execute(''); 
    8695    } 
     
    8998     * assure that 0 or 1 is returned when value not set or empty when no value 
    9099     * is required 
    91      */ 
    92     public function testUnsetWhenNotRequired() 
    93     { 
    94         $integerFilter = new stubIntegerFilter($this->mockStubRequestValueErrorFactory); 
     100     * 
     101     * @test 
     102     */ 
     103    public function unsetWhenNotRequired() 
     104    { 
     105        $integerFilter = new stubIntegerFilter($this->mockRequestValueErrorFactory); 
    95106        $integerFilter->setRequired(false); 
    96         $this->assertEqual($integerFilter->execute(null), 0); 
    97         $this->assertEqual($integerFilter->execute(''), 0); 
    98         $this->assertEqual($integerFilter->execute(true), 1); 
    99         $this->assertEqual($integerFilter->execute(false), 0); 
     107        $this->assertEquals(0, $integerFilter->execute(null)); 
     108        $this->assertEquals(0, $integerFilter->execute('')); 
     109        $this->assertEquals(1, $integerFilter->execute(true)); 
     110        $this->assertEquals(0, $integerFilter->execute(false)); 
    100111    } 
    101112 
    102113    /** 
    103114     * assure that an FilterException is thrown when value smaller then $min 
    104      */ 
    105     public function testWithMinValidator() 
    106     { 
    107         $this->mockStubValidatorMin->setReturnValueAt(0, 'validate', true); 
    108         $this->mockStubValidatorMin->setReturnValueAt(1, 'validate', false); 
    109         $this->mockStubValidatorMin->setReturnValue('getCriteria', array()); 
    110         $this->mockStubRequestValueErrorFactory->expect('create', array('VALUE_TOO_SMALL')); 
    111          
    112         $integerFilter = new stubIntegerFilter($this->mockStubRequestValueErrorFactory, $this->mockStubValidatorMin); 
    113         $this->assertEqual($integerFilter->execute(-10), -10); 
    114         $this->expectException('stubFilterException'); 
     115     * 
     116     * @test 
     117     */ 
     118    public function withMinValidator() 
     119    { 
     120        $this->mockValidatorMin->expects($this->once()) 
     121                               ->method('validate') 
     122                               ->will($this->returnValue(true)); 
     123        $this->mockValidatorMin->expects($this->never()) 
     124                               ->method('getCriteria'); 
     125        $integerFilter = new stubIntegerFilter($this->mockRequestValueErrorFactory, $this->mockValidatorMin); 
     126        $this->assertEquals(-10, $integerFilter->execute(-10)); 
     127    } 
     128 
     129    /** 
     130     * assure that an FilterException is thrown when value smaller then $min 
     131     * 
     132     * @test 
     133     * @expectedException  stubFilterException 
     134     */ 
     135    public function withMinValidatorFails() 
     136    { 
     137        $this->mockValidatorMin->expects($this->once()) 
     138                               ->method('validate') 
     139                               ->will($this->returnValue(false)); 
     140        $this->mockValidatorMin->expects($this->once()) 
     141                               ->method('getCriteria') 
     142                               ->will($this->returnValue(array())); 
     143        $integerFilter = new stubIntegerFilter($this->mockRequestValueErrorFactory, $this->mockValidatorMin); 
     144        $this->mockRequestValueErrorFactory->expects($this->once(