RegexTest.php 3.9 KB

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