This commit is contained in:
Paolo A
2024-08-13 13:44:16 +00:00
parent 1bbb23088d
commit e796d76612
4001 changed files with 30101 additions and 40075 deletions

View File

@@ -10,7 +10,6 @@ use PHPUnit\Framework\Constraint\FileExists;
use PHPUnit\Framework\Constraint\LogicalNot;
use PHPUnit\Framework\Constraint\RegularExpression;
use PHPUnit\Framework\InvalidArgumentException;
use PHPUnit\Util\InvalidArgumentHelper;
/**
* @internal This class is not meant to be used or overwritten outside the framework itself.
@@ -29,19 +28,11 @@ abstract class Assert extends PHPUnit
public static function assertArraySubset($subset, $array, bool $checkForIdentity = false, string $msg = ''): void
{
if (! (is_array($subset) || $subset instanceof ArrayAccess)) {
if (class_exists(InvalidArgumentException::class)) {
throw InvalidArgumentException::create(1, 'array or ArrayAccess');
} else {
throw InvalidArgumentHelper::factory(1, 'array or ArrayAccess');
}
throw InvalidArgumentException::create(1, 'array or ArrayAccess');
}
if (! (is_array($array) || $array instanceof ArrayAccess)) {
if (class_exists(InvalidArgumentException::class)) {
throw InvalidArgumentException::create(2, 'array or ArrayAccess');
} else {
throw InvalidArgumentHelper::factory(2, 'array or ArrayAccess');
}
throw InvalidArgumentException::create(2, 'array or ArrayAccess');
}
$constraint = new ArraySubset($subset, $checkForIdentity);

View File

@@ -3,6 +3,7 @@
namespace Illuminate\Testing\Constraints;
use Illuminate\Database\Connection;
use Illuminate\Database\Query\Expression;
use PHPUnit\Framework\Constraint\Constraint;
class HasInDatabase extends Constraint
@@ -111,6 +112,10 @@ class HasInDatabase extends Constraint
*/
public function toString($options = 0): string
{
return json_encode($this->data, $options);
foreach ($this->data as $key => $data) {
$output[$key] = $data instanceof Expression ? (string) $data : $data;
}
return json_encode($output ?? [], $options);
}
}

View File

@@ -10,6 +10,7 @@ use Illuminate\Support\Arr;
use Mockery;
use Mockery\Exception\NoMatchingExpectationException;
use PHPUnit\Framework\TestCase as PHPUnitTestCase;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
@@ -52,7 +53,14 @@ class PendingCommand
protected $expectedExitCode;
/**
* Determine if command has executed.
* The unexpected exit code.
*
* @var int
*/
protected $unexpectedExitCode;
/**
* Determine if the command has executed.
*
* @var bool
*/
@@ -133,6 +141,19 @@ class PendingCommand
return $this;
}
/**
* Specify output that should never be printed when the command runs.
*
* @param string $output
* @return $this
*/
public function doesntExpectOutput($output)
{
$this->test->unexpectedOutput[$output] = false;
return $this;
}
/**
* Specify a table that should be printed when the command runs.
*
@@ -144,12 +165,24 @@ class PendingCommand
*/
public function expectsTable($headers, $rows, $tableStyle = 'default', array $columnStyles = [])
{
$this->test->expectedTables[] = [
'headers' => (array) $headers,
'rows' => $rows instanceof Arrayable ? $rows->toArray() : $rows,
'tableStyle' => $tableStyle,
'columnStyles' => $columnStyles,
];
$table = (new Table($output = new BufferedOutput))
->setHeaders((array) $headers)
->setRows($rows instanceof Arrayable ? $rows->toArray() : $rows)
->setStyle($tableStyle);
foreach ($columnStyles as $columnIndex => $columnStyle) {
$table->setColumnStyle($columnIndex, $columnStyle);
}
$table->render();
$lines = array_filter(
explode(PHP_EOL, $output->fetch())
);
foreach ($lines as $line) {
$this->expectsOutput($line);
}
return $this;
}
@@ -167,6 +200,39 @@ class PendingCommand
return $this;
}
/**
* Assert that the command does not have the given exit code.
*
* @param int $exitCode
* @return $this
*/
public function assertNotExitCode($exitCode)
{
$this->unexpectedExitCode = $exitCode;
return $this;
}
/**
* Assert that the command has the success exit code.
*
* @return $this
*/
public function assertSuccessful()
{
return $this->assertExitCode(Command::SUCCESS);
}
/**
* Assert that the command does not have the success exit code.
*
* @return $this
*/
public function assertFailed()
{
return $this->assertNotExitCode(Command::SUCCESS);
}
/**
* Execute the command.
*
@@ -205,9 +271,15 @@ class PendingCommand
$this->expectedExitCode, $exitCode,
"Expected status code {$this->expectedExitCode} but received {$exitCode}."
);
} elseif (! is_null($this->unexpectedExitCode)) {
$this->test->assertNotEquals(
$this->unexpectedExitCode, $exitCode,
"Unexpected status code {$this->unexpectedExitCode} was received."
);
}
$this->verifyExpectations();
$this->flushExpectations();
return $exitCode;
}
@@ -238,6 +310,10 @@ class PendingCommand
if (count($this->test->expectedOutput)) {
$this->test->fail('Output "'.Arr::first($this->test->expectedOutput).'" was not printed.');
}
if ($output = array_search(true, $this->test->unexpectedOutput)) {
$this->test->fail('Output "'.$output.'" was printed.');
}
}
/**
@@ -287,8 +363,6 @@ class PendingCommand
->shouldAllowMockingProtectedMethods()
->shouldIgnoreMissing();
$this->applyTableOutputExpectations($mock);
foreach ($this->test->expectedOutput as $i => $output) {
$mock->shouldReceive('doWrite')
->once()
@@ -299,37 +373,30 @@ class PendingCommand
});
}
foreach ($this->test->unexpectedOutput as $output => $displayed) {
$mock->shouldReceive('doWrite')
->ordered()
->with($output, Mockery::any())
->andReturnUsing(function () use ($output) {
$this->test->unexpectedOutput[$output] = true;
});
}
return $mock;
}
/**
* Apply the output table expectations to the mock.
* Flush the expectations from the test case.
*
* @param \Mockery\MockInterface $mock
* @return void
*/
private function applyTableOutputExpectations($mock)
protected function flushExpectations()
{
foreach ($this->test->expectedTables as $consoleTable) {
$table = (new Table($output = new BufferedOutput))
->setHeaders($consoleTable['headers'])
->setRows($consoleTable['rows'])
->setStyle($consoleTable['tableStyle']);
foreach ($consoleTable['columnStyles'] as $columnIndex => $columnStyle) {
$table->setColumnStyle($columnIndex, $columnStyle);
}
$table->render();
$lines = array_filter(
preg_split("/\n/", $output->fetch())
);
foreach ($lines as $line) {
$this->expectsOutput($line);
}
}
$this->test->expectedOutput = [];
$this->test->unexpectedOutput = [];
$this->test->expectedTables = [];
$this->test->expectedQuestions = [];
$this->test->expectedChoices = [];
}
/**

File diff suppressed because it is too large Load Diff

View File

@@ -14,10 +14,11 @@
}
],
"require": {
"php": "^7.2.5|^8.0",
"illuminate/contracts": "^7.0",
"illuminate/support": "^7.0",
"symfony/polyfill-php73": "^1.17"
"php": "^7.3|^8.0",
"illuminate/collections": "^8.0",
"illuminate/contracts": "^8.0",
"illuminate/macroable": "^8.0",
"illuminate/support": "^8.0"
},
"autoload": {
"psr-4": {
@@ -26,15 +27,16 @@
},
"extra": {
"branch-alias": {
"dev-master": "7.x-dev"
"dev-master": "8.x-dev"
}
},
"suggest": {
"illuminate/console": "Required to assert console commands (^7.0).",
"illuminate/database": "Required to assert databases (^7.0).",
"illuminate/http": "Required to assert responses (^7.0).",
"mockery/mockery": "Required to use mocking (~1.3.3|^1.4.2).",
"phpunit/phpunit": "Required to use assertions and run tests (^8.4|^9.3.3)."
"brianium/paratest": "Required to run tests in parallel (^6.0).",
"illuminate/console": "Required to assert console commands (^8.0).",
"illuminate/database": "Required to assert databases (^8.0).",
"illuminate/http": "Required to assert responses (^8.0).",
"mockery/mockery": "Required to use mocking (^1.4.4).",
"phpunit/phpunit": "Required to use assertions and run tests (^8.5.19|^9.5.8)."
},
"config": {
"sort-packages": true