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;
|
2017-06-13 23:25:37 -05:00
|
|
|
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
|
|
|
|
2017-06-13 23:25:37 -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
|
2017-06-24 19:49:09 -05:00
|
|
|
{
|
|
|
|
|
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();
|
|
|
|
|
|
2025-12-30 15:27:11 -05:00
|
|
|
$now = Carbon::now()->startOfSecond();
|
|
|
|
|
|
|
|
|
|
Carbon::setTestNow($now);
|
|
|
|
|
CarbonImmutable::setTestNow($now);
|
2021-01-23 12:09:16 -08:00
|
|
|
|
2020-10-10 16:51:44 -07: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.
|
2020-10-10 16:51:44 -07:00
|
|
|
//
|
|
|
|
|
// 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();
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-14 13:30:55 -06:00
|
|
|
/**
|
|
|
|
|
* Tear down tests.
|
|
|
|
|
*/
|
2020-05-09 18:00:52 +02:00
|
|
|
protected function tearDown(): void
|
2018-01-14 13:30:55 -06:00
|
|
|
{
|
|
|
|
|
parent::tearDown();
|
|
|
|
|
|
2021-01-23 12:09:16 -08:00
|
|
|
Carbon::setTestNow();
|
|
|
|
|
CarbonImmutable::setTestNow();
|
2018-01-14 13:30:55 -06:00
|
|
|
}
|
|
|
|
|
|
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
|
2017-06-24 19:49:09 -05:00
|
|
|
}
|
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
|
|
|
}
|