PageRenderTime 193ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/ZendTest/Validator/NotEmptyTest.php

https://bitbucket.org/gencer/zf2
PHP | 1006 lines | 627 code | 55 blank | 324 comment | 0 complexity | 22cbe9617077f55408d5f446ba4a7772 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-2014 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace ZendTest\Validator;
  10. use stdClass;
  11. use Zend\Validator\NotEmpty;
  12. /**
  13. * @group Zend_Validator
  14. */
  15. class NotEmptyTest extends \PHPUnit_Framework_TestCase
  16. {
  17. /**
  18. * @var NotEmpty
  19. */
  20. protected $validator;
  21. public function setUp()
  22. {
  23. $this->validator = new NotEmpty();
  24. }
  25. /**
  26. * Ensures that the validator follows expected behavior
  27. *
  28. * ZF-6708 introduces a change for validating integer 0; it is a valid
  29. * integer value. '0' is also valid.
  30. *
  31. * @param mixed $value Value to test
  32. * @param boolean $valid Expected validity of value
  33. *
  34. * @group ZF-6708
  35. * @return void
  36. * @dataProvider basicProvider
  37. */
  38. public function testBasic($value, $valid)
  39. {
  40. $this->checkValidationValue($value, $valid);
  41. }
  42. /**
  43. * Provides values and expected validity for the basic test
  44. *
  45. * @return array
  46. */
  47. public function basicProvider()
  48. {
  49. return array(
  50. array('word', true),
  51. array('', false),
  52. array(' ', false),
  53. array(' word ', true),
  54. array('0', true),
  55. array(1, true),
  56. array(0, true),
  57. array(true, true),
  58. array(false, false),
  59. array(null, false),
  60. array(array(), false),
  61. array(array(5), true),
  62. array(0.0, false),
  63. array(1.0, true),
  64. array(new stdClass(), true),
  65. );
  66. }
  67. /**
  68. * Ensures that the validator follows expected behavior
  69. *
  70. * @param mixed $value Value to test
  71. * @param boolean $valid Expected validity of value
  72. *
  73. * @return void
  74. *
  75. * @dataProvider booleanProvider
  76. */
  77. public function testOnlyBoolean($value, $valid)
  78. {
  79. $this->validator->setType(NotEmpty::BOOLEAN);
  80. $this->checkValidationValue($value, $valid);
  81. }
  82. /**
  83. * Provides values and their expected validity for boolean empty
  84. *
  85. * @return array
  86. */
  87. public function booleanProvider()
  88. {
  89. return array(
  90. array(false, false),
  91. array(true, true),
  92. array(0, true),
  93. array(1, true),
  94. array(0.0, true),
  95. array(1.0, true),
  96. array('', true),
  97. array('abc', true),
  98. array('0', true),
  99. array('1', true),
  100. array(array(), true),
  101. array(array('xxx'), true),
  102. array(null, true),
  103. );
  104. }
  105. /**
  106. * Ensures that the validator follows expected behavior
  107. *
  108. * @param mixed $value Value to test
  109. * @param boolean $valid Expected validity of value
  110. *
  111. * @return void
  112. *
  113. * @dataProvider integerOnlyProvider
  114. */
  115. public function testOnlyInteger($value, $valid)
  116. {
  117. $this->validator->setType(NotEmpty::INTEGER);
  118. $this->checkValidationValue($value, $valid);
  119. }
  120. /**
  121. * Provides values and their expected validity for when the validator is testing empty integer values
  122. *
  123. * @return array
  124. */
  125. public function integerOnlyProvider()
  126. {
  127. return array(
  128. array(false, true),
  129. array(true, true),
  130. array(0, false),
  131. array(1, true),
  132. array(0.0, true),
  133. array(1.0, true),
  134. array('', true),
  135. array('abc', true),
  136. array('0', true),
  137. array('1', true),
  138. array(array(), true),
  139. array(array('xxx'), true),
  140. array(null, true),
  141. );
  142. }
  143. /**
  144. * Ensures that the validator follows expected behavior
  145. *
  146. * @param mixed $value Value to test
  147. * @param boolean $valid Expected validity of value
  148. *
  149. * @return void
  150. *
  151. * @dataProvider floatOnlyProvider
  152. */
  153. public function testOnlyFloat($value, $valid)
  154. {
  155. $this->validator->setType(NotEmpty::FLOAT);
  156. $this->checkValidationValue($value, $valid);
  157. }
  158. /**
  159. * Provides values and their expected validity for boolean empty
  160. *
  161. * @return array
  162. */
  163. public function floatOnlyProvider()
  164. {
  165. return array(
  166. array(false, true),
  167. array(true, true),
  168. array(0, true),
  169. array(1, true),
  170. array(0.0, false),
  171. array(1.0, true),
  172. array('', true),
  173. array('abc', true),
  174. array('0', true),
  175. array('1', true),
  176. array(array(), true),
  177. array(array('xxx'), true),
  178. array(null, true),
  179. );
  180. }
  181. /**
  182. * Ensures that the validator follows expected behavior
  183. *
  184. * @param mixed $value Value to test
  185. * @param boolean $valid Expected validity of value
  186. *
  187. * @return void
  188. *
  189. * @dataProvider stringOnlyProvider
  190. */
  191. public function testOnlyString($value, $valid)
  192. {
  193. $this->validator->setType(NotEmpty::STRING);
  194. $this->checkValidationValue($value, $valid);
  195. }
  196. /**
  197. * Provides values and their expected validity for boolean empty
  198. *
  199. * @return array
  200. */
  201. public function stringOnlyProvider()
  202. {
  203. return array(
  204. array(false, true),
  205. array(true, true),
  206. array(0, true),
  207. array(1, true),
  208. array(0.0, true),
  209. array(1.0, true),
  210. array('', false),
  211. array('abc', true),
  212. array('0', true),
  213. array('1', true),
  214. array(array(), true),
  215. array(array('xxx'), true),
  216. array(null, true),
  217. );
  218. }
  219. /**
  220. * Ensures that the validator follows expected behavior
  221. *
  222. * @param mixed $value Value to test
  223. * @param boolean $valid Expected validity of value
  224. *
  225. * @return void
  226. *
  227. * @dataProvider zeroOnlyProvider
  228. */
  229. public function testOnlyZero($value, $valid)
  230. {
  231. $this->validator->setType(NotEmpty::ZERO);
  232. $this->checkValidationValue($value, $valid);
  233. }
  234. /**
  235. * Provides values and their expected validity for boolean empty
  236. *
  237. * @return array
  238. */
  239. public function zeroOnlyProvider()
  240. {
  241. return array(
  242. array(false, true),
  243. array(true, true),
  244. array(0, true),
  245. array(1, true),
  246. array(0.0, true),
  247. array(1.0, true),
  248. array('', true),
  249. array('abc', true),
  250. array('0', false),
  251. array('1', true),
  252. array(array(), true),
  253. array(array('xxx'), true),
  254. array(null, true),
  255. );
  256. }
  257. /**
  258. * Ensures that the validator follows expected behavior
  259. *
  260. * @param mixed $value Value to test
  261. * @param boolean $valid Expected validity of value
  262. *
  263. * @return void
  264. *
  265. * @dataProvider arrayOnlyProvider
  266. */
  267. public function testOnlyArray($value, $valid)
  268. {
  269. $this->validator->setType(NotEmpty::EMPTY_ARRAY);
  270. $this->checkValidationValue($value, $valid);
  271. }
  272. /**
  273. * Provides values and their expected validity for boolean empty
  274. *
  275. * @return array
  276. */
  277. public function arrayOnlyProvider()
  278. {
  279. return array(
  280. array(false, true),
  281. array(true, true),
  282. array(0, true),
  283. array(1, true),
  284. array(0.0, true),
  285. array(1.0, true),
  286. array('', true),
  287. array('abc', true),
  288. array('0', true),
  289. array('1', true),
  290. array(array(), false),
  291. array(array('xxx'), true),
  292. array(null, true),
  293. );
  294. }
  295. /**
  296. * Ensures that the validator follows expected behavior
  297. *
  298. * @param mixed $value Value to test
  299. * @param boolean $valid Expected validity of value
  300. *
  301. * @return void
  302. *
  303. * @dataProvider nullOnlyProvider
  304. */
  305. public function testOnlyNull($value, $valid)
  306. {
  307. $this->validator->setType(NotEmpty::NULL);
  308. $this->checkValidationValue($value, $valid);
  309. }
  310. /**
  311. * Provides values and their expected validity for boolean empty
  312. *
  313. * @return array
  314. */
  315. public function nullOnlyProvider()
  316. {
  317. return array(
  318. array(false, true),
  319. array(true, true),
  320. array(0, true),
  321. array(1, true),
  322. array(0.0, true),
  323. array(1.0, true),
  324. array('', true),
  325. array('abc', true),
  326. array('0', true),
  327. array('1', true),
  328. array(array(), true),
  329. array(array('xxx'), true),
  330. array(null, false),
  331. );
  332. }
  333. /**
  334. * Ensures that the validator follows expected behavior
  335. *
  336. * @param mixed $value Value to test
  337. * @param boolean $valid Expected validity of value
  338. *
  339. * @return void
  340. *
  341. * @dataProvider phpOnlyProvider
  342. */
  343. public function testOnlyPHP($value, $valid)
  344. {
  345. $this->validator->setType(NotEmpty::PHP);
  346. $this->checkValidationValue($value, $valid);
  347. }
  348. /**
  349. * Provides values and their expected validity for boolean empty
  350. *
  351. * @return array
  352. */
  353. public function phpOnlyProvider()
  354. {
  355. return array(
  356. array(false, false),
  357. array(true, true),
  358. array(0, false),
  359. array(1, true),
  360. array(0.0, false),
  361. array(1.0, true),
  362. array('', false),
  363. array('abc', true),
  364. array('0', false),
  365. array('1', true),
  366. array(array(), false),
  367. array(array('xxx'), true),
  368. array(null, false),
  369. );
  370. }
  371. /**
  372. * Ensures that the validator follows expected behavior
  373. *
  374. * @param mixed $value Value to test
  375. * @param boolean $valid Expected validity of value
  376. *
  377. * @return void
  378. *
  379. * @dataProvider spaceOnlyProvider
  380. */
  381. public function testOnlySpace($value, $valid)
  382. {
  383. $this->validator->setType(NotEmpty::SPACE);
  384. $this->checkValidationValue($value, $valid);
  385. }
  386. /**
  387. * Provides values and their expected validity for boolean empty
  388. *
  389. * @return array
  390. */
  391. public function spaceOnlyProvider()
  392. {
  393. return array(
  394. array(false, true),
  395. array(true, true),
  396. array(0, true),
  397. array(1, true),
  398. array(0.0, true),
  399. array(1.0, true),
  400. array('', true),
  401. array('abc', true),
  402. array('0', true),
  403. array('1', true),
  404. array(array(), true),
  405. array(array('xxx'), true),
  406. array(null, true),
  407. );
  408. }
  409. /**
  410. * Ensures that the validator follows expected behavior
  411. *
  412. * @param mixed $value Value to test
  413. * @param boolean $valid Expected validity of value
  414. *
  415. * @return void
  416. *
  417. * @dataProvider onlyAllProvider
  418. */
  419. public function testOnlyAll($value, $valid)
  420. {
  421. $this->validator->setType(NotEmpty::ALL);
  422. $this->checkValidationValue($value, $valid);
  423. }
  424. /**
  425. * Provides values and their expected validity for boolean empty
  426. *
  427. * @return array
  428. */
  429. public function onlyAllProvider()
  430. {
  431. return array(
  432. array(false, false),
  433. array(true, true),
  434. array(0, false),
  435. array(1, true),
  436. array(0.0, false),
  437. array(1.0, true),
  438. array('', false),
  439. array('abc', true),
  440. array('0', false),
  441. array('1', true),
  442. array(array(), false),
  443. array(array('xxx'), true),
  444. array(null, false),
  445. );
  446. }
  447. /**
  448. * Ensures that the validator follows expected behavior
  449. *
  450. * @param mixed $value Value to test
  451. * @param boolean $valid Expected validity of value
  452. *
  453. * @return void
  454. *
  455. * @dataProvider arrayConstantNotationProvider
  456. */
  457. public function testArrayConstantNotation($value, $valid)
  458. {
  459. $this->validator = new NotEmpty(
  460. array(
  461. 'type' => array(
  462. NotEmpty::ZERO,
  463. NotEmpty::STRING,
  464. NotEmpty::BOOLEAN
  465. )
  466. )
  467. );
  468. $this->checkValidationValue($value, $valid);
  469. }
  470. /**
  471. * Provides values and their expected validity for boolean empty
  472. *
  473. * @return array
  474. */
  475. public function arrayConstantNotationProvider()
  476. {
  477. return array(
  478. array(false, false),
  479. array(true, true),
  480. array(0, true),
  481. array(1, true),
  482. array(0.0, true),
  483. array(1.0, true),
  484. array('', false),
  485. array('abc', true),
  486. array('0', false),
  487. array('1', true),
  488. array(array(), true),
  489. array(array('xxx'), true),
  490. array(null, true),
  491. );
  492. }
  493. /**
  494. * Ensures that the validator follows expected behavior
  495. *
  496. * @param mixed $value Value to test
  497. * @param boolean $valid Expected validity of value
  498. *
  499. * @return void
  500. *
  501. * @dataProvider arrayConfigNotationProvider
  502. */
  503. public function testArrayConfigNotation($value, $valid)
  504. {
  505. $this->validator = new NotEmpty(
  506. array(
  507. 'type' => array(
  508. NotEmpty::ZERO,
  509. NotEmpty::STRING,
  510. NotEmpty::BOOLEAN),
  511. 'test' => false
  512. )
  513. );
  514. $this->checkValidationValue($value, $valid);
  515. }
  516. /**
  517. * Provides values and their expected validity for boolean empty
  518. *
  519. * @return array
  520. */
  521. public function arrayConfigNotationProvider()
  522. {
  523. return array(
  524. array(false, false),
  525. array(true, true),
  526. array(0, true),
  527. array(1, true),
  528. array(0.0, true),
  529. array(1.0, true),
  530. array('', false),
  531. array('abc', true),
  532. array('0', false),
  533. array('1', true),
  534. array(array(), true),
  535. array(array('xxx'), true),
  536. array(null, true),
  537. );
  538. }
  539. /**
  540. * Ensures that the validator follows expected behavior
  541. *
  542. * @param mixed $value Value to test
  543. * @param boolean $valid Expected validity of value
  544. *
  545. * @return void
  546. *
  547. * @dataProvider multiConstantNotationProvider
  548. */
  549. public function testMultiConstantNotation($value, $valid)
  550. {
  551. $this->validator = new NotEmpty(
  552. NotEmpty::ZERO + NotEmpty::STRING + NotEmpty::BOOLEAN
  553. );
  554. $this->checkValidationValue($value, $valid);
  555. }
  556. /**
  557. * Ensures that the validator follows expected behavior
  558. *
  559. * @param mixed $value Value to test
  560. * @param boolean $valid Expected validity of value
  561. *
  562. * @return void
  563. *
  564. * @dataProvider multiConstantNotationProvider
  565. */
  566. public function testMultiConstantBooleanOrNotation($value, $valid)
  567. {
  568. $this->validator = new NotEmpty(
  569. NotEmpty::ZERO | NotEmpty::STRING | NotEmpty::BOOLEAN
  570. );
  571. $this->checkValidationValue($value, $valid);
  572. }
  573. /**
  574. * Provides values and their expected validity for boolean empty
  575. *
  576. * @return array
  577. */
  578. public function multiConstantNotationProvider()
  579. {
  580. return array(
  581. array(false, false),
  582. array(true, true),
  583. array(0, true),
  584. array(1, true),
  585. array(0.0, true),
  586. array(1.0, true),
  587. array('', false),
  588. array('abc', true),
  589. array('0', false),
  590. array('1', true),
  591. array(array(), true),
  592. array(array('xxx'), true),
  593. array(null, true),
  594. );
  595. }
  596. /**
  597. * Ensures that the validator follows expected behavior
  598. *
  599. * @param mixed $value Value to test
  600. * @param boolean $valid Expected validity of value
  601. *
  602. * @return void
  603. *
  604. * @dataProvider stringNotationProvider
  605. */
  606. public function testStringNotation($value, $valid)
  607. {
  608. $this->validator = new NotEmpty(
  609. array(
  610. 'type' => array('zero', 'string', 'boolean')
  611. )
  612. );
  613. $this->checkValidationValue($value, $valid);
  614. }
  615. /**
  616. * Provides values and their expected validity for boolean empty
  617. *
  618. * @return array
  619. */
  620. public function stringNotationProvider()
  621. {
  622. return array(
  623. array(false, false),
  624. array(true, true),
  625. array(0, true),
  626. array(1, true),
  627. array(0.0, true),
  628. array(1.0, true),
  629. array('', false),
  630. array('abc', true),
  631. array('0', false),
  632. array('1', true),
  633. array(array(), true),
  634. array(array('xxx'), true),
  635. array(null, true),
  636. );
  637. }
  638. /**
  639. * Ensures that the validator follows expected behavior so if a string is specified more than once, it doesn't
  640. * cause different validations to run
  641. *
  642. * @param string $string Array of string type values
  643. * @param integer $expected Expected type setting value
  644. *
  645. * @return void
  646. *
  647. * @dataProvider duplicateStringSettingProvider
  648. */
  649. public function testStringNotationWithDuplicate($string, $expected)
  650. {
  651. $type = array($string, $string);
  652. $this->validator->setType($type);
  653. $this->assertEquals($expected, $this->validator->getType());
  654. }
  655. /**
  656. * Data provider for testStringNotationWithDuplicate method. Provides a string which will be duplicated. The test
  657. * ensures that setting a string value more than once only turns on the appropriate bit once
  658. *
  659. * @return array
  660. */
  661. public function duplicateStringSettingProvider()
  662. {
  663. return array(
  664. array('boolean', NotEmpty::BOOLEAN),
  665. array('integer', NotEmpty::INTEGER),
  666. array('float', NotEmpty::FLOAT),
  667. array('string', NotEmpty::STRING),
  668. array('zero', NotEmpty::ZERO),
  669. array('array', NotEmpty::EMPTY_ARRAY),
  670. array('null', NotEmpty::NULL),
  671. array('php', NotEmpty::PHP),
  672. array('space', NotEmpty::SPACE),
  673. array('object', NotEmpty::OBJECT),
  674. array('objectstring', NotEmpty::OBJECT_STRING),
  675. array('objectcount', NotEmpty::OBJECT_COUNT),
  676. array('all', NotEmpty::ALL),
  677. );
  678. }
  679. /**
  680. * Ensures that the validator follows expected behavior
  681. *
  682. * @param mixed $value Value to test
  683. * @param boolean $valid Expected validity of value
  684. *
  685. * @return void
  686. *
  687. * @dataProvider singleStringNotationProvider
  688. */
  689. public function testSingleStringNotation($value, $valid)
  690. {
  691. $this->validator = new NotEmpty(
  692. 'boolean'
  693. );
  694. $this->checkValidationValue($value, $valid);
  695. }
  696. /**
  697. * Provides values and their expected validity for boolean empty
  698. *
  699. * @return array
  700. */
  701. public function singleStringNotationProvider()
  702. {
  703. return array(
  704. array(false, false),
  705. array(true, true),
  706. array(0, true),
  707. array(1, true),
  708. array(0.0, true),
  709. array(1.0, true),
  710. array('', true),
  711. array('abc', true),
  712. array('0', true),
  713. array('1', true),
  714. array(array(), true),
  715. array(array('xxx'), true),
  716. array(null, true),
  717. );
  718. }
  719. /**
  720. * Ensures that the validator follows expected behavior
  721. *
  722. * @param mixed $value Value to test
  723. * @param boolean $valid Expected validity of value
  724. *
  725. * @return void
  726. *
  727. * @dataProvider configObjectProvider
  728. */
  729. public function testConfigObject($value, $valid)
  730. {
  731. $options = array('type' => 'all');
  732. $config = new \Zend\Config\Config($options);
  733. $this->validator = new NotEmpty(
  734. $config
  735. );
  736. $this->checkValidationValue($value, $valid);
  737. }
  738. /**
  739. * Provides values and their expected validity for boolean empty
  740. *
  741. * @return array
  742. */
  743. public function configObjectProvider()
  744. {
  745. return array(
  746. array(false, false),
  747. array(true, true),
  748. array(0, false),
  749. array(1, true),
  750. array(0.0, false),
  751. array(1.0, true),
  752. array('', false),
  753. array('abc', true),
  754. array('0', false),
  755. array('1', true),
  756. array(array(), false),
  757. array(array('xxx'), true),
  758. array(null, false),
  759. );
  760. }
  761. /**
  762. * Ensures that the validator follows expected behavior
  763. *
  764. * @return void
  765. */
  766. public function testSettingFalseType()
  767. {
  768. $this->setExpectedException('Zend\Validator\Exception\InvalidArgumentException', 'Unknown');
  769. $this->validator->setType(true);
  770. }
  771. /**
  772. * Ensures that the validator follows expected behavior
  773. *
  774. * @return void
  775. */
  776. public function testGetType()
  777. {
  778. $this->assertEquals($this->validator->getDefaultType(), $this->validator->getType());
  779. }
  780. /**
  781. * @group ZF-3236
  782. */
  783. public function testStringWithZeroShouldNotBeTreatedAsEmpty()
  784. {
  785. $this->assertTrue($this->validator->isValid('0'));
  786. }
  787. /**
  788. * Ensures that getMessages() returns expected default value
  789. *
  790. * @return void
  791. */
  792. public function testGetMessages()
  793. {
  794. $this->assertEquals(array(), $this->validator->getMessages());
  795. }
  796. /**
  797. * @ZF-4352
  798. */
  799. public function testNonStringValidation()
  800. {
  801. $v2 = new NotEmpty();
  802. $this->assertTrue($this->validator->isValid($v2));
  803. }
  804. /**
  805. * @ZF-8767
  806. *
  807. * @return void
  808. */
  809. public function testZF8767()
  810. {
  811. $valid = new NotEmpty(NotEmpty::STRING);
  812. $this->assertFalse($valid->isValid(''));
  813. $messages = $valid->getMessages();
  814. $this->assertTrue(array_key_exists('isEmpty', $messages));
  815. $this->assertContains("can't be empty", $messages['isEmpty']);
  816. }
  817. /**
  818. * @return void
  819. */
  820. public function testObjects()
  821. {
  822. $valid = new NotEmpty(NotEmpty::STRING);
  823. $object = new stdClass();
  824. $this->assertFalse($valid->isValid($object));
  825. $valid = new NotEmpty(NotEmpty::OBJECT);
  826. $this->assertTrue($valid->isValid($object));
  827. }
  828. /**
  829. * @return void
  830. */
  831. public function testStringObjects()
  832. {
  833. $valid = new NotEmpty(NotEmpty::STRING);
  834. $object = $this->getMockBuilder('\stdClass')
  835. ->setMethods(array('__toString'))
  836. ->getMock();
  837. $object->expects($this->atLeastOnce())
  838. ->method('__toString')
  839. ->will($this->returnValue('Test'));
  840. $this->assertFalse($valid->isValid($object));
  841. $valid = new NotEmpty(NotEmpty::OBJECT_STRING);
  842. $this->assertTrue($valid->isValid($object));
  843. $object = $this->getMockBuilder('\stdClass')
  844. ->setMethods(array('__toString'))
  845. ->getMock();
  846. $object->expects($this->atLeastOnce())
  847. ->method('__toString')
  848. ->will($this->returnValue(''));
  849. $this->assertFalse($valid->isValid($object));
  850. }
  851. /**
  852. * @group ZF-11566
  853. *
  854. * @param mixed $value Value to test
  855. * @param boolean $valid Expected validity of value
  856. *
  857. * @dataProvider arrayConfigNotationWithoutKeyProvider
  858. */
  859. public function testArrayConfigNotationWithoutKey($value, $valid)
  860. {
  861. $this->validator = new NotEmpty(
  862. array('zero', 'string', 'boolean')
  863. );
  864. $this->checkValidationValue($value, $valid);
  865. }
  866. /**
  867. * Provides values and their expected validity for boolean empty
  868. *
  869. * @return array
  870. */
  871. public function arrayConfigNotationWithoutKeyProvider()
  872. {
  873. return array(
  874. array(false, false),
  875. array(true, true),
  876. array(0, true),
  877. array(1, true),
  878. array(0.0, true),
  879. array(1.0, true),
  880. array('', false),
  881. array('abc', true),
  882. array('0', false),
  883. array('1', true),
  884. array(array(), true),
  885. array(array('xxx'), true),
  886. array(null, true),
  887. );
  888. }
  889. public function testEqualsMessageTemplates()
  890. {
  891. $validator = $this->validator;
  892. $this->assertAttributeEquals(
  893. $validator->getOption('messageTemplates'),
  894. 'messageTemplates',
  895. $validator
  896. );
  897. }
  898. public function testTypeAutoDetectionHasNoSideEffect()
  899. {
  900. $validator = new NotEmpty(array('translatorEnabled' => true));
  901. $this->assertEquals($validator->getDefaultType(), $validator->getType());
  902. }
  903. public function testDefaultType()
  904. {
  905. $this->assertSame(
  906. NotEmpty::BOOLEAN
  907. | NotEmpty::FLOAT
  908. | NotEmpty::STRING
  909. | NotEmpty::EMPTY_ARRAY
  910. | NotEmpty::NULL
  911. | NotEmpty::SPACE
  912. | NotEmpty::OBJECT,
  913. $this->validator->getDefaultType()
  914. );
  915. }
  916. /**
  917. * Checks that the validation value matches the expected validity
  918. *
  919. * @param mixed $value Value to validate
  920. * @param bool $valid Expected validity
  921. *
  922. * @return void
  923. */
  924. protected function checkValidationValue($value, $valid)
  925. {
  926. $isValid = $this->validator->isValid($value);
  927. if ($valid) {
  928. $this->assertTrue($isValid);
  929. } else {
  930. $this->assertFalse($isValid);
  931. }
  932. }
  933. }