Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Add doesntExpectOutputToContain assertion method #42096

Merged
merged 2 commits into from
Apr 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ trait InteractsWithConsole
public $expectedOutput = [];

/**
* All of the expected text to be present on the output.
* All of the expected text to be present in the output.
*
* @var array
*/
Expand All @@ -36,6 +36,13 @@ trait InteractsWithConsole
*/
public $unexpectedOutput = [];

/**
* All of the text that is not expected to be present in the output.
*
* @var array
*/
public $unexpectedOutputSubstrings = [];

/**
* All of the expected output tables.
*
Expand Down
26 changes: 26 additions & 0 deletions src/Illuminate/Testing/PendingCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,19 @@ public function expectsOutputToContain($string)
return $this;
}

/**
* Specify that the given string shouldn't be contained in the command output.
*
* @param string $string
* @return $this
*/
public function doesntExpectOutputToContain($string)
{
$this->test->unexpectedOutputSubstrings[$string] = false;

return $this;
}

/**
* Specify a table that should be printed when the command runs.
*
Expand Down Expand Up @@ -331,6 +344,10 @@ protected function verifyExpectations()
if ($output = array_search(true, $this->test->unexpectedOutput)) {
$this->test->fail('Output "'.$output.'" was printed.');
}

if ($output = array_search(true, $this->test->unexpectedOutputSubstrings)) {
$this->test->fail('Output "'.$output.'" was printed.');
}
}

/**
Expand Down Expand Up @@ -407,6 +424,14 @@ private function createABufferedOutputMock()
});
}

foreach ($this->test->unexpectedOutputSubstrings as $text => $displayed) {
$mock->shouldReceive('doWrite')
->withArgs(fn ($output) => str_contains($output, $text))
->andReturnUsing(function () use ($text) {
$this->test->unexpectedOutputSubstrings[$text] = true;
});
}

return $mock;
}

Expand All @@ -420,6 +445,7 @@ protected function flushExpectations()
$this->test->expectedOutput = [];
$this->test->expectedOutputSubstrings = [];
$this->test->unexpectedOutput = [];
$this->test->unexpectedOutputSubstrings = [];
$this->test->expectedTables = [];
$this->test->expectedQuestions = [];
$this->test->expectedChoices = [];
Expand Down
10 changes: 10 additions & 0 deletions tests/Integration/Testing/ArtisanCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ public function test_console_command_that_fails_from_unexpected_output()
->assertExitCode(0);
}

public function test_console_command_that_fails_from_unexpected_output_substring()
{
$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('Output "Taylor Otwell" was printed.');

$this->artisan('contains')
->doesntExpectOutputToContain('Taylor Otwell')
->assertExitCode(0);
}

public function test_console_command_that_fails_from_missing_output()
{
$this->expectException(AssertionFailedError::class);
Expand Down