DateComparatorTest.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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\DateComparator;
  13. class DateComparatorTest extends TestCase
  14. {
  15. public function testConstructor()
  16. {
  17. try {
  18. new DateComparator('foobar');
  19. $this->fail('__construct() throws an \InvalidArgumentException if the test expression is not valid.');
  20. } catch (\Exception $e) {
  21. $this->assertInstanceOf('InvalidArgumentException', $e, '__construct() throws an \InvalidArgumentException if the test expression is not valid.');
  22. }
  23. try {
  24. new DateComparator('');
  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. * @dataProvider getTestData
  32. */
  33. public function testTest($test, $match, $noMatch)
  34. {
  35. $c = new DateComparator($test);
  36. foreach ($match as $m) {
  37. $this->assertTrue($c->test($m), '->test() tests a string against the expression');
  38. }
  39. foreach ($noMatch as $m) {
  40. $this->assertFalse($c->test($m), '->test() tests a string against the expression');
  41. }
  42. }
  43. public function getTestData()
  44. {
  45. return [
  46. ['< 2005-10-10', [strtotime('2005-10-09')], [strtotime('2005-10-15')]],
  47. ['until 2005-10-10', [strtotime('2005-10-09')], [strtotime('2005-10-15')]],
  48. ['before 2005-10-10', [strtotime('2005-10-09')], [strtotime('2005-10-15')]],
  49. ['> 2005-10-10', [strtotime('2005-10-15')], [strtotime('2005-10-09')]],
  50. ['after 2005-10-10', [strtotime('2005-10-15')], [strtotime('2005-10-09')]],
  51. ['since 2005-10-10', [strtotime('2005-10-15')], [strtotime('2005-10-09')]],
  52. ['!= 2005-10-10', [strtotime('2005-10-11')], [strtotime('2005-10-10')]],
  53. ];
  54. }
  55. }