Skip to content

Commit

Permalink
[12.x] Make Str::is() match multiline strings (#51196)
Browse files Browse the repository at this point in the history
* make Str::is() support multiline strings

* more tests with patterns containing new lines

* cs

* cs
  • Loading branch information
SjorsO authored Apr 25, 2024
1 parent 63797d3 commit baed746
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Illuminate/Process/PendingProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ public function withFakeHandlers(array $fakeHandlers)
protected function fakeFor(string $command)
{
return collect($this->fakeHandlers)
->first(fn ($handler, $pattern) => $pattern === '*' || Str::is($pattern, $command));
->first(fn ($handler, $pattern) => Str::is($pattern, $command));
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ public static function is($pattern, $value)
// If the given value is an exact match we can of course return true right
// from the beginning. Otherwise, we will translate asterisks and do an
// actual pattern match against the two strings to see if they match.
if ($pattern === $value) {
if ($pattern === '*' || $pattern === $value) {
return true;
}

Expand All @@ -443,7 +443,7 @@ public static function is($pattern, $value)
// pattern such as "library/*", making any string check convenient.
$pattern = str_replace('\*', '.*', $pattern);

if (preg_match('#^'.$pattern.'\z#u', $value) === 1) {
if (preg_match('#^'.$pattern.'\z#su', $value) === 1) {
return true;
}
}
Expand Down
25 changes: 24 additions & 1 deletion tests/Process/ProcessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public function testBasicProcessFakeWithMultiLineCommand()
$factory->preventStrayProcesses();

$factory->fake([
'*' => 'The output',
'*' => $expectedOutput = 'The output',
]);

$result = $factory->run(<<<'COMMAND'
Expand All @@ -167,6 +167,29 @@ public function testBasicProcessFakeWithMultiLineCommand()
COMMAND);

$this->assertSame(0, $result->exitCode());
$this->assertSame("$expectedOutput\n", $result->output());
}

public function testProcessFakeWithMultiLineCommand()
{
$factory = new Factory;

$factory->preventStrayProcesses();

$factory->fake([
'*--branch main*' => 'not this one',
'*--branch develop*' => $expectedOutput = 'yes thank you',
]);

$result = $factory->run(<<<'COMMAND'
git clone --depth 1 \
--single-branch \
--branch develop \
git://some-url .
COMMAND);

$this->assertSame(0, $result->exitCode());
$this->assertSame("$expectedOutput\n", $result->output());
}

public function testProcessFakeExitCodes()
Expand Down
46 changes: 46 additions & 0 deletions tests/Support/SupportStrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,52 @@ public function testIs()
$this->assertTrue(Str::is([null], null));
}

public function testIsWithMultilineStrings()
{
$this->assertFalse(Str::is('/', "/\n"));
$this->assertTrue(Str::is('/*', "/\n"));
$this->assertTrue(Str::is('*/*', "/\n"));
$this->assertTrue(Str::is('*/*', "\n/\n"));

$this->assertTrue(Str::is('*', "\n"));
$this->assertTrue(Str::is('*', "\n\n"));
$this->assertFalse(Str::is('', "\n"));
$this->assertFalse(Str::is('', "\n\n"));

$multilineValue = <<<'VALUE'
<?php
namespace Illuminate\Tests\Support;
use Exception;
VALUE;

$this->assertTrue(Str::is($multilineValue, $multilineValue));
$this->assertTrue(Str::is('*', $multilineValue));
$this->assertTrue(Str::is("*namespace Illuminate\Tests\*", $multilineValue));
$this->assertFalse(Str::is("namespace Illuminate\Tests\*", $multilineValue));
$this->assertFalse(Str::is("*namespace Illuminate\Tests", $multilineValue));
$this->assertTrue(Str::is('<?php*', $multilineValue));
$this->assertTrue(Str::is("<?php*namespace Illuminate\Tests\*", $multilineValue));
$this->assertFalse(Str::is('use Exception;', $multilineValue));
$this->assertFalse(Str::is('use Exception;*', $multilineValue));
$this->assertTrue(Str::is('*use Exception;', $multilineValue));

$this->assertTrue(Str::is("<?php\n\nnamespace Illuminate\Tests\*", $multilineValue));

$this->assertTrue(Str::is(<<<'PATTERN'
<?php
*
namespace Illuminate\Tests\*
PATTERN, $multilineValue));

$this->assertTrue(Str::is(<<<'PATTERN'
<?php
namespace Illuminate\Tests\*
PATTERN, $multilineValue));
}

public function testIsUrl()
{
$this->assertTrue(Str::isUrl('https://laravel.com'));
Expand Down
32 changes: 32 additions & 0 deletions tests/Support/SupportStringableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,38 @@ public function testIs()
$this->assertFalse($this->stringable('test')->is([]));
}

public function testIsWithMultilineStrings()
{
$this->assertFalse($this->stringable("/\n")->is('/'));
$this->assertTrue($this->stringable("/\n")->is('/*'));
$this->assertTrue($this->stringable("/\n")->is('*/*'));
$this->assertTrue($this->stringable("\n/\n")->is('*/*'));

$this->assertTrue($this->stringable("\n")->is('*'));
$this->assertTrue($this->stringable("\n\n")->is('*'));
$this->assertFalse($this->stringable("\n")->is(''));
$this->assertFalse($this->stringable("\n\n")->is(''));

$multilineValue = <<<'VALUE'
<?php
namespace Illuminate\Tests\Support;
use Exception;
VALUE;

$this->assertTrue($this->stringable($multilineValue)->is($multilineValue));
$this->assertTrue($this->stringable($multilineValue)->is('*'));
$this->assertTrue($this->stringable($multilineValue)->is("*namespace Illuminate\Tests\*"));
$this->assertFalse($this->stringable($multilineValue)->is("namespace Illuminate\Tests\*"));
$this->assertFalse($this->stringable($multilineValue)->is("*namespace Illuminate\Tests"));
$this->assertTrue($this->stringable($multilineValue)->is('<?php*'));
$this->assertTrue($this->stringable($multilineValue)->is("<?php*namespace Illuminate\Tests\*"));
$this->assertFalse($this->stringable($multilineValue)->is('use Exception;'));
$this->assertFalse($this->stringable($multilineValue)->is('use Exception;*'));
$this->assertTrue($this->stringable($multilineValue)->is('*use Exception;'));
}

public function testKebab()
{
$this->assertSame('laravel-php-framework', (string) $this->stringable('LaravelPhpFramework')->kebab());
Expand Down

0 comments on commit baed746

Please sign in to comment.