GlobTest.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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(['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(['.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(['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(['one/.dot', 'one/a', 'one/b', 'one/b/c.neon', 'one/b/d.neon'], $match);
  78. }
  79. }