PageRenderTime 43ms CodeModel.GetById 10ms app.highlight 27ms RepoModel.GetById 1ms app.codeStats 1ms

/vendor/zendframework/zendframework/tests/ZendTest/Validator/NotEmptyTest.php

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