PageRenderTime 66ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/system/tests/kohana/ValidTest.php

https://bitbucket.org/levjke/kohanablogds
PHP | 981 lines | 551 code | 58 blank | 372 comment | 4 complexity | 5c9c48f9e57050804df31d56981e6c8e MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  1. <?php defined('SYSPATH') OR die('Kohana bootstrap needs to be included before tests run');
  2. /**
  3. * Tests the Valid class
  4. *
  5. * @group kohana
  6. * @group kohana.valid
  7. *
  8. * @package Kohana
  9. * @category Tests
  10. * @author Kohana Team
  11. * @author BRMatt <matthew@sigswitch.com>
  12. * @copyright (c) 2008-2011 Kohana Team
  13. * @license http://kohanaframework.org/license
  14. */
  15. class Kohana_ValidTest extends Unittest_TestCase
  16. {
  17. /**
  18. * Provides test data for test_alpha()
  19. * @return array
  20. */
  21. public function provider_alpha()
  22. {
  23. return array(
  24. array('asdavafaiwnoabwiubafpowf', TRUE),
  25. array('!aidhfawiodb', FALSE),
  26. array('51535oniubawdawd78', FALSE),
  27. array('!"£$(G$W£(HFW£F(HQ)"n', FALSE),
  28. // UTF-8 tests
  29. array('あいうえお', TRUE, TRUE),
  30. array('¥', FALSE, TRUE),
  31. // Empty test
  32. array('', FALSE, FALSE),
  33. array(NULL, FALSE, FALSE),
  34. array(FALSE, FALSE, FALSE),
  35. );
  36. }
  37. /**
  38. * Tests Valid::alpha()
  39. *
  40. * Checks whether a string consists of alphabetical characters only.
  41. *
  42. * @test
  43. * @dataProvider provider_alpha
  44. * @param string $string
  45. * @param boolean $expected
  46. */
  47. public function test_alpha($string, $expected, $utf8 = FALSE)
  48. {
  49. $this->assertSame(
  50. $expected,
  51. Valid::alpha($string, $utf8)
  52. );
  53. }
  54. /*
  55. * Provides test data for test_alpha_numeric
  56. */
  57. public function provide_alpha_numeric()
  58. {
  59. return array(
  60. array('abcd1234', TRUE),
  61. array('abcd', TRUE),
  62. array('1234', TRUE),
  63. array('abc123&^/-', FALSE),
  64. // UTF-8 tests
  65. array('あいうえお', TRUE, TRUE),
  66. array('零一二三四五', TRUE, TRUE),
  67. array('あい四五£^£^', FALSE, TRUE),
  68. // Empty test
  69. array('', FALSE, FALSE),
  70. array(NULL, FALSE, FALSE),
  71. array(FALSE, FALSE, FALSE),
  72. );
  73. }
  74. /**
  75. * Tests Valid::alpha_numberic()
  76. *
  77. * Checks whether a string consists of alphabetical characters and numbers only.
  78. *
  79. * @test
  80. * @dataProvider provide_alpha_numeric
  81. * @param string $input The string to test
  82. * @param boolean $expected Is $input valid
  83. */
  84. public function test_alpha_numeric($input, $expected, $utf8 = FALSE)
  85. {
  86. $this->assertSame(
  87. $expected,
  88. Valid::alpha_numeric($input, $utf8)
  89. );
  90. }
  91. /**
  92. * Provides test data for test_alpha_dash
  93. */
  94. public function provider_alpha_dash()
  95. {
  96. return array(
  97. array('abcdef', TRUE),
  98. array('12345', TRUE),
  99. array('abcd1234', TRUE),
  100. array('abcd1234-', TRUE),
  101. array('abc123&^/-', FALSE),
  102. // Empty test
  103. array('', FALSE),
  104. array(NULL, FALSE),
  105. array(FALSE, FALSE),
  106. );
  107. }
  108. /**
  109. * Tests Valid::alpha_dash()
  110. *
  111. * Checks whether a string consists of alphabetical characters, numbers, underscores and dashes only.
  112. *
  113. * @test
  114. * @dataProvider provider_alpha_dash
  115. * @param string $input The string to test
  116. * @param boolean $contains_utf8 Does the string contain utf8 specific characters
  117. * @param boolean $expected Is $input valid?
  118. */
  119. public function test_alpha_dash($input, $expected, $contains_utf8 = FALSE)
  120. {
  121. if ( ! $contains_utf8)
  122. {
  123. $this->assertSame(
  124. $expected,
  125. Valid::alpha_dash($input)
  126. );
  127. }
  128. $this->assertSame(
  129. $expected,
  130. Valid::alpha_dash($input, TRUE)
  131. );
  132. }
  133. /**
  134. * DataProvider for the valid::date() test
  135. */
  136. public function provider_date()
  137. {
  138. return array(
  139. array('now',TRUE),
  140. array('10 September 2010',TRUE),
  141. array('+1 day',TRUE),
  142. array('+1 week',TRUE),
  143. array('+1 week 2 days 4 hours 2 seconds',TRUE),
  144. array('next Thursday',TRUE),
  145. array('last Monday',TRUE),
  146. array('blarg',FALSE),
  147. array('in the year 2000',FALSE),
  148. array('324824',FALSE),
  149. // Empty test
  150. array('', FALSE),
  151. array(NULL, FALSE),
  152. array(FALSE, FALSE),
  153. );
  154. }
  155. /**
  156. * Tests Valid::date()
  157. *
  158. * @test
  159. * @dataProvider provider_date
  160. * @param string $date The date to validate
  161. * @param integer $expected
  162. */
  163. public function test_date($date, $expected)
  164. {
  165. $this->assertSame(
  166. $expected,
  167. Valid::date($date, $expected)
  168. );
  169. }
  170. /**
  171. * DataProvider for the valid::decimal() test
  172. */
  173. public function provider_decimal()
  174. {
  175. return array(
  176. // Empty test
  177. array('', 2, NULL, FALSE),
  178. array(NULL, 2, NULL, FALSE),
  179. array(FALSE, 2, NULL, FALSE),
  180. array('45.1664', 3, NULL, FALSE),
  181. array('45.1664', 4, NULL, TRUE),
  182. array('45.1664', 4, 2, TRUE),
  183. array('-45.1664', 4, NULL, TRUE),
  184. array('+45.1664', 4, NULL, TRUE),
  185. array('-45.1664', 3, NULL, FALSE),
  186. );
  187. }
  188. /**
  189. * Tests Valid::decimal()
  190. *
  191. * @test
  192. * @dataProvider provider_decimal
  193. * @param string $decimal The decimal to validate
  194. * @param integer $places The number of places to check to
  195. * @param integer $digits The number of digits preceding the point to check
  196. * @param boolean $expected Whether $decimal conforms to $places AND $digits
  197. */
  198. public function test_decimal($decimal, $places, $digits, $expected)
  199. {
  200. $this->assertSame(
  201. $expected,
  202. Valid::decimal($decimal, $places, $digits),
  203. 'Decimal: "'.$decimal.'" to '.$places.' places and '.$digits.' digits (preceeding period)'
  204. );
  205. }
  206. /**
  207. * Provides test data for test_digit
  208. * @return array
  209. */
  210. public function provider_digit()
  211. {
  212. return array(
  213. array('12345', TRUE),
  214. array('10.5', FALSE),
  215. array('abcde', FALSE),
  216. array('abcd1234', FALSE),
  217. array('-5', FALSE),
  218. array(-5, FALSE),
  219. // Empty test
  220. array('', FALSE),
  221. array(NULL, FALSE),
  222. array(FALSE, FALSE),
  223. );
  224. }
  225. /**
  226. * Tests Valid::digit()
  227. *
  228. * @test
  229. * @dataProvider provider_digit
  230. * @param mixed $input Input to validate
  231. * @param boolean $expected Is $input valid
  232. */
  233. public function test_digit($input, $expected, $contains_utf8 = FALSE)
  234. {
  235. if ( ! $contains_utf8)
  236. {
  237. $this->assertSame(
  238. $expected,
  239. Valid::digit($input)
  240. );
  241. }
  242. $this->assertSame(
  243. $expected,
  244. Valid::digit($input, TRUE)
  245. );
  246. }
  247. /**
  248. * DataProvider for the valid::color() test
  249. */
  250. public function provider_color()
  251. {
  252. return array(
  253. array('#000000', TRUE),
  254. array('#GGGGGG', FALSE),
  255. array('#AbCdEf', TRUE),
  256. array('#000', TRUE),
  257. array('#abc', TRUE),
  258. array('#DEF', TRUE),
  259. array('000000', TRUE),
  260. array('GGGGGG', FALSE),
  261. array('AbCdEf', TRUE),
  262. array('000', TRUE),
  263. array('DEF', TRUE),
  264. // Empty test
  265. array('', FALSE),
  266. array(NULL, FALSE),
  267. array(FALSE, FALSE),
  268. );
  269. }
  270. /**
  271. * Tests Valid::color()
  272. *
  273. * @test
  274. * @dataProvider provider_color
  275. * @param string $color The color to test
  276. * @param boolean $expected Is $color valid
  277. */
  278. public function test_color($color, $expected)
  279. {
  280. $this->assertSame(
  281. $expected,
  282. Valid::color($color)
  283. );
  284. }
  285. /**
  286. * Provides test data for test_credit_card()
  287. */
  288. public function provider_credit_card()
  289. {
  290. return array(
  291. array('4222222222222', 'visa', TRUE),
  292. array('4012888888881881', 'visa', TRUE),
  293. array('4012888888881881', NULL, TRUE),
  294. array('4012888888881881', array('mastercard', 'visa'), TRUE),
  295. array('4012888888881881', array('discover', 'mastercard'), FALSE),
  296. array('4012888888881881', 'mastercard', FALSE),
  297. array('5105105105105100', 'mastercard', TRUE),
  298. array('6011111111111117', 'discover', TRUE),
  299. array('6011111111111117', 'visa', FALSE),
  300. // Empty test
  301. array('', NULL, FALSE),
  302. array(NULL, NULL, FALSE),
  303. array(FALSE, NULL, FALSE),
  304. );
  305. }
  306. /**
  307. * Tests Valid::credit_card()
  308. *
  309. * @test
  310. * @covers Valid::credit_card
  311. * @dataProvider provider_credit_card()
  312. * @param string $number Credit card number
  313. * @param string $type Credit card type
  314. * @param boolean $expected
  315. */
  316. public function test_credit_card($number, $type, $expected)
  317. {
  318. $this->assertSame(
  319. $expected,
  320. Valid::credit_card($number, $type)
  321. );
  322. }
  323. /**
  324. * Provides test data for test_credit_card()
  325. */
  326. public function provider_luhn()
  327. {
  328. return array(
  329. array('4222222222222', TRUE),
  330. array('4012888888881881', TRUE),
  331. array('5105105105105100', TRUE),
  332. array('6011111111111117', TRUE),
  333. array('60111111111111.7', FALSE),
  334. array('6011111111111117X', FALSE),
  335. array('6011111111111117 ', FALSE),
  336. array('WORD ', FALSE),
  337. // Empty test
  338. array('', FALSE),
  339. array(NULL, FALSE),
  340. array(FALSE, FALSE),
  341. );
  342. }
  343. /**
  344. * Tests Valid::luhn()
  345. *
  346. * @test
  347. * @covers Valid::luhn
  348. * @dataProvider provider_luhn()
  349. * @param string $number Credit card number
  350. * @param boolean $expected
  351. */
  352. public function test_luhn($number, $expected)
  353. {
  354. $this->assertSame(
  355. $expected,
  356. Valid::luhn($number)
  357. );
  358. }
  359. /**
  360. * Provides test data for test_email()
  361. *
  362. * @return array
  363. */
  364. public function provider_email()
  365. {
  366. return array(
  367. array('foo', TRUE, FALSE),
  368. array('foo', FALSE, FALSE),
  369. array('foo@bar', TRUE, TRUE),
  370. // RFC is less strict than the normal regex, presumably to allow
  371. // admin@localhost, therefore we IGNORE IT!!!
  372. array('foo@bar', FALSE, FALSE),
  373. array('foo@bar.com', FALSE, TRUE),
  374. array('foo@barcom:80', FALSE, FALSE),
  375. array('foo@bar.sub.com', FALSE, TRUE),
  376. array('foo+asd@bar.sub.com', FALSE, TRUE),
  377. array('foo.asd@bar.sub.com', FALSE, TRUE),
  378. // RFC says 254 length max #4011
  379. array(Text::random(NULL, 200).'@'.Text::random(NULL, 50).'.com', FALSE, FALSE),
  380. // Empty test
  381. array('', TRUE, FALSE),
  382. array(NULL, TRUE, FALSE),
  383. array(FALSE, TRUE, FALSE),
  384. );
  385. }
  386. /**
  387. * Tests Valid::email()
  388. *
  389. * Check an email address for correct format.
  390. *
  391. * @test
  392. * @dataProvider provider_email
  393. * @param string $email Address to check
  394. * @param boolean $strict Use strict settings
  395. * @param boolean $correct Is $email address valid?
  396. */
  397. public function test_email($email, $strict, $correct)
  398. {
  399. $this->assertSame(
  400. $correct,
  401. Valid::email($email, $strict)
  402. );
  403. }
  404. /**
  405. * Returns test data for test_email_domain()
  406. *
  407. * @return array
  408. */
  409. public function provider_email_domain()
  410. {
  411. return array(
  412. array('google.com', TRUE),
  413. // Don't anybody dare register this...
  414. array('DAWOMAWIDAIWNDAIWNHDAWIHDAIWHDAIWOHDAIOHDAIWHD.com', FALSE),
  415. // Empty test
  416. array('', FALSE),
  417. array(NULL, FALSE),
  418. array(FALSE, FALSE),
  419. );
  420. }
  421. /**
  422. * Tests Valid::email_domain()
  423. *
  424. * Validate the domain of an email address by checking if the domain has a
  425. * valid MX record.
  426. *
  427. * Test skips on windows
  428. *
  429. * @test
  430. * @dataProvider provider_email_domain
  431. * @param string $email Email domain to check
  432. * @param boolean $correct Is it correct?
  433. */
  434. public function test_email_domain($email, $correct)
  435. {
  436. if ( ! $this->hasInternet())
  437. {
  438. $this->markTestSkipped('An internet connection is required for this test');
  439. }
  440. if ( ! Kohana::$is_windows OR version_compare(PHP_VERSION, '5.3.0', '>='))
  441. {
  442. $this->assertSame(
  443. $correct,
  444. Valid::email_domain($email)
  445. );
  446. }
  447. else
  448. {
  449. $this->markTestSkipped('checkdnsrr() was not added on windows until PHP 5.3');
  450. }
  451. }
  452. /**
  453. * Provides data for test_exact_length()
  454. *
  455. * @return array
  456. */
  457. public function provider_exact_length()
  458. {
  459. return array(
  460. array('somestring', 10, TRUE),
  461. array('somestring', 11, FALSE),
  462. array('anotherstring', 13, TRUE),
  463. // Empty test
  464. array('', 10, FALSE),
  465. array(NULL, 10, FALSE),
  466. array(FALSE, 10, FALSE),
  467. // Test array of allowed lengths
  468. array('somestring', array(1, 3, 5, 7, 9, 10), TRUE),
  469. array('somestring', array(1, 3, 5, 7, 9), FALSE),
  470. );
  471. }
  472. /**
  473. *
  474. * Tests Valid::exact_length()
  475. *
  476. * Checks that a field is exactly the right length.
  477. *
  478. * @test
  479. * @dataProvider provider_exact_length
  480. * @param string $string The string to length check
  481. * @param integer $length The length of the string
  482. * @param boolean $correct Is $length the actual length of the string?
  483. * @return bool
  484. */
  485. public function test_exact_length($string, $length, $correct)
  486. {
  487. return $this->assertSame(
  488. $correct,
  489. Valid::exact_length($string, $length),
  490. 'Reported string length is not correct'
  491. );
  492. }
  493. /**
  494. * Provides data for test_equals()
  495. *
  496. * @return array
  497. */
  498. public function provider_equals()
  499. {
  500. return array(
  501. array('foo', 'foo', TRUE),
  502. array('1', '1', TRUE),
  503. array(1, '1', FALSE),
  504. array('011', 011, FALSE),
  505. // Empty test
  506. array('', 123, FALSE),
  507. array(NULL, 123, FALSE),
  508. array(FALSE, 123, FALSE),
  509. );
  510. }
  511. /**
  512. * Tests Valid::equals()
  513. *
  514. * @test
  515. * @dataProvider provider_equals
  516. * @param string $string value to check
  517. * @param integer $required required value
  518. * @param boolean $correct is $string the same as $required?
  519. * @return boolean
  520. */
  521. public function test_equals($string, $required, $correct)
  522. {
  523. return $this->assertSame(
  524. $correct,
  525. Valid::equals($string, $required),
  526. 'Values are not equal'
  527. );
  528. }
  529. /**
  530. * DataProvider for the valid::ip() test
  531. * @return array
  532. */
  533. public function provider_ip()
  534. {
  535. return array(
  536. array('75.125.175.50', FALSE, TRUE),
  537. // PHP 5.3.6 fixed a bug that allowed 127.0.0.1 as a public ip: http://bugs.php.net/53150
  538. array('127.0.0.1', FALSE, version_compare(PHP_VERSION, '5.3.6', '<')),
  539. array('256.257.258.259', FALSE, FALSE),
  540. array('255.255.255.255', FALSE, FALSE),
  541. array('192.168.0.1', FALSE, FALSE),
  542. array('192.168.0.1', TRUE, TRUE),
  543. // Empty test
  544. array('', TRUE, FALSE),
  545. array(NULL, TRUE, FALSE),
  546. array(FALSE, TRUE, FALSE),
  547. );
  548. }
  549. /**
  550. * Tests Valid::ip()
  551. *
  552. * @test
  553. * @dataProvider provider_ip
  554. * @param string $input_ip
  555. * @param boolean $allow_private
  556. * @param boolean $expected_result
  557. */
  558. public function test_ip($input_ip, $allow_private, $expected_result)
  559. {
  560. $this->assertEquals(
  561. $expected_result,
  562. Valid::ip($input_ip, $allow_private)
  563. );
  564. }
  565. /**
  566. * Returns test data for test_max_length()
  567. *
  568. * @return array
  569. */
  570. public function provider_max_length()
  571. {
  572. return array(
  573. // Border line
  574. array('some', 4, TRUE),
  575. // Exceeds
  576. array('KOHANARULLLES', 2, FALSE),
  577. // Under
  578. array('CakeSucks', 10, TRUE),
  579. // Empty test
  580. array('', -10, FALSE),
  581. array(NULL, -10, FALSE),
  582. array(FALSE, -10, FALSE),
  583. );
  584. }
  585. /**
  586. * Tests Valid::max_length()
  587. *
  588. * Checks that a field is short enough.
  589. *
  590. * @test
  591. * @dataProvider provider_max_length
  592. * @param string $string String to test
  593. * @param integer $maxlength Max length for this string
  594. * @param boolean $correct Is $string <= $maxlength
  595. */
  596. public function test_max_length($string, $maxlength, $correct)
  597. {
  598. $this->assertSame(
  599. $correct,
  600. Valid::max_length($string, $maxlength)
  601. );
  602. }
  603. /**
  604. * Returns test data for test_min_length()
  605. *
  606. * @return array
  607. */
  608. public function provider_min_length()
  609. {
  610. return array(
  611. array('This is obviously long enough', 10, TRUE),
  612. array('This is not', 101, FALSE),
  613. array('This is on the borderline', 25, TRUE),
  614. // Empty test
  615. array('', 10, FALSE),
  616. array(NULL, 10, FALSE),
  617. array(FALSE, 10, FALSE),
  618. );
  619. }
  620. /**
  621. * Tests Valid::min_length()
  622. *
  623. * Checks that a field is long enough.
  624. *
  625. * @test
  626. * @dataProvider provider_min_length
  627. * @param string $string String to compare
  628. * @param integer $minlength The minimum allowed length
  629. * @param boolean $correct Is $string 's length >= $minlength
  630. */
  631. public function test_min_length($string, $minlength, $correct)
  632. {
  633. $this->assertSame(
  634. $correct,
  635. Valid::min_length($string, $minlength)
  636. );
  637. }
  638. /**
  639. * Returns test data for test_not_empty()
  640. *
  641. * @return array
  642. */
  643. public function provider_not_empty()
  644. {
  645. // Create a blank arrayObject
  646. $ao = new ArrayObject;
  647. // arrayObject with value
  648. $ao1 = new ArrayObject;
  649. $ao1['test'] = 'value';
  650. return array(
  651. array(array(), FALSE),
  652. array(NULL, FALSE),
  653. array('', FALSE),
  654. array($ao, FALSE),
  655. array($ao1, TRUE),
  656. array(array(NULL), TRUE),
  657. array(0, TRUE),
  658. array('0', TRUE),
  659. array('Something', TRUE),
  660. );
  661. }
  662. /**
  663. * Tests Valid::not_empty()
  664. *
  665. * Checks if a field is not empty.
  666. *
  667. * @test
  668. * @dataProvider provider_not_empty
  669. * @param mixed $value Value to check
  670. * @param boolean $empty Is the value really empty?
  671. */
  672. public function test_not_empty($value, $empty)
  673. {
  674. return $this->assertSame(
  675. $empty,
  676. Valid::not_empty($value)
  677. );
  678. }
  679. /**
  680. * DataProvider for the Valid::numeric() test
  681. */
  682. public function provider_numeric()
  683. {
  684. return array(
  685. array(12345, TRUE),
  686. array(123.45, TRUE),
  687. array('12345', TRUE),
  688. array('10.5', TRUE),
  689. array('-10.5', TRUE),
  690. array('10.5a', FALSE),
  691. // @issue 3240
  692. array(.4, TRUE),
  693. array(-.4, TRUE),
  694. array(4., TRUE),
  695. array(-4., TRUE),
  696. array('.5', TRUE),
  697. array('-.5', TRUE),
  698. array('5.', TRUE),
  699. array('-5.', TRUE),
  700. array('.', FALSE),
  701. array('1.2.3', FALSE),
  702. // Empty test
  703. array('', FALSE),
  704. array(NULL, FALSE),
  705. array(FALSE, FALSE),
  706. );
  707. }
  708. /**
  709. * Tests Valid::numeric()
  710. *
  711. * @test
  712. * @dataProvider provider_numeric
  713. * @param string $input Input to test
  714. * @param boolean $expected Whether or not $input is numeric
  715. */
  716. public function test_numeric($input, $expected)
  717. {
  718. $this->assertSame(
  719. $expected,
  720. Valid::numeric($input)
  721. );
  722. }
  723. /**
  724. * Provides test data for test_phone()
  725. * @return array
  726. */
  727. public function provider_phone()
  728. {
  729. return array(
  730. array('0163634840', NULL, TRUE),
  731. array('+27173634840', NULL, TRUE),
  732. array('123578', NULL, FALSE),
  733. // Some uk numbers
  734. array('01234456778', NULL, TRUE),
  735. array('+0441234456778', NULL, FALSE),
  736. // Google UK case you're interested
  737. array('+44 20-7031-3000', array(12), TRUE),
  738. // BT Corporate
  739. array('020 7356 5000', NULL, TRUE),
  740. // Empty test
  741. array('', NULL, FALSE),
  742. array(NULL, NULL, FALSE),
  743. array(FALSE, NULL, FALSE),
  744. );
  745. }
  746. /**
  747. * Tests Valid::phone()
  748. *
  749. * @test
  750. * @dataProvider provider_phone
  751. * @param string $phone Phone number to test
  752. * @param boolean $expected Is $phone valid
  753. */
  754. public function test_phone($phone, $lengths, $expected)
  755. {
  756. $this->assertSame(
  757. $expected,
  758. Valid::phone($phone, $lengths)
  759. );
  760. }
  761. /**
  762. * DataProvider for the valid::regex() test
  763. */
  764. public function provider_regex()
  765. {
  766. return array(
  767. array('hello world', '/[a-zA-Z\s]++/', TRUE),
  768. array('123456789', '/[0-9]++/', TRUE),
  769. array('£$%£%', '/[abc]/', FALSE),
  770. array('Good evening', '/hello/', FALSE),
  771. // Empty test
  772. array('', '/hello/', FALSE),
  773. array(NULL, '/hello/', FALSE),
  774. array(FALSE, '/hello/', FALSE),
  775. );
  776. }
  777. /**
  778. * Tests Valid::range()
  779. *
  780. * Tests if a number is within a range.
  781. *
  782. * @test
  783. * @dataProvider provider_regex
  784. * @param string Value to test against
  785. * @param string Valid pcre regular expression
  786. * @param bool Does the value match the expression?
  787. */
  788. public function test_regex($value, $regex, $expected)
  789. {
  790. $this->AssertSame(
  791. $expected,
  792. Valid::regex($value, $regex)
  793. );
  794. }
  795. /**
  796. * DataProvider for the valid::range() test
  797. */
  798. public function provider_range()
  799. {
  800. return array(
  801. array(1, 0, 2, TRUE),
  802. array(-1, -5, 0, TRUE),
  803. array(-1, 0, 1, FALSE),
  804. array(1, 0, 0, FALSE),
  805. array(2147483647, 0, 200000000000000, TRUE),
  806. array(-2147483647, -2147483655, 2147483645, TRUE),
  807. // Empty test
  808. array('', 5, 10, FALSE),
  809. array(NULL, 5, 10, FALSE),
  810. array(FALSE, 5, 10, FALSE),
  811. );
  812. }
  813. /**
  814. * Tests Valid::range()
  815. *
  816. * Tests if a number is within a range.
  817. *
  818. * @test
  819. * @dataProvider provider_range
  820. * @param integer $number Number to test
  821. * @param integer $min Lower bound
  822. * @param integer $max Upper bound
  823. * @param boolean $expected Is Number within the bounds of $min && $max
  824. */
  825. public function test_range($number, $min, $max, $expected)
  826. {
  827. $this->AssertSame(
  828. $expected,
  829. Valid::range($number, $min, $max)
  830. );
  831. }
  832. /**
  833. * Provides test data for test_url()
  834. *
  835. * @return array
  836. */
  837. public function provider_url()
  838. {
  839. $data = array(
  840. array('http://google.com', TRUE),
  841. array('http://google.com/', TRUE),
  842. array('http://google.com/?q=abc', TRUE),
  843. array('http://google.com/#hash', TRUE),
  844. array('http://localhost', TRUE),
  845. array('http://hello-world.pl', TRUE),
  846. array('http://hello--world.pl', TRUE),
  847. array('http://h.e.l.l.0.pl', TRUE),
  848. array('http://server.tld/get/info', TRUE),
  849. array('http://127.0.0.1', TRUE),
  850. array('http://127.0.0.1:80', TRUE),
  851. array('http://user@127.0.0.1', TRUE),
  852. array('http://user:pass@127.0.0.1', TRUE),
  853. array('ftp://my.server.com', TRUE),
  854. array('rss+xml://rss.example.com', TRUE),
  855. array('http://google.2com', FALSE),
  856. array('http://google.com?q=abc', FALSE),
  857. array('http://google.com#hash', FALSE),
  858. array('http://hello-.pl', FALSE),
  859. array('http://hel.-lo.world.pl', FALSE),
  860. array('http://ww£.google.com', FALSE),
  861. array('http://127.0.0.1234', FALSE),
  862. array('http://127.0.0.1.1', FALSE),
  863. array('http://user:@127.0.0.1', FALSE),
  864. array("http://finalnewline.com\n", FALSE),
  865. // Empty test
  866. array('', FALSE),
  867. array(NULL, FALSE),
  868. array(FALSE, FALSE),
  869. );
  870. $data[] = array('http://'.str_repeat('123456789.', 25).'com/', TRUE); // 253 chars
  871. $data[] = array('http://'.str_repeat('123456789.', 25).'info/', FALSE); // 254 chars
  872. return $data;
  873. }
  874. /**
  875. * Tests Valid::url()
  876. *
  877. * @test
  878. * @dataProvider provider_url
  879. * @param string $url The url to test
  880. * @param boolean $expected Is it valid?
  881. */
  882. public function test_url($url, $expected)
  883. {
  884. $this->assertSame(
  885. $expected,
  886. Valid::url($url)
  887. );
  888. }
  889. /**
  890. * DataProvider for the valid::matches() test
  891. */
  892. public function provider_matches()
  893. {
  894. return array(
  895. array(array('a' => 'hello', 'b' => 'hello'), 'a', 'b', TRUE),
  896. array(array('a' => 'hello', 'b' => 'hello '), 'a', 'b', FALSE),
  897. array(array('a' => '1', 'b' => 1), 'a', 'b', FALSE),
  898. // Empty test
  899. array(array('a' => '', 'b' => 'hello'), 'a', 'b', FALSE),
  900. array(array('a' => NULL, 'b' => 'hello'), 'a', 'b', FALSE),
  901. array(array('a' => FALSE, 'b' => 'hello'), 'a', 'b', FALSE),
  902. );
  903. }
  904. /**
  905. * Tests Valid::matches()
  906. *
  907. * Tests if a field matches another from an array of data
  908. *
  909. * @test
  910. * @dataProvider provider_matches
  911. * @param array $data Array of fields
  912. * @param integer $field First field name
  913. * @param integer $match Field name that must match $field in $data
  914. * @param boolean $expected Do the two fields match?
  915. */
  916. public function test_matches($data, $field, $match, $expected)
  917. {
  918. $this->AssertSame(
  919. $expected,
  920. Valid::matches($data, $field, $match)
  921. );
  922. }
  923. }