1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47:
48: class String_Test extends PHPUnit_Framework_TestCase {
49: 50: 51: 52: 53: 54:
55: private $str1;
56:
57: 58: 59: 60: 61: 62:
63: private $usernameHelper = 'Please provide a name for display on the website (only letters, numbers, and .-_ characters are allowed!).';
64:
65: 66: 67: 68: 69: 70:
71: private $emailHelper = 'Please provide a valid e-mail address as your username';
72:
73: 74: 75: 76: 77: 78:
79: private $urlHelper = 'URLs must be in the format http://some_domain/ or left blank!';
80:
81: 82: 83: 84: 85: 86: 87:
88: protected function setUp() {
89: $this->str1 = new String();
90: }
91:
92: 93: 94: 95: 96: 97: 98:
99: protected function tearDown() {
100: unset($this->str1);
101: }
102:
103: 104: 105: 106: 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: 116: 117: 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: 136: 137: 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: 153: 154: 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: 173: 174: 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: 199: 200: 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: 218: 219: 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: 239: 240: 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: 258: 259: 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: 269: 270: 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: 280: 281: 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: ?>