-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathpest-bootstrap.php
More file actions
277 lines (216 loc) · 14.7 KB
/
Copy pathpest-bootstrap.php
File metadata and controls
277 lines (216 loc) · 14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
declare(strict_types=1);
$autoloadCandidates = [
getcwd() . '/server_vendor/autoload.php',
getcwd() . '/vendor/autoload.php',
];
foreach ($autoloadCandidates as $candidate) {
if (is_file($candidate)) {
require_once $candidate;
break;
}
}
if (class_exists('Illuminate\Support\Str') && !Illuminate\Support\Str::hasMacro('humanize')) {
Illuminate\Support\Str::macro('humanize', function (string $value, bool $title = true): string {
$humanized = str_replace(['-', '_'], ' ', Illuminate\Support\Str::snake($value));
return $title ? Illuminate\Support\Str::title($humanized) : $humanized;
});
}
if (!function_exists('config')) {
function config(?string $key = null, mixed $default = null): mixed
{
if (class_exists('Illuminate\Container\Container')) {
$container = Illuminate\Container\Container::getInstance();
if ($container->bound('config')) {
$repository = $container->make('config');
return $key === null ? $repository : $repository->get($key, $default);
}
}
return $default;
}
}
if (class_exists('Illuminate\Container\Container') && class_exists('Illuminate\Support\Facades\Facade')) {
$app = Illuminate\Container\Container::getInstance();
if (!method_exists($app, 'environment')) {
if (!class_exists('Fleetbase\TestSupport\TestContainer')) {
eval('namespace Fleetbase\TestSupport; class TestContainer extends \Illuminate\Container\Container { public array $registeredProviders = []; public function environment(array|string ...$environments): bool|string { return $environments === [] ? "testing" : in_array("testing", is_array($environments[0] ?? null) ? $environments[0] : $environments, true); } public function runningUnitTests(): bool { return true; } public function runningInConsole(): bool { return true; } public function register($provider, $force = false) { $this->registeredProviders[] = $provider; return $provider; } }');
}
$app = new Fleetbase\TestSupport\TestContainer();
Illuminate\Container\Container::setInstance($app);
}
Illuminate\Support\Facades\Facade::setFacadeApplication($app);
if (!$app->bound('http') && class_exists('Illuminate\Http\Client\Factory')) {
$app->singleton('http', fn () => new Illuminate\Http\Client\Factory());
}
if (!$app->bound('cache') && class_exists('Illuminate\Cache\Repository') && class_exists('Illuminate\Cache\ArrayStore')) {
$app->singleton('cache', fn () => new Illuminate\Cache\Repository(new Illuminate\Cache\ArrayStore()));
}
if (!$app->bound('responsecache')) {
if (!class_exists('Fleetbase\TestSupport\ResponseCacheManager')) {
eval('namespace Fleetbase\TestSupport; class ResponseCacheManager { public function clear(): bool { return true; } }');
}
$app->singleton('responsecache', fn () => new Fleetbase\TestSupport\ResponseCacheManager());
}
if (!$app->bound('config') && class_exists('Illuminate\Config\Repository')) {
$app->singleton('config', fn () => new Illuminate\Config\Repository([
'app' => ['url' => 'https://api.example.test'],
'api' => ['cache' => ['enabled' => false]],
'fleetbase' => ['connection' => ['db' => 'testing']],
]));
}
if (!$app->bound('log') && class_exists('Psr\Log\NullLogger')) {
if (!class_exists('Fleetbase\TestSupport\LoggerManager')) {
eval('namespace Fleetbase\TestSupport; class LoggerManager extends \Psr\Log\NullLogger { public static array $records = []; public function channel(?string $name = null): self { return $this; } public function log($level, string|\Stringable $message, array $context = []): void { self::$records[] = compact("level", "message", "context"); } }');
}
$app->singleton('log', fn () => new Fleetbase\TestSupport\LoggerManager());
}
if (!$app->bound('router')) {
if (!class_exists('Fleetbase\TestSupport\RouteRegistrar')) {
eval('namespace Fleetbase\TestSupport; class RouteRegistrar { public static array $routes = []; public static function reset(): void { self::$routes = []; } public function prefix(string $prefix): self { return $this; } public function namespace(string $namespace): self { return $this; } public function group(array|\Closure $attributes, ?\Closure $callback = null): self { ($callback ?? $attributes)($this); return $this; } public function get(string $uri, mixed $action): self { self::$routes[] = ["GET", $uri, $action]; return $this; } public function post(string $uri, mixed $action): self { self::$routes[] = ["POST", $uri, $action]; return $this; } public function fleetbaseRoutes(string $resource, ?\Closure $callback = null): self { self::$routes[] = ["RESOURCE", $resource, null]; if ($callback) { $callback($this, fn (string $method): string => $resource . "Controller@" . $method); } return $this; } }');
}
$app->singleton('router', fn () => new Fleetbase\TestSupport\RouteRegistrar());
}
}
if (!function_exists('url')) {
function url(?string $path = null, mixed $parameters = [], ?bool $secure = null): string
{
$base = $secure === false ? 'http://api.example.test' : 'https://api.example.test';
return rtrim($base, '/') . '/' . ltrim((string) $path, '/');
}
}
if (!function_exists('response')) {
function response(): object
{
return new class {
public function json(mixed $data = [], int $status = 200, array $headers = []): Illuminate\Http\JsonResponse
{
return new Illuminate\Http\JsonResponse($data, $status, $headers);
}
};
}
}
if (!function_exists('abort')) {
function abort(int $code, string $message = '', array $headers = []): never
{
throw new Symfony\Component\HttpKernel\Exception\HttpException($code, $message, null, $headers);
}
}
if (!function_exists('event')) {
function event(object $event): object
{
if (class_exists('Fleetbase\TestSupport\EventRecorder')) {
Fleetbase\TestSupport\EventRecorder::record($event);
}
return $event;
}
}
if (!function_exists('app')) {
function app(?string $abstract = null, array $parameters = []): mixed
{
if (class_exists('Illuminate\Container\Container')) {
$container = Illuminate\Container\Container::getInstance();
return $abstract === null ? $container : $container->make($abstract, $parameters);
}
return $abstract === null ? null : new $abstract(...array_values($parameters));
}
}
if (!function_exists('request')) {
function request(?string $key = null, mixed $default = null): mixed
{
$request = class_exists('Illuminate\Http\Request') ? Illuminate\Http\Request::create('/') : new stdClass();
return $key === null ? $request : $default;
}
}
if (!function_exists('session')) {
function session(array|string|null $key = null, mixed $default = null): mixed
{
static $values = [];
if (is_array($key)) {
$values = array_merge($values, $key);
return null;
}
if ($key !== null) {
return $values[$key] ?? $default;
}
return new class($values) {
public function __construct(private array $values)
{
}
public function missing(string $key): bool
{
return !array_key_exists($key, $this->values);
}
public function has(string $key): bool
{
return array_key_exists($key, $this->values);
}
public function get(string $key, mixed $default = null): mixed
{
return $this->values[$key] ?? $default;
}
};
}
}
if (!function_exists('now') && class_exists('Illuminate\Support\Carbon')) {
function now($tz = null): Illuminate\Support\Carbon
{
return Illuminate\Support\Carbon::now($tz);
}
}
if (!trait_exists('Illuminate\Foundation\Auth\Access\AuthorizesRequests')) {
eval('namespace Illuminate\Foundation\Auth\Access; trait AuthorizesRequests {}');
}
if (!class_exists('Illuminate\Foundation\Auth\User')) {
eval('namespace Illuminate\Foundation\Auth; class User extends \Illuminate\Database\Eloquent\Model {}');
}
if (!class_exists('Fleetbase\Models\Customer') && class_exists('Illuminate\Database\Eloquent\Model')) {
eval('namespace Fleetbase\Models; class Customer extends \Illuminate\Database\Eloquent\Model { protected $table = "customers"; protected $primaryKey = "uuid"; public $incrementing = false; protected $keyType = "string"; }');
}
if (!class_exists('Illuminate\Pagination\Paginator')) {
eval('namespace Illuminate\Pagination; class Paginator implements \JsonSerializable { protected $items; public function __construct($items, protected int $perPage, protected ?int $currentPage = null, protected array $options = []) { $this->items = $items instanceof \Illuminate\Support\Collection ? $items : collect($items); } public static function resolveCurrentPage($pageName = "page", $default = 1): int { return $default; } public static function resolveCurrentPath($default = "/"): string { return $default; } public function first() { return $this->items->first(); } public function mapInto(string $class) { return $this->items->mapInto($class); } public function toBase() { return $this->items->toBase(); } public function jsonSerialize(): mixed { return $this->toArray(); } public function toArray(): array { return ["data" => $this->items->values()->all(), "per_page" => $this->perPage, "current_page" => $this->currentPage ?? 1]; } } class LengthAwarePaginator extends Paginator { public function __construct($items, protected int $total, int $perPage, ?int $currentPage = null, array $options = []) { parent::__construct($items, $perPage, $currentPage, $options); } public function toArray(): array { return array_merge(parent::toArray(), ["total" => $this->total, "last_page" => max(1, (int) ceil($this->total / $this->perPage))]); } }');
}
if (!trait_exists('Illuminate\Foundation\Bus\Dispatchable')) {
eval('namespace Illuminate\Foundation\Bus; trait Dispatchable {}');
}
if (!trait_exists('Illuminate\Foundation\Bus\DispatchesJobs')) {
eval('namespace Illuminate\Foundation\Bus; trait DispatchesJobs {}');
}
if (!trait_exists('Illuminate\Foundation\Events\Dispatchable')) {
if (!class_exists('Fleetbase\TestSupport\EventRecorder')) {
eval('namespace Fleetbase\TestSupport; class EventRecorder { public static array $events = []; public static function record(object $event): object { self::$events[] = $event; return $event; } public static function reset(): void { self::$events = []; } }');
}
eval('namespace Illuminate\Foundation\Events; trait Dispatchable { public static function dispatch(...$arguments): object { return \Fleetbase\TestSupport\EventRecorder::record(new static(...$arguments)); } }');
}
if (!trait_exists('Illuminate\Foundation\Validation\ValidatesRequests')) {
eval('namespace Illuminate\Foundation\Validation; trait ValidatesRequests {}');
}
if (!class_exists('Illuminate\Foundation\Http\FormRequest') && class_exists('Illuminate\Http\Request')) {
eval('namespace Illuminate\Foundation\Http; class FormRequest extends \Illuminate\Http\Request { public function authorize(): bool { return true; } public function rules(): array { return []; } public function responseWithErrors(\Illuminate\Contracts\Validation\Validator $validator) { return $validator; } }');
}
if (!interface_exists('Fleetbase\Ai\Contracts\AIContextCapabilityInterface')) {
eval('namespace Fleetbase\Ai\Contracts; interface AIContextCapabilityInterface {}');
}
if (!interface_exists('Fleetbase\Ai\Contracts\AIActionCapabilityInterface')) {
eval('namespace Fleetbase\Ai\Contracts; interface AIActionCapabilityInterface {}');
}
if (!class_exists('Fleetbase\Ai\Models\AiTask')) {
eval('namespace Fleetbase\Ai\Models; class AiTask { public function __construct(array $attributes = []) { foreach ($attributes as $key => $value) { $this->{$key} = $value; } } }');
}
if (!class_exists('Fleetbase\Ai\Support\Capabilities\AbstractAICapability')) {
eval('namespace Fleetbase\Ai\Support\Capabilities; abstract class AbstractAICapability {}');
}
if (!class_exists('Fleetbase\Ai\Support\AiQueryableResource')) {
eval('namespace Fleetbase\Ai\Support; class AiQueryableResource { public string $key; public array $fields; public array $aliases; public function __construct(string $key, string $label = "", string $module = "", string $modelClass = "", string $permission = "", array $aliases = [], array $fields = [], array $sampleFields = [], ?string $locationField = null, ?string $directivePermission = null, int $maxLimit = 100) { $this->key = $key; $this->fields = $fields; $this->aliases = $aliases; } public function hasField(string $field): bool { return array_key_exists($field, $this->fields); } }');
}
if (!class_exists('Fleetbase\Ai\Support\AiQueryRegistry')) {
eval('namespace Fleetbase\Ai\Support; class AiQueryRegistry { private array $resources = []; public function register(AiQueryableResource $resource): void { $this->resources[$resource->key] = $resource; foreach ($resource->aliases as $alias) { $this->resources[$alias] = $resource; } } public function find(string $key): ?AiQueryableResource { return $this->resources[$key] ?? null; } }');
}
if (!class_exists('Fleetbase\Ai\Support\AiRelativeDateResolver') && class_exists('Illuminate\Support\Carbon')) {
eval('namespace Fleetbase\Ai\Support; class AiRelativeDateResolver { public function __construct($parser = null) {} public function resolveDateTime(string $prompt, ?string $timezone = null): ?\Illuminate\Support\Carbon { if (preg_match("/(\d+)\s+days?\s+from\s+now/i", $prompt, $matches)) { return \Illuminate\Support\Carbon::now($timezone)->addDays((int) $matches[1]); } return null; } public function resolveWindow(string $prompt, ?string $timezone = null): ?array { $timezone = $timezone ?: date_default_timezone_get(); $now = \Illuminate\Support\Carbon::now($timezone); if (str_contains(strtolower($prompt), "last week")) { $start = $now->copy()->subWeek()->startOfWeek(); $end = $now->copy()->subWeek()->endOfWeek(); return ["label" => "last week", "timezone" => $timezone, "start" => $start, "end" => $end]; } if (str_contains(strtolower($prompt), "yesterday")) { $start = $now->copy()->subDay()->startOfDay(); $end = $now->copy()->subDay()->endOfDay(); return ["label" => "yesterday", "timezone" => $timezone, "start" => $start, "end" => $end]; } return null; } }');
}
set_error_handler(function (int $severity, string $message): bool {
if (str_contains($message, '/pestphp/pest/vendor/autoload.php')) {
return true;
}
return false;
}, E_WARNING);