NumberComparatorTest.php 3.3 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 [
  48. ['< 1000', ['500', '999'], ['1000', '1500']],
  49. ['< 1K', ['500', '999'], ['1000', '1500']],
  50. ['<1k', ['500', '999'], ['1000', '1500']],
  51. [' < 1 K ', ['500', '999'], ['1000', '1500']],
  52. ['<= 1K', ['1000'], ['1001']],
  53. ['> 1K', ['1001'], ['1000']],
  54. ['>= 1K', ['1000'], ['999']],
  55. ['< 1KI', ['500', '1023'], ['1024', '1500']],
  56. ['<= 1KI', ['1024'], ['1025']],
  57. ['> 1KI', ['1025'], ['1024']],
  58. ['>= 1KI', ['1024'], ['1023']],
  59. ['1KI', ['1024'], ['1023', '1025']],
  60. ['==1KI', ['1024'], ['1023', '1025']],
  61. ['==1m', ['1000000'], ['999999', '1000001']],
  62. ['==1mi', [1024 * 1024], [1024 * 1024 - 1, 1024 * 1024 + 1]],
  63. ['==1g', ['1000000000'], ['999999999', '1000000001']],
  64. ['==1gi', [1024 * 1024 * 1024], [1024 * 1024 * 1024 - 1, 1024 * 1024 * 1024 + 1]],
  65. ['!= 1000', ['500', '999'], ['1000']],
  66. ];
  67. }
  68. public function getConstructorTestData()
  69. {
  70. return [
  71. [
  72. [
  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. [
  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. }