mirror of
https://github.com/coollabsio/coolify.git
synced 2026-12-06 10:45:53 +08:00
70 lines
2.4 KiB
PHP
70 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace Tests;
|
|
|
|
use Illuminate\Container\Container;
|
|
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
|
|
use Illuminate\Http\Client\StrayRequestException;
|
|
use Illuminate\Support\Facades\Facade;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Facades\Process;
|
|
use Psr\Http\Message\RequestInterface;
|
|
use Tests\Support\NoRealSshProcessFactory;
|
|
|
|
abstract class TestCase extends BaseTestCase
|
|
{
|
|
use CreatesApplication;
|
|
|
|
/** @var list<string> SSH commands and HTTP requests that the guards refused, also when the code caught the error */
|
|
private array $refusedConnections = [];
|
|
|
|
/**
|
|
* A test must not reach a real server. Fake SSH commands with fakeRemoteProcesses() and HTTP requests with
|
|
* Http::fake(). An unfaked SSH command or HTTP request throws at once instead of waiting for a timeout.
|
|
*/
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
Process::swap(new NoRealSshProcessFactory(function (string $command): void {
|
|
$this->refusedConnections[] = $command;
|
|
}));
|
|
Http::preventStrayRequests();
|
|
Http::globalMiddleware(fn (callable $handler) => function (RequestInterface $request, array $options) use ($handler) {
|
|
try {
|
|
return $handler($request, $options);
|
|
} catch (StrayRequestException $exception) {
|
|
$this->refusedConnections[] = $request->getMethod().' '.$request->getUri();
|
|
|
|
throw $exception;
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Fails a test that passed although the code under test caught a refused connection.
|
|
*/
|
|
protected function assertPostConditions(): void
|
|
{
|
|
parent::assertPostConditions();
|
|
|
|
if ($this->refusedConnections !== []) {
|
|
$this->fail("The test tried to reach a real server, and the code caught the error. Fake the SSH commands with fakeRemoteProcesses() and the HTTP requests with Http::fake():\n".implode("\n", $this->refusedConnections));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Laravel flushes the application on tear down but leaves it set as the facade
|
|
* application and container instance. Plain unit tests that mock facades or call
|
|
* helpers afterwards would then resolve bindings from that empty application.
|
|
*/
|
|
protected function tearDown(): void
|
|
{
|
|
parent::tearDown();
|
|
|
|
Facade::clearResolvedInstances();
|
|
Facade::setFacadeApplication(null);
|
|
Container::setInstance(null);
|
|
}
|
|
}
|