GlobTest.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Finder\Finder;
  13. use Symfony\Component\Finder\Glob;
  14. class GlobTest extends TestCase
  15. {
  16. public function testGlobToRegexDelimiters()
  17. {
  18. $this->assertEquals('#^(?=[^\.])\#$#', Glob::toRegex('#'));
  19. $this->assertEquals('#^\.[^/]*$#', Glob::toRegex('.*'));
  20. $this->assertEquals('^\.[^/]*$', Glob::toRegex('.*', true, true, ''));
  21. $this->assertEquals('/^\.[^/]*$/', Glob::toRegex('.*', true, true, '/'));
  22. }
  23. public function testGlobToRegexDoubleStarStrictDots()
  24. {
  25. $finder = new Finder();
  26. $finder->ignoreDotFiles(false);
  27. $regex = Glob::toRegex('/**/*.neon');
  28. foreach ($finder->in(__DIR__) as $k => $v) {
  29. $k = str_replace(DIRECTORY_SEPARATOR, '/', $k);
  30. if (preg_match($regex, substr($k, strlen(__DIR__)))) {
  31. $match[] = substr($k, 10 + strlen(__DIR__));
  32. }
  33. }
  34. sort($match);
  35. $this->assertSame(array('one/b/c.neon', 'one/b/d.neon'), $match);
  36. }
  37. public function testGlobToRegexDoubleStarNonStrictDots()
  38. {
  39. $finder = new Finder();
  40. $finder->ignoreDotFiles(false);
  41. $regex = Glob::toRegex('/**/*.neon', false);
  42. foreach ($finder->in(__DIR__) as $k => $v) {
  43. $k = str_replace(DIRECTORY_SEPARATOR, '/', $k);
  44. if (preg_match($regex, substr($k, strlen(__DIR__)))) {
  45. $match[] = substr($k, 10 + strlen(__DIR__));
  46. }
  47. }
  48. sort($match);
  49. $this->assertSame(array('.dot/b/c.neon', '.dot/b/d.neon', 'one/b/c.neon', 'one/b/d.neon'), $match);
  50. }
  51. public function testGlobToRegexDoubleStarWithoutLeadingSlash()
  52. {
  53. $finder = new Finder();
  54. $finder->ignoreDotFiles(false);
  55. $regex = Glob::toRegex('/Fixtures/one/**');
  56. foreach ($finder->in(__DIR__) as $k => $v) {
  57. $k = str_replace(DIRECTORY_SEPARATOR, '/', $k);
  58. if (preg_match($regex, substr($k, strlen(__DIR__)))) {
  59. $match[] = substr($k, 10 + strlen(__DIR__));
  60. }
  61. }
  62. sort($match);
  63. $this->assertSame(array('one/a', 'one/b', 'one/b/c.neon', 'one/b/d.neon'), $match);
  64. }
  65. public function testGlobToRegexDoubleStarWithoutLeadingSlashNotStrictLeadingDot()
  66. {
  67. $finder = new Finder();
  68. $finder->ignoreDotFiles(false);
  69. $regex = Glob::toRegex('/Fixtures/one/**', false);
  70. foreach ($finder->in(__DIR__) as $k => $v) {
  71. $k = str_replace(DIRECTORY_SEPARATOR, '/', $k);
  72. if (preg_match($regex, substr($k, strlen(__DIR__)))) {
  73. $match[] = substr($k, 10 + strlen(__DIR__));
  74. }
  75. }
  76. sort($match);
  77. $this->assertSame(array('one/.dot', 'one/a', 'one/b', 'one/b/c.neon', 'one/b/d.neon'), $match);
  78. }
  79. }