      1: <?php
      2:
      3: /**
      4:  *
      5:  * Test case for the String data type
      6:  *
      7:  * @package alpha::tests
      8:  * @since 1.0
      9:  * @author John Collins <dev@alphaframework.org>
     10:  * @version $Id: String_Test.php 1496 2012-02-12 20:32:21Z alphadev $
     11:  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
     12:  * @copyright Copyright (c) 2012, John Collins (founder of Alpha Framework).
     13:  * All rights reserved.
     14:  *
     15:  * <pre>
     16:  * Redistribution and use in source and binary forms, with or
     17:  * without modification, are permitted provided that the
     18:  * following conditions are met:
     19:  *
     20:  * * Redistributions of source code must retain the above
     21:  *   copyright notice, this list of conditions and the
     22:  *   following disclaimer.
     23:  * * Redistributions in binary form must reproduce the above
     24:  *   copyright notice, this list of conditions and the
     25:  *   following disclaimer in the documentation and/or other
     26:  *   materials provided with the distribution.
     27:  * * Neither the name of the Alpha Framework nor the names
     28:  *   of its contributors may be used to endorse or promote
     29:  *   products derived from this software without specific
     30:  *   prior written permission.
     31:  *
     32:  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
     33:  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
     34:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     35:  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     36:  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
     37:  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     38:  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     39:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     40:  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     41:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     42:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
     43:  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     44:  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     45:  * </pre>
     46:  *
     47:  */
     48: class String_Test extends PHPUnit_Framework_TestCase {
     49:     /**
     50:      * A String for testing
     51:      *
     52:      * @var String
     53:      * @since 1.0
     54:      */
     55:     private $str1;
     56:
     57:     /**
     58:      * A helper string for username reg-ex validation tests
     59:      *
     60:      * @var string
     61:      * @since 1.0
     62:      */
     63:     private $usernameHelper = 'Please provide a name for display on the website (only letters, numbers, and .-_ characters are allowed!).';
     64:
     65:     /**
     66:      * A helper string for email reg-ex validation tests
     67:      *
     68:      * @var string
     69:      * @since 1.0
     70:      */
     71:     private $emailHelper = 'Please provide a valid e-mail address as your username';
     72:
     73:     /**
     74:      * A helper string for URL reg-ex validation tests
     75:      *
     76:      * @var string
     77:      * @since 1.0
     78:      */
     79:     private $urlHelper = 'URLs must be in the format http://some_domain/ or left blank!';
     80:
     81:     /**
     82:      * Called before the test functions will be executed
     83:      * this function is defined in PHPUnit_TestCase and overwritten
     84:      * here
     85:      *
     86:      * @since 1.0
     87:      */
     88:     protected function setUp() {
     89:         $this->str1 = new String();
     90:     }
     91:
     92:     /**
     93:      * Called after the test functions are executed
     94:      * this function is defined in PHPUnit_TestCase and overwritten
     95:      * here
     96:      *
     97:      * @since 1.0
     98:      */
     99:     protected function tearDown() {
    100:         unset($this->str1);
    101:     }
    102:
    103:     /**
    104:      * Testing the str constructor for acceptance of correct data
    105:      *
    106:      * @since 1.0
    107:      */
    108:     public function testConstructorPass() {
    109:         $this->str1 = new String('A String Value!');
    110:
    111:         $this->assertEquals('A String Value!', $this->str1->getValue(), "testing the String constructor for pass");
    112:     }
    113:
    114:     /**
    115:      * Testing passing an invalid username string
    116:      *
    117:      * @since 1.0
    118:      */
    119:     public function testSetUsernameValueInvalid() {
    120:         try {
    121:             $this->str1->setRule(AlphaValidator::REQUIRED_USERNAME);
    122:             $this->str1->setSize(70);
    123:             $this->str1->setHelper($this->usernameHelper);
    124:
    125:             $this->str1->setValue('invalid user.');
    126:             $this->fail('testing passing an invalid username string');
    127:         }catch (AlphaException $e) {
    128:             $this->assertEquals($this->usernameHelper
    129:                 , $e->getMessage()
    130:                 , 'testing passing an invalid username string');
    131:         }
    132:     }
    133:
    134:     /**
    135:      * Testing passing a valid username string
    136:      *
    137:      * @since 1.0
    138:      */
    139:     public function testSetUsernameValueValid() {
    140:         try {
    141:             $this->str1->setRule(AlphaValidator::REQUIRED_USERNAME);
    142:             $this->str1->setSize(70);
    143:             $this->str1->setHelper($this->usernameHelper);
    144:
    145:             $this->str1->setValue('user_name.-test123gg');
    146:         }catch (AlphaException $e) {
    147:             $this->fail('testing passing a valid username string: '.$e->getMessage());
    148:         }
    149:     }
    150:
    151:     /**
    152:      * Testing passing an invalid email string
    153:      *
    154:      * @since 1.0
    155:      */
    156:     public function testSetEmailValueInvalid() {
    157:         try {
    158:             $this->str1->setRule(AlphaValidator::REQUIRED_EMAIL);
    159:             $this->str1->setSize(70);
    160:             $this->str1->setHelper($this->emailHelper);
    161:
    162:             $this->str1->setValue('invalid email');
    163:             $this->fail('testing passing an invalid email string');
    164:         }catch (AlphaException $e) {
    165:             $this->assertEquals($this->emailHelper
    166:                 , $e->getMessage()
    167:                 , 'testing passing an invalid email string');
    168:         }
    169:     }
    170:
    171:     /**
    172:      * Testing passing a valid email string
    173:      *
    174:      * @since 1.0
    175:      */
    176:     public function testSetEmailValueValid() {
    177:         try {
    178:             $this->str1->setRule(AlphaValidator::REQUIRED_EMAIL);
    179:             $this->str1->setSize(70);
    180:             $this->str1->setHelper($this->emailHelper);
    181:
    182:             $this->str1->setValue('user@somewhere.com');
    183:             $this->str1->setValue('user@somewhere.ie');
    184:             $this->str1->setValue('user@somewhere.co.uk');
    185:             $this->str1->setValue('user@somewhere.net');
    186:             $this->str1->setValue('user@somewhere.org');
    187:             $this->str1->setValue('some.user@somewhere.com');
    188:             $this->str1->setValue('some.user@somewhere.ie');
    189:             $this->str1->setValue('some.user@somewhere.co.uk');
    190:             $this->str1->setValue('some.user@somewhere.net');
    191:             $this->str1->setValue('some.user@somewhere.org');
    192:         }catch (AlphaException $e) {
    193:             $this->fail('testing passing a valid email string: '.$e->getMessage());
    194:         }
    195:     }
    196:
    197:     /**
    198:      * Testing passing an invalid URL string
    199:      *
    200:      * @since 1.0
    201:      */
    202:     public function testSetURLValueInvalid() {
    203:         try {
    204:             $this->str1->setRule(AlphaValidator::OPTIONAL_HTTP_URL);
    205:             $this->str1->setHelper($this->urlHelper);
    206:
    207:             $this->str1->setValue('invalid url');
    208:             $this->fail('testing passing an invalid URL string');
    209:         }catch (AlphaException $e) {
    210:             $this->assertEquals($this->urlHelper
    211:                 , $e->getMessage()
    212:                 , 'testing passing an invalid URL string');
    213:         }
    214:     }
    215:
    216:     /**
    217:      * Testing passing a valid URL string
    218:      *
    219:      * @since 1.0
    220:      */
    221:     public function testSetURLValueValid() {
    222:         try {
    223:             $this->str1->setRule(AlphaValidator::OPTIONAL_HTTP_URL);
    224:             $this->str1->setHelper($this->urlHelper);
    225:
    226:             $this->str1->setValue('http://www.google.com/');
    227:             $this->str1->setValue('http://slashdot.org/');
    228:             $this->str1->setValue('http://www.yahoo.com/');
    229:             $this->str1->setValue('http://www.design-ireland.net/');
    230:             $this->str1->setValue('http://www.theregister.co.uk/');
    231:             $this->str1->setValue('http://www.bbc.co.uk/');
    232:         }catch (AlphaException $e) {
    233:             $this->fail('testing passing a valid URL string: '.$e->getMessage());
    234:         }
    235:     }
    236:
    237:     /**
    238:      * Testing the setSize method to see if validation fails
    239:      *
    240:      * @since 1.0
    241:      */
    242:     public function testSetSizeInvalid() {
    243:         $this->str1 = new String();
    244:         $this->str1->setSize(4);
    245:
    246:         try {
    247:             $this->str1->setValue('Too many characters!');
    248:             $this->fail('testing the setSize method to see if validation fails');
    249:         }catch (AlphaException $e) {
    250:             $this->assertEquals('Not a valid string value!'
    251:                 , $e->getMessage()
    252:                 , 'testing the setSize method to see if validation fails');
    253:         }
    254:     }
    255:
    256:     /**
    257:      * Testing the __toString method
    258:      *
    259:      * @since 1.0
    260:      */
    261:     public function testToString() {
    262:         $this->str1 = new String('__toString result');
    263:
    264:         $this->assertEquals('The value of __toString result', 'The value of '.$this->str1, 'testing the __toString method');
    265:     }
    266:
    267:     /**
    268:      * Testing to see if the password setter/inspector is working
    269:      *
    270:      * @since 1.0
    271:      */
    272:     public function testIsPassword() {
    273:         $this->str1->isPassword();
    274:
    275:         $this->assertTrue($this->str1->checkIsPassword(), 'testing to see if the password setter/inspector is working');
    276:     }
    277:
    278:     /**
    279:      * Testing to see that isPassword makes the string required
    280:      *
    281:      * @since 1.0
    282:      */
    283:     public function testIsPasswordRequired() {
    284:         $this->str1->isPassword();
    285:
    286:         try {
    287:             $this->str1->setValue('');
    288:             $this->fail('testing to see that isPassword makes the string required');
    289:         }catch (AlphaException $e) {
    290:             $this->assertEquals('Password is required!'
    291:                 , $e->getMessage()
    292:                 , 'testing to see that isPassword makes the string required');
    293:         }
    294:     }
    295: }
    296:
    297: ?>