Files
pterodactyl-panel/tests/TestCase.php
T

120 lines
4.0 KiB
PHP
Raw Normal View History

2015-12-06 13:58:49 -05:00
<?php
2026-10-05 18:10:18 -04:00
declare(strict_types=1);
2021-01-23 12:09:16 -08:00
namespace Pterodactyl\Tests;
2015-12-06 13:58:49 -05:00
2021-01-23 12:09:16 -08:00
use Carbon\Carbon;
use Carbon\CarbonImmutable;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
2026-10-05 18:10:18 -04:00
use Illuminate\Routing\Events\RouteMatched;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Process;
use Illuminate\Support\Sleep;
use Illuminate\Support\Str;
use Illuminate\Testing\TestResponse;
use Pterodactyl\Tests\Support\OpenApiSpecValidator;
2015-12-06 13:58:49 -05:00
abstract class TestCase extends BaseTestCase
{
2026-10-05 18:10:18 -04:00
private ?string $lastMatchedRoute = null;
2017-10-07 23:29:08 -05:00
/**
* Setup tests.
*/
2026-10-05 18:10:18 -04:00
protected function setUp(): void
{
parent::setUp();
2017-10-07 23:29:08 -05:00
2026-10-05 18:10:18 -04:00
// When ROUTE_COVERAGE_FILE is set, append every route the suite dispatches so
// tooling can diff the hit list against `route:list` for endpoint coverage.
if ($file = env('ROUTE_COVERAGE_FILE')) {
Event::listen(RouteMatched::class, function (RouteMatched $event) use ($file) {
file_put_contents(
$file,
implode('|', $event->route->methods()).' /'.mb_ltrim($event->route->uri(), '/').PHP_EOL,
FILE_APPEND | LOCK_EX,
);
});
}
// When OPENAPI_VALIDATE is set, every JSON success response the suite produces
// is checked against the generated OpenAPI spec, turning contract drift into
// test failures. Requires a freshly generated storage spec (composer docs:openapi).
if (env('OPENAPI_VALIDATE') && OpenApiSpecValidator::available()) {
Event::listen(RouteMatched::class, function (RouteMatched $event) {
$this->lastMatchedRoute = $event->route->uri();
});
}
Http::preventStrayRequests();
Process::preventStrayProcesses();
Sleep::fake();
Str::createRandomStringsNormally();
Str::createUuidsNormally();
$now = Carbon::now()->startOfSecond();
Carbon::setTestNow($now);
CarbonImmutable::setTestNow($now);
2021-01-23 12:09:16 -08:00
// Why, you ask? If we don't force this to false it is possible for certain exceptions
// to show their error message properly in the integration test output, but not actually
2021-01-23 12:09:16 -08:00
// be setup correctly to display their message in production.
//
// If we expect a message in a test, and it isn't showing up (rather, showing the generic
// "an error occurred" message), we can probably assume that the exception isn't one that
// is recognized as being user viewable.
config()->set('app.debug', false);
2017-10-07 23:29:08 -05:00
$this->setKnownUuidFactory();
}
/**
* Tear down tests.
*/
protected function tearDown(): void
{
parent::tearDown();
2021-01-23 12:09:16 -08:00
Carbon::setTestNow();
CarbonImmutable::setTestNow();
}
2017-10-07 23:29:08 -05:00
/**
* Handles the known UUID handling in certain unit tests. Use the "KnownUuid" trait
* in order to enable this ability.
*/
2026-10-05 18:10:18 -04:00
public function setKnownUuidFactory(): void
2017-10-07 23:29:08 -05:00
{
// do nothing
}
2026-10-05 18:10:18 -04:00
public function call($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null): TestResponse
{
$this->lastMatchedRoute = null;
$response = parent::call($method, $uri, $parameters, $cookies, $files, $server, $content);
if (env('OPENAPI_VALIDATE') && $this->lastMatchedRoute !== null && OpenApiSpecValidator::available()) {
$errors = OpenApiSpecValidator::validateResponse(
$method,
$this->lastMatchedRoute,
$response->getStatusCode(),
(string) $response->getContent(),
);
$this->assertSame([], $errors, sprintf(
"Response for [%s /%s] violates the OpenAPI contract:\n - %s",
mb_strtoupper($method),
mb_ltrim($this->lastMatchedRoute, '/'),
implode("\n - ", $errors),
));
}
return $response;
}
2015-12-06 13:58:49 -05:00
}