RegexTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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\Expression;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Finder\Expression\Expression;
  13. /**
  14. * @group legacy
  15. */
  16. class RegexTest extends TestCase
  17. {
  18. /**
  19. * @dataProvider getHasFlagsData
  20. */
  21. public function testHasFlags($regex, $start, $end)
  22. {
  23. $expr = new Expression($regex);
  24. $this->assertEquals($start, $expr->getRegex()->hasStartFlag());
  25. $this->assertEquals($end, $expr->getRegex()->hasEndFlag());
  26. }
  27. /**
  28. * @dataProvider getHasJokersData
  29. */
  30. public function testHasJokers($regex, $start, $end)
  31. {
  32. $expr = new Expression($regex);
  33. $this->assertEquals($start, $expr->getRegex()->hasStartJoker());
  34. $this->assertEquals($end, $expr->getRegex()->hasEndJoker());
  35. }
  36. /**
  37. * @dataProvider getSetFlagsData
  38. */
  39. public function testSetFlags($regex, $start, $end, $expected)
  40. {
  41. $expr = new Expression($regex);
  42. $expr->getRegex()->setStartFlag($start)->setEndFlag($end);
  43. $this->assertEquals($expected, $expr->render());
  44. }
  45. /**
  46. * @dataProvider getSetJokersData
  47. */
  48. public function testSetJokers($regex, $start, $end, $expected)
  49. {
  50. $expr = new Expression($regex);
  51. $expr->getRegex()->setStartJoker($start)->setEndJoker($end);
  52. $this->assertEquals($expected, $expr->render());
  53. }
  54. public function testOptions()
  55. {
  56. $expr = new Expression('~abc~is');
  57. $expr->getRegex()->removeOption('i')->addOption('m');
  58. $this->assertEquals('~abc~sm', $expr->render());
  59. }
  60. public function testMixFlagsAndJokers()
  61. {
  62. $expr = new Expression('~^.*abc.*$~is');
  63. $expr->getRegex()->setStartFlag(false)->setEndFlag(false)->setStartJoker(false)->setEndJoker(false);
  64. $this->assertEquals('~abc~is', $expr->render());
  65. $expr->getRegex()->setStartFlag(true)->setEndFlag(true)->setStartJoker(true)->setEndJoker(true);
  66. $this->assertEquals('~^.*abc.*$~is', $expr->render());
  67. }
  68. /**
  69. * @dataProvider getReplaceJokersTestData
  70. */
  71. public function testReplaceJokers($regex, $expected)
  72. {
  73. $expr = new Expression($regex);
  74. $expr = $expr->getRegex()->replaceJokers('@');
  75. $this->assertEquals($expected, $expr->renderPattern());
  76. }
  77. public function getHasFlagsData()
  78. {
  79. return array(
  80. array('~^abc~', true, false),
  81. array('~abc$~', false, true),
  82. array('~abc~', false, false),
  83. array('~^abc$~', true, true),
  84. array('~^abc\\$~', true, false),
  85. );
  86. }
  87. public function getHasJokersData()
  88. {
  89. return array(
  90. array('~.*abc~', true, false),
  91. array('~abc.*~', false, true),
  92. array('~abc~', false, false),
  93. array('~.*abc.*~', true, true),
  94. array('~.*abc\\.*~', true, false),
  95. );
  96. }
  97. public function getSetFlagsData()
  98. {
  99. return array(
  100. array('~abc~', true, false, '~^abc~'),
  101. array('~abc~', false, true, '~abc$~'),
  102. array('~abc~', false, false, '~abc~'),
  103. array('~abc~', true, true, '~^abc$~'),
  104. );
  105. }
  106. public function getSetJokersData()
  107. {
  108. return array(
  109. array('~abc~', true, false, '~.*abc~'),
  110. array('~abc~', false, true, '~abc.*~'),
  111. array('~abc~', false, false, '~abc~'),
  112. array('~abc~', true, true, '~.*abc.*~'),
  113. );
  114. }
  115. public function getReplaceJokersTestData()
  116. {
  117. return array(
  118. array('~.abc~', '@abc'),
  119. array('~\\.abc~', '\\.abc'),
  120. array('~\\\\.abc~', '\\\\@abc'),
  121. array('~\\\\\\.abc~', '\\\\\\.abc'),
  122. );
  123. }
  124. }