PageRenderTime 126ms CodeModel.GetById 61ms app.highlight 29ms RepoModel.GetById 31ms app.codeStats 0ms

/tests/Zend/Validate/NotEmptyTest.php

https://bitbucket.org/dbaltas/zend-framework-1.x-on-git
PHP | 627 lines | 428 code | 37 blank | 162 comment | 1 complexity | 040ea3f14ef33c00a277ba77905a0f1c MD5 | raw file
  1<?php
  2/**
  3 * Zend Framework
  4 *
  5 * LICENSE
  6 *
  7 * This source file is subject to the new BSD license that is bundled
  8 * with this package in the file LICENSE.txt.
  9 * It is also available through the world-wide-web at this URL:
 10 * http://framework.zend.com/license/new-bsd
 11 * If you did not receive a copy of the license and are unable to
 12 * obtain it through the world-wide-web, please send an email
 13 * to license@zend.com so we can send you a copy immediately.
 14 *
 15 * @category   Zend
 16 * @package    Zend_Validate
 17 * @subpackage UnitTests
 18 * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 19 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 20 * @version    $Id: NotEmptyTest.php 24593 2012-01-05 20:35:02Z matthew $
 21 */
 22
 23// Call Zend_Validate_NotEmptyTest::main() if this source file is executed directly.
 24if (!defined("PHPUnit_MAIN_METHOD")) {
 25    define("PHPUnit_MAIN_METHOD", "Zend_Validate_NotEmptyTest::main");
 26}
 27
 28/**
 29 * @see Zend_Validate_NotEmpty
 30 */
 31require_once 'Zend/Validate/NotEmpty.php';
 32
 33
 34/**
 35 * @category   Zend
 36 * @package    Zend_Validate
 37 * @subpackage UnitTests
 38 * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 39 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 40 * @group      Zend_Validate
 41 */
 42class Zend_Validate_NotEmptyTest extends PHPUnit_Framework_TestCase
 43{
 44    /**
 45     * Runs the test methods of this class.
 46     *
 47     * @return void
 48     */
 49    public static function main()
 50    {
 51        $suite  = new PHPUnit_Framework_TestSuite("Zend_Validate_NotEmptyTest");
 52        $result = PHPUnit_TextUI_TestRunner::run($suite);
 53    }
 54
 55    /**
 56     * Zend_Validate_NotEmpty object
 57     *
 58     * @var Zend_Validate_NotEmpty
 59     */
 60    protected $_validator;
 61
 62    /**
 63     * Creates a new Zend_Validate_NotEmpty object for each test method
 64     *
 65     * @return void
 66     */
 67    public function setUp()
 68    {
 69        $this->_validator = new Zend_Validate_NotEmpty();
 70    }
 71
 72    /**
 73     * Ensures that the validator follows expected behavior
 74     *
 75     * ZF-6708 introduces a change for validating integer 0; it is a valid
 76     * integer value. '0' is also valid.
 77     *
 78     * @group ZF-6708
 79     * @return void
 80     */
 81    public function testBasic()
 82    {
 83        $valuesExpected = array(
 84            array('word', true),
 85            array('', false),
 86            array('    ', false),
 87            array('  word  ', true),
 88            array('0', true),
 89            array(1, true),
 90            array(0, true),
 91            array(true, true),
 92            array(false, false),
 93            array(null, false),
 94            array(array(), false),
 95            array(array(5), true),
 96        );
 97        foreach ($valuesExpected as $i => $element) {
 98            $this->assertEquals($element[1], $this->_validator->isValid($element[0]),
 99                "Failed test #$i");
100        }
101    }
102
103    /**
104     * Ensures that the validator follows expected behavior
105     *
106     * @return void
107     */
108    public function testOnlyBoolean()
109    {
110        $this->_validator->setType(Zend_Validate_NotEmpty::BOOLEAN);
111        $this->assertFalse($this->_validator->isValid(false));
112        $this->assertTrue($this->_validator->isValid(true));
113        $this->assertTrue($this->_validator->isValid(0));
114        $this->assertTrue($this->_validator->isValid(1));
115        $this->assertTrue($this->_validator->isValid(0.0));
116        $this->assertTrue($this->_validator->isValid(1.0));
117        $this->assertTrue($this->_validator->isValid(''));
118        $this->assertTrue($this->_validator->isValid('abc'));
119        $this->assertTrue($this->_validator->isValid('0'));
120        $this->assertTrue($this->_validator->isValid('1'));
121        $this->assertTrue($this->_validator->isValid(array()));
122        $this->assertTrue($this->_validator->isValid(array('xxx')));
123        $this->assertTrue($this->_validator->isValid(null));
124    }
125
126    /**
127     * Ensures that the validator follows expected behavior
128     *
129     * @return void
130     */
131    public function testOnlyInteger()
132    {
133        $this->_validator->setType(Zend_Validate_NotEmpty::INTEGER);
134        $this->assertTrue($this->_validator->isValid(false));
135        $this->assertTrue($this->_validator->isValid(true));
136        $this->assertFalse($this->_validator->isValid(0));
137        $this->assertTrue($this->_validator->isValid(1));
138        $this->assertTrue($this->_validator->isValid(0.0));
139        $this->assertTrue($this->_validator->isValid(1.0));
140        $this->assertTrue($this->_validator->isValid(''));
141        $this->assertTrue($this->_validator->isValid('abc'));
142        $this->assertTrue($this->_validator->isValid('0'));
143        $this->assertTrue($this->_validator->isValid('1'));
144        $this->assertTrue($this->_validator->isValid(array()));
145        $this->assertTrue($this->_validator->isValid(array('xxx')));
146        $this->assertTrue($this->_validator->isValid(null));
147    }
148
149    /**
150     * Ensures that the validator follows expected behavior
151     *
152     * @return void
153     */
154    public function testOnlyFloat()
155    {
156        $this->_validator->setType(Zend_Validate_NotEmpty::FLOAT);
157        $this->assertTrue($this->_validator->isValid(false));
158        $this->assertTrue($this->_validator->isValid(true));
159        $this->assertTrue($this->_validator->isValid(0));
160        $this->assertTrue($this->_validator->isValid(1));
161        $this->assertFalse($this->_validator->isValid(0.0));
162        $this->assertTrue($this->_validator->isValid(1.0));
163        $this->assertTrue($this->_validator->isValid(''));
164        $this->assertTrue($this->_validator->isValid('abc'));
165        $this->assertTrue($this->_validator->isValid('0'));
166        $this->assertTrue($this->_validator->isValid('1'));
167        $this->assertTrue($this->_validator->isValid(array()));
168        $this->assertTrue($this->_validator->isValid(array('xxx')));
169        $this->assertTrue($this->_validator->isValid(null));
170    }
171
172    /**
173     * Ensures that the validator follows expected behavior
174     *
175     * @return void
176     */
177    public function testOnlyString()
178    {
179        $this->_validator->setType(Zend_Validate_NotEmpty::STRING);
180        $this->assertTrue($this->_validator->isValid(false));
181        $this->assertTrue($this->_validator->isValid(true));
182        $this->assertTrue($this->_validator->isValid(0));
183        $this->assertTrue($this->_validator->isValid(1));
184        $this->assertTrue($this->_validator->isValid(0.0));
185        $this->assertTrue($this->_validator->isValid(1.0));
186        $this->assertFalse($this->_validator->isValid(''));
187        $this->assertTrue($this->_validator->isValid('abc'));
188        $this->assertTrue($this->_validator->isValid('0'));
189        $this->assertTrue($this->_validator->isValid('1'));
190        $this->assertTrue($this->_validator->isValid(array()));
191        $this->assertTrue($this->_validator->isValid(array('xxx')));
192        $this->assertTrue($this->_validator->isValid(null));
193    }
194
195    /**
196     * Ensures that the validator follows expected behavior
197     *
198     * @return void
199     */
200    public function testOnlyZero()
201    {
202        $this->_validator->setType(Zend_Validate_NotEmpty::ZERO);
203        $this->assertTrue($this->_validator->isValid(false));
204        $this->assertTrue($this->_validator->isValid(true));
205        $this->assertTrue($this->_validator->isValid(0));
206        $this->assertTrue($this->_validator->isValid(1));
207        $this->assertTrue($this->_validator->isValid(0.0));
208        $this->assertTrue($this->_validator->isValid(1.0));
209        $this->assertTrue($this->_validator->isValid(''));
210        $this->assertTrue($this->_validator->isValid('abc'));
211        $this->assertFalse($this->_validator->isValid('0'));
212        $this->assertTrue($this->_validator->isValid('1'));
213        $this->assertTrue($this->_validator->isValid(array()));
214        $this->assertTrue($this->_validator->isValid(array('xxx')));
215        $this->assertTrue($this->_validator->isValid(null));
216    }
217
218    /**
219     * Ensures that the validator follows expected behavior
220     *
221     * @return void
222     */
223    public function testOnlyArray()
224    {
225        $this->_validator->setType(Zend_Validate_NotEmpty::EMPTY_ARRAY);
226        $this->assertTrue($this->_validator->isValid(false));
227        $this->assertTrue($this->_validator->isValid(true));
228        $this->assertTrue($this->_validator->isValid(0));
229        $this->assertTrue($this->_validator->isValid(1));
230        $this->assertTrue($this->_validator->isValid(0.0));
231        $this->assertTrue($this->_validator->isValid(1.0));
232        $this->assertTrue($this->_validator->isValid(''));
233        $this->assertTrue($this->_validator->isValid('abc'));
234        $this->assertTrue($this->_validator->isValid('0'));
235        $this->assertTrue($this->_validator->isValid('1'));
236        $this->assertFalse($this->_validator->isValid(array()));
237        $this->assertTrue($this->_validator->isValid(array('xxx')));
238        $this->assertTrue($this->_validator->isValid(null));
239    }
240
241    /**
242     * Ensures that the validator follows expected behavior
243     *
244     * @return void
245     */
246    public function testOnlyNull()
247    {
248        $this->_validator->setType(Zend_Validate_NotEmpty::NULL);
249        $this->assertTrue($this->_validator->isValid(false));
250        $this->assertTrue($this->_validator->isValid(true));
251        $this->assertTrue($this->_validator->isValid(0));
252        $this->assertTrue($this->_validator->isValid(1));
253        $this->assertTrue($this->_validator->isValid(0.0));
254        $this->assertTrue($this->_validator->isValid(1.0));
255        $this->assertTrue($this->_validator->isValid(''));
256        $this->assertTrue($this->_validator->isValid('abc'));
257        $this->assertTrue($this->_validator->isValid('0'));
258        $this->assertTrue($this->_validator->isValid('1'));
259        $this->assertTrue($this->_validator->isValid(array()));
260        $this->assertTrue($this->_validator->isValid(array('xxx')));
261        $this->assertFalse($this->_validator->isValid(null));
262    }
263
264    /**
265     * Ensures that the validator follows expected behavior
266     *
267     * @return void
268     */
269    public function testOnlyPHP()
270    {
271        $this->_validator->setType(Zend_Validate_NotEmpty::PHP);
272        $this->assertFalse($this->_validator->isValid(false));
273        $this->assertTrue($this->_validator->isValid(true));
274        $this->assertFalse($this->_validator->isValid(0));
275        $this->assertTrue($this->_validator->isValid(1));
276        $this->assertFalse($this->_validator->isValid(0.0));
277        $this->assertTrue($this->_validator->isValid(1.0));
278        $this->assertFalse($this->_validator->isValid(''));
279        $this->assertTrue($this->_validator->isValid('abc'));
280        $this->assertFalse($this->_validator->isValid('0'));
281        $this->assertTrue($this->_validator->isValid('1'));
282        $this->assertFalse($this->_validator->isValid(array()));
283        $this->assertTrue($this->_validator->isValid(array('xxx')));
284        $this->assertFalse($this->_validator->isValid(null));
285    }
286
287    /**
288     * Ensures that the validator follows expected behavior
289     *
290     * @return void
291     */
292    public function testOnlySpace()
293    {
294        $this->_validator->setType(Zend_Validate_NotEmpty::SPACE);
295        $this->assertTrue($this->_validator->isValid(false));
296        $this->assertTrue($this->_validator->isValid(true));
297        $this->assertTrue($this->_validator->isValid(0));
298        $this->assertTrue($this->_validator->isValid(1));
299        $this->assertTrue($this->_validator->isValid(0.0));
300        $this->assertTrue($this->_validator->isValid(1.0));
301        $this->assertTrue($this->_validator->isValid(''));
302        $this->assertTrue($this->_validator->isValid('abc'));
303        $this->assertTrue($this->_validator->isValid('0'));
304        $this->assertTrue($this->_validator->isValid('1'));
305        $this->assertTrue($this->_validator->isValid(array()));
306        $this->assertTrue($this->_validator->isValid(array('xxx')));
307        $this->assertTrue($this->_validator->isValid(null));
308    }
309
310    /**
311     * Ensures that the validator follows expected behavior
312     *
313     * @return void
314     */
315    public function testOnlyAll()
316    {
317        $this->_validator->setType(Zend_Validate_NotEmpty::ALL);
318        $this->assertFalse($this->_validator->isValid(false));
319        $this->assertTrue($this->_validator->isValid(true));
320        $this->assertFalse($this->_validator->isValid(0));
321        $this->assertTrue($this->_validator->isValid(1));
322        $this->assertFalse($this->_validator->isValid(0.0));
323        $this->assertTrue($this->_validator->isValid(1.0));
324        $this->assertFalse($this->_validator->isValid(''));
325        $this->assertTrue($this->_validator->isValid('abc'));
326        $this->assertFalse($this->_validator->isValid('0'));
327        $this->assertTrue($this->_validator->isValid('1'));
328        $this->assertFalse($this->_validator->isValid(array()));
329        $this->assertTrue($this->_validator->isValid(array('xxx')));
330        $this->assertFalse($this->_validator->isValid(null));
331    }
332
333    /**
334     * Ensures that the validator follows expected behavior
335     *
336     * @return void
337     */
338    public function testArrayConstantNotation()
339    {
340        $filter = new Zend_Validate_NotEmpty(
341            array(
342                'type' => array(
343                    Zend_Validate_NotEmpty::ZERO,
344                    Zend_Validate_NotEmpty::STRING,
345                    Zend_Validate_NotEmpty::BOOLEAN
346                )
347            )
348        );
349
350        $this->assertFalse($filter->isValid(false));
351        $this->assertTrue($filter->isValid(true));
352        $this->assertTrue($filter->isValid(0));
353        $this->assertTrue($filter->isValid(1));
354        $this->assertTrue($filter->isValid(0.0));
355        $this->assertTrue($filter->isValid(1.0));
356        $this->assertFalse($filter->isValid(''));
357        $this->assertTrue($filter->isValid('abc'));
358        $this->assertFalse($filter->isValid('0'));
359        $this->assertTrue($filter->isValid('1'));
360        $this->assertTrue($filter->isValid(array()));
361        $this->assertTrue($filter->isValid(array('xxx')));
362        $this->assertTrue($filter->isValid(null));
363    }
364
365    /**
366     * Ensures that the validator follows expected behavior
367     *
368     * @return void
369     */
370    public function testArrayConfigNotation()
371    {
372        $filter = new Zend_Validate_NotEmpty(
373            array(
374                'type' => array(
375                    Zend_Validate_NotEmpty::ZERO,
376                    Zend_Validate_NotEmpty::STRING,
377                    Zend_Validate_NotEmpty::BOOLEAN),
378                'test' => false
379            )
380        );
381
382        $this->assertFalse($filter->isValid(false));
383        $this->assertTrue($filter->isValid(true));
384        $this->assertTrue($filter->isValid(0));
385        $this->assertTrue($filter->isValid(1));
386        $this->assertTrue($filter->isValid(0.0));
387        $this->assertTrue($filter->isValid(1.0));
388        $this->assertFalse($filter->isValid(''));
389        $this->assertTrue($filter->isValid('abc'));
390        $this->assertFalse($filter->isValid('0'));
391        $this->assertTrue($filter->isValid('1'));
392        $this->assertTrue($filter->isValid(array()));
393        $this->assertTrue($filter->isValid(array('xxx')));
394        $this->assertTrue($filter->isValid(null));
395    }
396
397    /**
398     * Ensures that the validator follows expected behavior
399     *
400     * @return void
401     */
402    public function testMultiConstantNotation()
403    {
404        $filter = new Zend_Validate_NotEmpty(
405            Zend_Validate_NotEmpty::ZERO + Zend_Validate_NotEmpty::STRING + Zend_Validate_NotEmpty::BOOLEAN
406        );
407
408        $this->assertFalse($filter->isValid(false));
409        $this->assertTrue($filter->isValid(true));
410        $this->assertTrue($filter->isValid(0));
411        $this->assertTrue($filter->isValid(1));
412        $this->assertTrue($filter->isValid(0.0));
413        $this->assertTrue($filter->isValid(1.0));
414        $this->assertFalse($filter->isValid(''));
415        $this->assertTrue($filter->isValid('abc'));
416        $this->assertFalse($filter->isValid('0'));
417        $this->assertTrue($filter->isValid('1'));
418        $this->assertTrue($filter->isValid(array()));
419        $this->assertTrue($filter->isValid(array('xxx')));
420        $this->assertTrue($filter->isValid(null));
421    }
422
423    /**
424     * Ensures that the validator follows expected behavior
425     *
426     * @return void
427     */
428    public function testStringNotation()
429    {
430        $filter = new Zend_Validate_NotEmpty(
431            array(
432                'type' => array('zero', 'string', 'boolean')
433            )
434        );
435
436        $this->assertFalse($filter->isValid(false));
437        $this->assertTrue($filter->isValid(true));
438        $this->assertTrue($filter->isValid(0));
439        $this->assertTrue($filter->isValid(1));
440        $this->assertTrue($filter->isValid(0.0));
441        $this->assertTrue($filter->isValid(1.0));
442        $this->assertFalse($filter->isValid(''));
443        $this->assertTrue($filter->isValid('abc'));
444        $this->assertFalse($filter->isValid('0'));
445        $this->assertTrue($filter->isValid('1'));
446        $this->assertTrue($filter->isValid(array()));
447        $this->assertTrue($filter->isValid(array('xxx')));
448        $this->assertTrue($filter->isValid(null));
449    }
450
451    /**
452     * Ensures that the validator follows expected behavior
453     *
454     * @return void
455     */
456    public function testSingleStringNotation()
457    {
458        $filter = new Zend_Validate_NotEmpty(
459            'boolean'
460        );
461
462        $this->assertFalse($filter->isValid(false));
463        $this->assertTrue($filter->isValid(true));
464        $this->assertTrue($filter->isValid(0));
465        $this->assertTrue($filter->isValid(1));
466        $this->assertTrue($filter->isValid(0.0));
467        $this->assertTrue($filter->isValid(1.0));
468        $this->assertTrue($filter->isValid(''));
469        $this->assertTrue($filter->isValid('abc'));
470        $this->assertTrue($filter->isValid('0'));
471        $this->assertTrue($filter->isValid('1'));
472        $this->assertTrue($filter->isValid(array()));
473        $this->assertTrue($filter->isValid(array('xxx')));
474        $this->assertTrue($filter->isValid(null));
475    }
476
477    /**
478     * Ensures that the validator follows expected behavior
479     *
480     * @return void
481     */
482    public function testConfigObject()
483    {
484        require_once 'Zend/Config.php';
485        $options = array('type' => 'all');
486        $config  = new Zend_Config($options);
487
488        $filter = new Zend_Validate_NotEmpty(
489            $config
490        );
491
492        $this->assertFalse($filter->isValid(false));
493        $this->assertTrue($filter->isValid(true));
494        $this->assertFalse($filter->isValid(0));
495        $this->assertTrue($filter->isValid(1));
496        $this->assertFalse($filter->isValid(0.0));
497        $this->assertTrue($filter->isValid(1.0));
498        $this->assertFalse($filter->isValid(''));
499        $this->assertTrue($filter->isValid('abc'));
500        $this->assertFalse($filter->isValid('0'));
501        $this->assertTrue($filter->isValid('1'));
502        $this->assertFalse($filter->isValid(array()));
503        $this->assertTrue($filter->isValid(array('xxx')));
504        $this->assertFalse($filter->isValid(null));
505    }
506
507    /**
508     * Ensures that the validator follows expected behavior
509     *
510     * @return void
511     */
512    public function testSettingFalseType()
513    {
514        try {
515            $this->_validator->setType(true);
516            $this->fail();
517        } catch (Zend_Exception $e) {
518            $this->assertContains('Unknown', $e->getMessage());
519        }
520    }
521
522    /**
523     * Ensures that the validator follows expected behavior
524     *
525     * @return void
526     */
527    public function testGetType()
528    {
529        $this->assertEquals(493, $this->_validator->getType());
530    }
531
532    /**
533     * @group ZF-3236
534     */
535    public function testStringWithZeroShouldNotBeTreatedAsEmpty()
536    {
537        $this->assertTrue($this->_validator->isValid('0'));
538    }
539
540    /**
541     * Ensures that getMessages() returns expected default value
542     *
543     * @return void
544     */
545    public function testGetMessages()
546    {
547        $this->assertEquals(array(), $this->_validator->getMessages());
548    }
549
550    /**
551     * @ZF-4352
552     */
553    public function testNonStringValidation()
554    {
555        $v2 = new Zend_Validate_NotEmpty();
556        $this->assertTrue($this->_validator->isValid($v2));
557    }
558
559    /**
560     * @ZF-8767
561     *
562     * @return void
563     */
564    public function testZF8767()
565    {
566        $valid = new Zend_Validate_NotEmpty(Zend_Validate_NotEmpty::STRING);
567
568        $this->assertFalse($valid->isValid(''));
569        $messages = $valid->getMessages();
570        $this->assertTrue(array_key_exists('isEmpty', $messages));
571        $this->assertContains("can't be empty", $messages['isEmpty']);
572    }
573
574    /**
575     * @return void
576     */
577    public function testObjects()
578    {
579        $valid = new Zend_Validate_NotEmpty(Zend_Validate_NotEmpty::STRING);
580        $object = new ClassTest1();
581
582        $this->assertFalse($valid->isValid($object));
583
584        $valid = new Zend_Validate_NotEmpty(Zend_Validate_NotEmpty::OBJECT);
585        $this->assertTrue($valid->isValid($object));
586    }
587
588    /**
589     * @return void
590     */
591    public function testStringObjects()
592    {
593        $valid = new Zend_Validate_NotEmpty(Zend_Validate_NotEmpty::STRING);
594        $object = new ClassTest2();
595
596        $this->assertFalse($valid->isValid($object));
597
598        $valid = new Zend_Validate_NotEmpty(Zend_Validate_NotEmpty::OBJECT_STRING);
599        $this->assertTrue($valid->isValid($object));
600
601        $object = new ClassTest3();
602        $this->assertFalse($valid->isValid($object));
603    }
604}
605
606class ClassTest1 {}
607
608class ClassTest2
609{
610    public function __toString()
611    {
612        return 'Test';
613    }
614}
615
616class ClassTest3
617{
618    public function toString()
619    {
620        return '';
621    }
622}
623
624// Call Zend_Validate_NotEmptyTest::main() if this source file is executed directly.
625if (PHPUnit_MAIN_METHOD == "Zend_Validate_NotEmptyTest::main") {
626    Zend_Validate_NotEmptyTest::main();
627}