NumberComparatorTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Finder\Comparator\NumberComparator;
  13. class NumberComparatorTest extends TestCase
  14. {
  15. /**
  16. * @dataProvider getConstructorTestData
  17. */
  18. public function testConstructor($successes, $failures)
  19. {
  20. foreach ($successes as $s) {
  21. new NumberComparator($s);
  22. }
  23. foreach ($failures as $f) {
  24. try {
  25. new NumberComparator($f);
  26. $this->fail('__construct() throws an \InvalidArgumentException if the test expression is not valid.');
  27. } catch (\Exception $e) {
  28. $this->assertInstanceOf('InvalidArgumentException', $e, '__construct() throws an \InvalidArgumentException if the test expression is not valid.');
  29. }
  30. }
  31. }
  32. /**
  33. * @dataProvider getTestData
  34. */
  35. public function testTest($test, $match, $noMatch)
  36. {
  37. $c = new NumberComparator($test);
  38. foreach ($match as $m) {
  39. $this->assertTrue($c->test($m), '->test() tests a string against the expression');
  40. }
  41. foreach ($noMatch as $m) {
  42. $this->assertFalse($c->test($m), '->test() tests a string against the expression');
  43. }
  44. }
  45. public function getTestData()
  46. {
  47. return array(
  48. array('< 1000', array('500', '999'), array('1000', '1500')),
  49. array('< 1K', array('500', '999'), array('1000', '1500')),
  50. array('<1k', array('500', '999'), array('1000', '1500')),
  51. array(' < 1 K ', array('500', '999'), array('1000', '1500')),
  52. array('<= 1K', array('1000'), array('1001')),
  53. array('> 1K', array('1001'), array('1000')),
  54. array('>= 1K', array('1000'), array('999')),
  55. array('< 1KI', array('500', '1023'), array('1024', '1500')),
  56. array('<= 1KI', array('1024'), array('1025')),
  57. array('> 1KI', array('1025'), array('1024')),
  58. array('>= 1KI', array('1024'), array('1023')),
  59. array('1KI', array('1024'), array('1023', '1025')),
  60. array('==1KI', array('1024'), array('1023', '1025')),
  61. array('==1m', array('1000000'), array('999999', '1000001')),
  62. array('==1mi', array(1024 * 1024), array(1024 * 1024 - 1, 1024 * 1024 + 1)),
  63. array('==1g', array('1000000000'), array('999999999', '1000000001')),
  64. array('==1gi', array(1024 * 1024 * 1024), array(1024 * 1024 * 1024 - 1, 1024 * 1024 * 1024 + 1)),
  65. array('!= 1000', array('500', '999'), array('1000')),
  66. );
  67. }
  68. public function getConstructorTestData()
  69. {
  70. return array(
  71. array(
  72. array(
  73. '1', '0',
  74. '3.5', '33.55', '123.456', '123456.78',
  75. '.1', '.123',
  76. '.0', '0.0',
  77. '1.', '0.', '123.',
  78. '==1', '!=1', '<1', '>1', '<=1', '>=1',
  79. '==1k', '==1ki', '==1m', '==1mi', '==1g', '==1gi',
  80. '1k', '1ki', '1m', '1mi', '1g', '1gi',
  81. ),
  82. array(
  83. false, null, '',
  84. ' ', 'foobar',
  85. '=1', '===1',
  86. '0 . 1', '123 .45', '234. 567',
  87. '..', '.0.', '0.1.2',
  88. ),
  89. ),
  90. );
  91. }
  92. }