PageRenderTime 55ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Zend/Filter/BooleanTest.php

https://github.com/matthewfitz/zf2
PHP | 798 lines | 583 code | 42 blank | 173 comment | 0 complexity | edd9cf82ed8ca2958860f7cd3ae17c5e MD5 | raw file
Possible License(s): BSD-3-Clause
  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_Filter
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. namespace ZendTest\Filter;
  23. use Zend\Filter\Boolean as BooleanFilter,
  24. Zend\Locale\Locale;
  25. /**
  26. * @category Zend
  27. * @package Zend_Filter
  28. * @subpackage UnitTests
  29. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. * @group Zend_Filter
  32. */
  33. class BooleanTest extends \PHPUnit_Framework_TestCase
  34. {
  35. /**
  36. * Zend_Filter_Boolean object
  37. *
  38. * @var Zend_Filter_Boolean
  39. */
  40. protected $_filter;
  41. /**
  42. * Creates a new Zend_Filter_Boolean object for each test method
  43. *
  44. * @return void
  45. */
  46. public function setUp()
  47. {
  48. $this->_filter = new BooleanFilter();
  49. }
  50. /**
  51. * Ensures that the filter follows expected behavior
  52. *
  53. * @return void
  54. */
  55. public function testBasic()
  56. {
  57. $filter = $this->_filter;
  58. $this->assertFalse($filter(false));
  59. $this->assertTrue($filter(true));
  60. $this->assertFalse($filter(0));
  61. $this->assertTrue($filter(1));
  62. $this->assertFalse($filter(0.0));
  63. $this->assertTrue($filter(1.0));
  64. $this->assertFalse($filter(''));
  65. $this->assertTrue($filter('abc'));
  66. $this->assertFalse($filter('0'));
  67. $this->assertTrue($filter('1'));
  68. $this->assertFalse($filter(array()));
  69. $this->assertTrue($filter(array('xxx')));
  70. $this->assertFalse($filter(null));
  71. $this->assertTrue($filter('false'));
  72. $this->assertTrue($filter('true'));
  73. $this->assertTrue($filter('no'));
  74. $this->assertTrue($filter('yes'));
  75. }
  76. /**
  77. * Ensures that the filter follows expected behavior
  78. *
  79. * @return void
  80. */
  81. public function testOnlyBoolean()
  82. {
  83. $filter = $this->_filter;
  84. $filter->setType(BooleanFilter::BOOLEAN);
  85. $this->assertFalse($filter(false));
  86. $this->assertTrue($filter(true));
  87. $this->assertTrue($filter(0));
  88. $this->assertTrue($filter(1));
  89. $this->assertTrue($filter(0.0));
  90. $this->assertTrue($filter(1.0));
  91. $this->assertTrue($filter(''));
  92. $this->assertTrue($filter('abc'));
  93. $this->assertTrue($filter('0'));
  94. $this->assertTrue($filter('1'));
  95. $this->assertTrue($filter(array()));
  96. $this->assertTrue($filter(array('xxx')));
  97. $this->assertTrue($filter(null));
  98. $this->assertTrue($filter('false'));
  99. $this->assertTrue($filter('true'));
  100. $this->assertTrue($filter('no'));
  101. $this->assertTrue($filter('yes'));
  102. }
  103. /**
  104. * Ensures that the filter follows expected behavior
  105. *
  106. * @return void
  107. */
  108. public function testOnlyInteger()
  109. {
  110. $filter = $this->_filter;
  111. $filter->setType(BooleanFilter::INTEGER);
  112. $this->assertTrue($filter(false));
  113. $this->assertTrue($filter(true));
  114. $this->assertFalse($filter(0));
  115. $this->assertTrue($filter(1));
  116. $this->assertTrue($filter(0.0));
  117. $this->assertTrue($filter(1.0));
  118. $this->assertTrue($filter(''));
  119. $this->assertTrue($filter('abc'));
  120. $this->assertTrue($filter('0'));
  121. $this->assertTrue($filter('1'));
  122. $this->assertTrue($filter(array()));
  123. $this->assertTrue($filter(array('xxx')));
  124. $this->assertTrue($filter(null));
  125. $this->assertTrue($filter('false'));
  126. $this->assertTrue($filter('true'));
  127. $this->assertTrue($filter('no'));
  128. $this->assertTrue($filter('yes'));
  129. }
  130. /**
  131. * Ensures that the filter follows expected behavior
  132. *
  133. * @return void
  134. */
  135. public function testOnlyFloat()
  136. {
  137. $filter = $this->_filter;
  138. $filter->setType(BooleanFilter::FLOAT);
  139. $this->assertTrue($filter(false));
  140. $this->assertTrue($filter(true));
  141. $this->assertTrue($filter(0));
  142. $this->assertTrue($filter(1));
  143. $this->assertFalse($filter(0.0));
  144. $this->assertTrue($filter(1.0));
  145. $this->assertTrue($filter(''));
  146. $this->assertTrue($filter('abc'));
  147. $this->assertTrue($filter('0'));
  148. $this->assertTrue($filter('1'));
  149. $this->assertTrue($filter(array()));
  150. $this->assertTrue($filter(array('xxx')));
  151. $this->assertTrue($filter(null));
  152. $this->assertTrue($filter('false'));
  153. $this->assertTrue($filter('true'));
  154. $this->assertTrue($filter('no'));
  155. $this->assertTrue($filter('yes'));
  156. }
  157. /**
  158. * Ensures that the filter follows expected behavior
  159. *
  160. * @return void
  161. */
  162. public function testOnlyString()
  163. {
  164. $filter = $this->_filter;
  165. $filter->setType(BooleanFilter::STRING);
  166. $this->assertTrue($filter(false));
  167. $this->assertTrue($filter(true));
  168. $this->assertTrue($filter(0));
  169. $this->assertTrue($filter(1));
  170. $this->assertTrue($filter(0.0));
  171. $this->assertTrue($filter(1.0));
  172. $this->assertFalse($filter(''));
  173. $this->assertTrue($filter('abc'));
  174. $this->assertTrue($filter('0'));
  175. $this->assertTrue($filter('1'));
  176. $this->assertTrue($filter(array()));
  177. $this->assertTrue($filter(array('xxx')));
  178. $this->assertTrue($filter(null));
  179. $this->assertTrue($filter('false'));
  180. $this->assertTrue($filter('true'));
  181. $this->assertTrue($filter('no'));
  182. $this->assertTrue($filter('yes'));
  183. }
  184. /**
  185. * Ensures that the filter follows expected behavior
  186. *
  187. * @return void
  188. */
  189. public function testOnlyZero()
  190. {
  191. $filter = $this->_filter;
  192. $filter->setType(BooleanFilter::ZERO);
  193. $this->assertTrue($filter(false));
  194. $this->assertTrue($filter(true));
  195. $this->assertTrue($filter(0));
  196. $this->assertTrue($filter(1));
  197. $this->assertTrue($filter(0.0));
  198. $this->assertTrue($filter(1.0));
  199. $this->assertTrue($filter(''));
  200. $this->assertTrue($filter('abc'));
  201. $this->assertFalse($filter('0'));
  202. $this->assertTrue($filter('1'));
  203. $this->assertTrue($filter(array()));
  204. $this->assertTrue($filter(array('xxx')));
  205. $this->assertTrue($filter(null));
  206. $this->assertTrue($filter('false'));
  207. $this->assertTrue($filter('true'));
  208. $this->assertTrue($filter('no'));
  209. $this->assertTrue($filter('yes'));
  210. }
  211. /**
  212. * Ensures that the filter follows expected behavior
  213. *
  214. * @return void
  215. */
  216. public function testOnlyArray()
  217. {
  218. $filter = $this->_filter;
  219. $filter->setType(BooleanFilter::EMPTY_ARRAY);
  220. $this->assertTrue($filter(false));
  221. $this->assertTrue($filter(true));
  222. $this->assertTrue($filter(0));
  223. $this->assertTrue($filter(1));
  224. $this->assertTrue($filter(0.0));
  225. $this->assertTrue($filter(1.0));
  226. $this->assertTrue($filter(''));
  227. $this->assertTrue($filter('abc'));
  228. $this->assertTrue($filter('0'));
  229. $this->assertTrue($filter('1'));
  230. $this->assertFalse($filter(array()));
  231. $this->assertTrue($filter(array('xxx')));
  232. $this->assertTrue($filter(null));
  233. $this->assertTrue($filter('false'));
  234. $this->assertTrue($filter('true'));
  235. $this->assertTrue($filter('no'));
  236. $this->assertTrue($filter('yes'));
  237. }
  238. /**
  239. * Ensures that the filter follows expected behavior
  240. *
  241. * @return void
  242. */
  243. public function testOnlyNull()
  244. {
  245. $filter = $this->_filter;
  246. $filter->setType(BooleanFilter::NULL);
  247. $this->assertTrue($filter(false));
  248. $this->assertTrue($filter(true));
  249. $this->assertTrue($filter(0));
  250. $this->assertTrue($filter(1));
  251. $this->assertTrue($filter(0.0));
  252. $this->assertTrue($filter(1.0));
  253. $this->assertTrue($filter(''));
  254. $this->assertTrue($filter('abc'));
  255. $this->assertTrue($filter('0'));
  256. $this->assertTrue($filter('1'));
  257. $this->assertTrue($filter(array()));
  258. $this->assertTrue($filter(array('xxx')));
  259. $this->assertFalse($filter(null));
  260. $this->assertTrue($filter('false'));
  261. $this->assertTrue($filter('true'));
  262. $this->assertTrue($filter('no'));
  263. $this->assertTrue($filter('yes'));
  264. }
  265. /**
  266. * Ensures that the filter follows expected behavior
  267. *
  268. * @return void
  269. */
  270. public function testOnlyPHP()
  271. {
  272. $filter = $this->_filter;
  273. $filter->setType(BooleanFilter::PHP);
  274. $this->assertFalse($filter(false));
  275. $this->assertTrue($filter(true));
  276. $this->assertFalse($filter(0));
  277. $this->assertTrue($filter(1));
  278. $this->assertFalse($filter(0.0));
  279. $this->assertTrue($filter(1.0));
  280. $this->assertFalse($filter(''));
  281. $this->assertTrue($filter('abc'));
  282. $this->assertFalse($filter('0'));
  283. $this->assertTrue($filter('1'));
  284. $this->assertFalse($filter(array()));
  285. $this->assertTrue($filter(array('xxx')));
  286. $this->assertFalse($filter(null));
  287. $this->assertTrue($filter('false'));
  288. $this->assertTrue($filter('true'));
  289. $this->assertTrue($filter('no'));
  290. $this->assertTrue($filter('yes'));
  291. }
  292. /**
  293. * Ensures that the filter follows expected behavior
  294. *
  295. * @return void
  296. */
  297. public function testOnlyFalseString()
  298. {
  299. $filter = $this->_filter;
  300. $filter->setType(BooleanFilter::FALSE_STRING);
  301. $this->assertTrue($filter(false));
  302. $this->assertTrue($filter(true));
  303. $this->assertTrue($filter(0));
  304. $this->assertTrue($filter(1));
  305. $this->assertTrue($filter(0.0));
  306. $this->assertTrue($filter(1.0));
  307. $this->assertTrue($filter(''));
  308. $this->assertTrue($filter('abc'));
  309. $this->assertTrue($filter('0'));
  310. $this->assertTrue($filter('1'));
  311. $this->assertTrue($filter(array()));
  312. $this->assertTrue($filter(array('xxx')));
  313. $this->assertTrue($filter(null));
  314. $this->assertFalse($filter('false'));
  315. $this->assertTrue($filter('true'));
  316. $this->assertTrue($filter('no'));
  317. $this->assertTrue($filter('yes'));
  318. }
  319. /**
  320. * Ensures that the filter follows expected behavior
  321. *
  322. * @return void
  323. */
  324. public function testOnlyYes()
  325. {
  326. $filter = $this->_filter;
  327. $filter->setType(BooleanFilter::YES);
  328. $filter->setLocale('en');
  329. $this->assertTrue($filter(false));
  330. $this->assertTrue($filter(true));
  331. $this->assertTrue($filter(0));
  332. $this->assertTrue($filter(1));
  333. $this->assertTrue($filter(0.0));
  334. $this->assertTrue($filter(1.0));
  335. $this->assertTrue($filter(''));
  336. $this->assertTrue($filter('abc'));
  337. $this->assertTrue($filter('0'));
  338. $this->assertTrue($filter('1'));
  339. $this->assertTrue($filter(array()));
  340. $this->assertTrue($filter(array('xxx')));
  341. $this->assertTrue($filter(null));
  342. $this->assertTrue($filter('false'));
  343. $this->assertTrue($filter('true'));
  344. $this->assertFalse($filter('no'));
  345. $this->assertTrue($filter('yes'));
  346. }
  347. /**
  348. * Ensures that the filter follows expected behavior
  349. *
  350. * @return void
  351. */
  352. public function testOnlyAll()
  353. {
  354. $filter = $this->_filter;
  355. $filter->setType(BooleanFilter::ALL);
  356. $filter->setLocale('en');
  357. $this->assertFalse($filter(false));
  358. $this->assertTrue($filter(true));
  359. $this->assertFalse($filter(0));
  360. $this->assertTrue($filter(1));
  361. $this->assertFalse($filter(0.0));
  362. $this->assertTrue($filter(1.0));
  363. $this->assertFalse($filter(''));
  364. $this->assertTrue($filter('abc'));
  365. $this->assertFalse($filter('0'));
  366. $this->assertTrue($filter('1'));
  367. $this->assertFalse($filter(array()));
  368. $this->assertTrue($filter(array('xxx')));
  369. $this->assertFalse($filter(null));
  370. $this->assertFalse($filter('false'));
  371. $this->assertTrue($filter('true'));
  372. $this->assertFalse($filter('no'));
  373. $this->assertTrue($filter('yes'));
  374. }
  375. /**
  376. * Ensures that the filter follows expected behavior
  377. *
  378. * @return void
  379. */
  380. public function testArrayConstantNotation()
  381. {
  382. $filter = new BooleanFilter(
  383. array(
  384. 'type' => array(
  385. BooleanFilter::ZERO,
  386. BooleanFilter::STRING,
  387. BooleanFilter::BOOLEAN,
  388. ),
  389. )
  390. );
  391. $this->assertFalse($filter(false));
  392. $this->assertTrue($filter(true));
  393. $this->assertTrue($filter(0));
  394. $this->assertTrue($filter(1));
  395. $this->assertTrue($filter(0.0));
  396. $this->assertTrue($filter(1.0));
  397. $this->assertFalse($filter(''));
  398. $this->assertTrue($filter('abc'));
  399. $this->assertFalse($filter('0'));
  400. $this->assertTrue($filter('1'));
  401. $this->assertTrue($filter(array()));
  402. $this->assertTrue($filter(array('xxx')));
  403. $this->assertTrue($filter(null));
  404. $this->assertTrue($filter('false'));
  405. $this->assertTrue($filter('true'));
  406. $this->assertTrue($filter('no'));
  407. $this->assertTrue($filter('yes'));
  408. }
  409. /**
  410. * Ensures that the filter follows expected behavior
  411. *
  412. * @return void
  413. */
  414. public function testArrayConfigNotation()
  415. {
  416. $filter = new BooleanFilter(
  417. array(
  418. 'type' => array(
  419. BooleanFilter::ZERO,
  420. BooleanFilter::STRING,
  421. BooleanFilter::BOOLEAN,
  422. ),
  423. 'test' => false,
  424. )
  425. );
  426. $this->assertFalse($filter(false));
  427. $this->assertTrue($filter(true));
  428. $this->assertTrue($filter(0));
  429. $this->assertTrue($filter(1));
  430. $this->assertTrue($filter(0.0));
  431. $this->assertTrue($filter(1.0));
  432. $this->assertFalse($filter(''));
  433. $this->assertTrue($filter('abc'));
  434. $this->assertFalse($filter('0'));
  435. $this->assertTrue($filter('1'));
  436. $this->assertTrue($filter(array()));
  437. $this->assertTrue($filter(array('xxx')));
  438. $this->assertTrue($filter(null));
  439. $this->assertTrue($filter('false'));
  440. $this->assertTrue($filter('true'));
  441. $this->assertTrue($filter('no'));
  442. $this->assertTrue($filter('yes'));
  443. }
  444. /**
  445. * Ensures that the filter follows expected behavior
  446. *
  447. * @return void
  448. */
  449. public function testMultiConstantNotation()
  450. {
  451. $filter = new BooleanFilter(
  452. BooleanFilter::ZERO + BooleanFilter::STRING + BooleanFilter::BOOLEAN
  453. );
  454. $this->assertFalse($filter(false));
  455. $this->assertTrue($filter(true));
  456. $this->assertTrue($filter(0));
  457. $this->assertTrue($filter(1));
  458. $this->assertTrue($filter(0.0));
  459. $this->assertTrue($filter(1.0));
  460. $this->assertFalse($filter(''));
  461. $this->assertTrue($filter('abc'));
  462. $this->assertFalse($filter('0'));
  463. $this->assertTrue($filter('1'));
  464. $this->assertTrue($filter(array()));
  465. $this->assertTrue($filter(array('xxx')));
  466. $this->assertTrue($filter(null));
  467. $this->assertTrue($filter('false'));
  468. $this->assertTrue($filter('true'));
  469. $this->assertTrue($filter('no'));
  470. $this->assertTrue($filter('yes'));
  471. }
  472. /**
  473. * Ensures that the filter follows expected behavior
  474. *
  475. * @return void
  476. */
  477. public function testStringNotation()
  478. {
  479. $filter = new BooleanFilter(
  480. array(
  481. 'type' => array('zero', 'string', 'boolean')
  482. )
  483. );
  484. $this->assertFalse($filter(false));
  485. $this->assertTrue($filter(true));
  486. $this->assertTrue($filter(0));
  487. $this->assertTrue($filter(1));
  488. $this->assertTrue($filter(0.0));
  489. $this->assertTrue($filter(1.0));
  490. $this->assertFalse($filter(''));
  491. $this->assertTrue($filter('abc'));
  492. $this->assertFalse($filter('0'));
  493. $this->assertTrue($filter('1'));
  494. $this->assertTrue($filter(array()));
  495. $this->assertTrue($filter(array('xxx')));
  496. $this->assertTrue($filter(null));
  497. $this->assertTrue($filter('false'));
  498. $this->assertTrue($filter('true'));
  499. $this->assertTrue($filter('no'));
  500. $this->assertTrue($filter('yes'));
  501. }
  502. /**
  503. * Ensures that the filter follows expected behavior
  504. *
  505. * @return void
  506. */
  507. public function testSingleStringNotation()
  508. {
  509. $filter = new BooleanFilter(
  510. 'boolean'
  511. );
  512. $this->assertFalse($filter(false));
  513. $this->assertTrue($filter(true));
  514. $this->assertTrue($filter(0));
  515. $this->assertTrue($filter(1));
  516. $this->assertTrue($filter(0.0));
  517. $this->assertTrue($filter(1.0));
  518. $this->assertTrue($filter(''));
  519. $this->assertTrue($filter('abc'));
  520. $this->assertTrue($filter('0'));
  521. $this->assertTrue($filter('1'));
  522. $this->assertTrue($filter(array()));
  523. $this->assertTrue($filter(array('xxx')));
  524. $this->assertTrue($filter(null));
  525. $this->assertTrue($filter('false'));
  526. $this->assertTrue($filter('true'));
  527. $this->assertTrue($filter('no'));
  528. $this->assertTrue($filter('yes'));
  529. }
  530. /**
  531. * Ensures that the filter follows expected behavior
  532. *
  533. * @return void
  534. */
  535. public function testSettingLocale()
  536. {
  537. $filter = $this->_filter;
  538. $filter->setType(BooleanFilter::ALL);
  539. $filter->setLocale('de');
  540. $this->assertFalse($filter(false));
  541. $this->assertTrue($filter(true));
  542. $this->assertFalse($filter(0));
  543. $this->assertTrue($filter(1));
  544. $this->assertFalse($filter(0.0));
  545. $this->assertTrue($filter(1.0));
  546. $this->assertFalse($filter(''));
  547. $this->assertTrue($filter('abc'));
  548. $this->assertFalse($filter('0'));
  549. $this->assertTrue($filter('1'));
  550. $this->assertFalse($filter(array()));
  551. $this->assertTrue($filter(array('xxx')));
  552. $this->assertFalse($filter(null));
  553. $this->assertFalse($filter('false'));
  554. $this->assertTrue($filter('true'));
  555. $this->assertTrue($filter('no'));
  556. $this->assertTrue($filter('yes'));
  557. $this->assertFalse($filter('nein'));
  558. $this->assertTrue($filter('ja'));
  559. }
  560. /**
  561. * Ensures that the filter follows expected behavior
  562. *
  563. * @return void
  564. */
  565. public function testSettingLocalePerConstructorString()
  566. {
  567. $filter = new BooleanFilter(
  568. 'all', true, 'de'
  569. );
  570. $this->assertFalse($filter(false));
  571. $this->assertTrue($filter(true));
  572. $this->assertFalse($filter(0));
  573. $this->assertTrue($filter(1));
  574. $this->assertFalse($filter(0.0));
  575. $this->assertTrue($filter(1.0));
  576. $this->assertFalse($filter(''));
  577. $this->assertTrue($filter('abc'));
  578. $this->assertFalse($filter('0'));
  579. $this->assertTrue($filter('1'));
  580. $this->assertFalse($filter(array()));
  581. $this->assertTrue($filter(array('xxx')));
  582. $this->assertFalse($filter(null));
  583. $this->assertFalse($filter('false'));
  584. $this->assertTrue($filter('true'));
  585. $this->assertTrue($filter('no'));
  586. $this->assertTrue($filter('yes'));
  587. $this->assertFalse($filter('nein'));
  588. $this->assertTrue($filter('ja'));
  589. }
  590. /**
  591. * Ensures that the filter follows expected behavior
  592. *
  593. * @return void
  594. */
  595. public function testConfigObject()
  596. {
  597. $options = array('type' => 'all', 'locale' => 'de');
  598. $config = new \Zend\Config\Config($options);
  599. $filter = new BooleanFilter(
  600. $config
  601. );
  602. $this->assertFalse($filter(false));
  603. $this->assertTrue($filter(true));
  604. $this->assertFalse($filter(0));
  605. $this->assertTrue($filter(1));
  606. $this->assertFalse($filter(0.0));
  607. $this->assertTrue($filter(1.0));
  608. $this->assertFalse($filter(''));
  609. $this->assertTrue($filter('abc'));
  610. $this->assertFalse($filter('0'));
  611. $this->assertTrue($filter('1'));
  612. $this->assertFalse($filter(array()));
  613. $this->assertTrue($filter(array('xxx')));
  614. $this->assertFalse($filter(null));
  615. $this->assertFalse($filter('false'));
  616. $this->assertTrue($filter('true'));
  617. $this->assertTrue($filter('no'));
  618. $this->assertTrue($filter('yes'));
  619. $this->assertFalse($filter('nein'));
  620. $this->assertTrue($filter('ja'));
  621. }
  622. /**
  623. * Ensures that the filter follows expected behavior
  624. *
  625. * @return void
  626. */
  627. public function testSettingLocalePerConstructorArray()
  628. {
  629. $filter = new BooleanFilter(
  630. array('type' => 'all', 'locale' => 'de')
  631. );
  632. $this->assertFalse($filter(false));
  633. $this->assertTrue($filter(true));
  634. $this->assertFalse($filter(0));
  635. $this->assertTrue($filter(1));
  636. $this->assertFalse($filter(0.0));
  637. $this->assertTrue($filter(1.0));
  638. $this->assertFalse($filter(''));
  639. $this->assertTrue($filter('abc'));
  640. $this->assertFalse($filter('0'));
  641. $this->assertTrue($filter('1'));
  642. $this->assertFalse($filter(array()));
  643. $this->assertTrue($filter(array('xxx')));
  644. $this->assertFalse($filter(null));
  645. $this->assertFalse($filter('false'));
  646. $this->assertTrue($filter('true'));
  647. $this->assertTrue($filter('no'));
  648. $this->assertTrue($filter('yes'));
  649. $this->assertFalse($filter('nein'));
  650. $this->assertTrue($filter('ja'));
  651. }
  652. /**
  653. * Ensures that the filter follows expected behavior
  654. *
  655. * @return void
  656. */
  657. public function testSettingLocaleInstance()
  658. {
  659. $locale = new Locale('de');
  660. $filter = new BooleanFilter(
  661. array('type' => 'all', 'locale' => $locale)
  662. );
  663. $this->assertFalse($filter(false));
  664. $this->assertTrue($filter(true));
  665. $this->assertFalse($filter(0));
  666. $this->assertTrue($filter(1));
  667. $this->assertFalse($filter(0.0));
  668. $this->assertTrue($filter(1.0));
  669. $this->assertFalse($filter(''));
  670. $this->assertTrue($filter('abc'));
  671. $this->assertFalse($filter('0'));
  672. $this->assertTrue($filter('1'));
  673. $this->assertFalse($filter(array()));
  674. $this->assertTrue($filter(array('xxx')));
  675. $this->assertFalse($filter(null));
  676. $this->assertFalse($filter('false'));
  677. $this->assertTrue($filter('true'));
  678. $this->assertTrue($filter('no'));
  679. $this->assertTrue($filter('yes'));
  680. $this->assertFalse($filter('nein'));
  681. $this->assertTrue($filter('ja'));
  682. }
  683. /**
  684. * Ensures that the filter follows expected behavior
  685. *
  686. * @return void
  687. */
  688. public function testWithoutCasting()
  689. {
  690. $locale = new Locale('de');
  691. $filter = new BooleanFilter(
  692. array('type' => 'all', 'casting' => false, 'locale' => $locale)
  693. );
  694. $this->assertFalse($filter(false));
  695. $this->assertTrue($filter(true));
  696. $this->assertFalse($filter(0));
  697. $this->assertTrue($filter(1));
  698. $this->assertEquals(2, $filter(2));
  699. $this->assertFalse($filter(0.0));
  700. $this->assertTrue($filter(1.0));
  701. $this->assertEquals(0.5, $filter(0.5));
  702. $this->assertFalse($filter(''));
  703. $this->assertEquals('abc', $filter('abc'));
  704. $this->assertFalse($filter('0'));
  705. $this->assertTrue($filter('1'));
  706. $this->assertEquals('2', $filter('2'));
  707. $this->assertFalse($filter(array()));
  708. $this->assertEquals(array('xxx'), $filter(array('xxx')));
  709. $this->assertEquals(null, $filter(null));
  710. $this->assertFalse($filter('false'));
  711. $this->assertTrue($filter('true'));
  712. $this->assertEquals('no', $filter('no'));
  713. $this->assertEquals('yes', $filter('yes'));
  714. $this->assertFalse($filter('nein'));
  715. $this->assertTrue($filter('ja'));
  716. }
  717. /**
  718. * Ensures that the filter follows expected behavior
  719. *
  720. * @return void
  721. */
  722. public function testSettingFalseType()
  723. {
  724. $this->setExpectedException('\\Zend\\Filter\\Exception', 'Unknown');
  725. $this->_filter->setType(true);
  726. }
  727. /**
  728. * Ensures that the filter follows expected behavior
  729. *
  730. * @return void
  731. */
  732. public function testGetType()
  733. {
  734. $this->assertEquals(127, $this->_filter->getType());
  735. }
  736. /**
  737. * Ensures that the filter follows expected behavior
  738. *
  739. * @return void
  740. */
  741. public function testSettingFalseLocaleType()
  742. {
  743. $this->setExpectedException('\\Zend\\Filter\\Exception', 'Locale has to be');
  744. $this->_filter->setLocale(true);
  745. }
  746. /**
  747. * Ensures that the filter follows expected behavior
  748. *
  749. * @return void
  750. */
  751. public function testSettingUnknownLocale()
  752. {
  753. $this->setExpectedException('\\Zend\\Filter\\Exception', 'Unknown locale');
  754. $this->_filter->setLocale('yy');
  755. }
  756. }