No Description

NumberComparatorTest.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Finder\Tests\Comparator;
  11. use Symfony\Component\Finder\Comparator\NumberComparator;
  12. class NumberComparatorTest extends \PHPUnit_Framework_TestCase
  13. {
  14. /**
  15. * @dataProvider getConstructorTestData
  16. */
  17. public function testConstructor($successes, $failures)
  18. {
  19. foreach ($successes as $s) {
  20. new NumberComparator($s);
  21. }
  22. foreach ($failures as $f) {
  23. try {
  24. new NumberComparator($f);
  25. $this->fail('__construct() throws an \InvalidArgumentException if the test expression is not valid.');
  26. } catch (\Exception $e) {
  27. $this->assertInstanceOf('InvalidArgumentException', $e, '__construct() throws an \InvalidArgumentException if the test expression is not valid.');
  28. }
  29. }
  30. }
  31. /**
  32. * @dataProvider getTestData
  33. */
  34. public function testTest($test, $match, $noMatch)
  35. {
  36. $c = new NumberComparator($test);
  37. foreach ($match as $m) {
  38. $this->assertTrue($c->test($m), '->test() tests a string against the expression');
  39. }
  40. foreach ($noMatch as $m) {
  41. $this->assertFalse($c->test($m), '->test() tests a string against the expression');
  42. }
  43. }
  44. public function getTestData()
  45. {
  46. return array(
  47. array('< 1000', array('500', '999'), array('1000', '1500')),
  48. array('< 1K', array('500', '999'), array('1000', '1500')),
  49. array('<1k', array('500', '999'), array('1000', '1500')),
  50. array(' < 1 K ', array('500', '999'), array('1000', '1500')),
  51. array('<= 1K', array('1000'), array('1001')),
  52. array('> 1K', array('1001'), array('1000')),
  53. array('>= 1K', array('1000'), array('999')),
  54. array('< 1KI', array('500', '1023'), array('1024', '1500')),
  55. array('<= 1KI', array('1024'), array('1025')),
  56. array('> 1KI', array('1025'), array('1024')),
  57. array('>= 1KI', array('1024'), array('1023')),
  58. array('1KI', array('1024'), array('1023', '1025')),
  59. array('==1KI', array('1024'), array('1023', '1025')),
  60. array('==1m', array('1000000'), array('999999', '1000001')),
  61. array('==1mi', array(1024 * 1024), array(1024 * 1024 - 1, 1024 * 1024 + 1)),
  62. array('==1g', array('1000000000'), array('999999999', '1000000001')),
  63. array('==1gi', array(1024 * 1024 * 1024), array(1024 * 1024 * 1024 - 1, 1024 * 1024 * 1024 + 1)),
  64. array('!= 1000', array('500', '999'), array('1000')),
  65. );
  66. }
  67. public function getConstructorTestData()
  68. {
  69. return array(
  70. array(
  71. array(
  72. '1', '0',
  73. '3.5', '33.55', '123.456', '123456.78',
  74. '.1', '.123',
  75. '.0', '0.0',
  76. '1.', '0.', '123.',
  77. '==1', '!=1', '<1', '>1', '<=1', '>=1',
  78. '==1k', '==1ki', '==1m', '==1mi', '==1g', '==1gi',
  79. '1k', '1ki', '1m', '1mi', '1g', '1gi',
  80. ),
  81. array(
  82. false, null, '',
  83. ' ', 'foobar',
  84. '=1', '===1',
  85. '0 . 1', '123 .45', '234. 567',
  86. '..', '.0.', '0.1.2',
  87. ),
  88. ),
  89. );
  90. }
  91. }