Files
appwrite/tests/unit/Utopia/RequestTest.php
Chirag Aggarwal d2230f8fe7 chore: bump PHPStan to level 4 and fix all new errors
Raises `phpstan.neon` level from 3 to 4 and fixes the 549 new errors
that level 4 surfaces across 157 files. Fixes are root-cause — no
`@phpstan-ignore`, no `@var` casts, no baseline entries, no widened
types. A handful of latent bugs were fixed along the way:

- `app/controllers/general.php`: path-traversal guard was negating
  `\substr(...)` before the strict comparison (`!\substr(...) === $base`
  was always `false === $base`). Rewritten as `\substr(...) !== $base`.
- `src/Appwrite/Platform/Modules/Databases/Http/Databases/Logs/XList.php`
  and `.../TablesDB/Logs/XList.php`: were importing the raw Matomo
  `DeviceDetector` (whose `getDevice()` returns `?int`) but treating the
  result as an array with `deviceName/deviceBrand/deviceModel` keys.
  Swapped to `Appwrite\Detector\Detector`, matching the wrapper already
  used a few lines below for `$os`/`$client`.
- `src/Appwrite/Platform/Modules/Functions/Workers/Builds.php`: a match
  key was checking `$resourceKey === 'functions'` when `$resourceKey`
  is `'functionId'|'siteId'` — always false. Switched to the intended
  `$resource->getCollection() === 'functions'` check.
- `src/Appwrite/OpenSSL/OpenSSL.php`: `encrypt()` return type tightened
  to `string|false` to match `openssl_encrypt`; this lets callers'
  `=== false` error handling remain meaningful.
- `app/controllers/api/messaging.php`: removed a dead
  `array_key_exists('from', [])` branch in the Msg91 provider (empty
  array literal; branch was unreachable).

Large cleanup categories across the 549 fixes:
- Removed redundant `?? default` on array offsets and expressions that
  PHPStan now knows are non-nullable.
- Removed unreachable statements (mostly `return;` after `throw` or
  `markTestSkipped()`).
- Removed redundant `is_array`/`is_string`/`is_bool`/`instanceof` checks
  on already-narrowed types.
- Added `default =>` arms (or throwing arms) to non-exhaustive matches
  on `string`/`mixed` input.
- Removed dead `$document === false` branches where method return types
  were tightened to non-nullable `Document`.
- Removed unused properties (`$version` on Etsy/Zoom OAuth2, `$paths` on
  Installer State, `$source` on MigrationsWorker, `$account2` on two
  GraphQL auth tests), unused traits (`ApiVectorsDB`, `DatabaseFixture`),
  and an unused `cleanupStaleExecutions` task method.
- Replaced `assertTrue(true)` and redundant `assertIsArray`/`assertIsString`/
  `assertNotNull` assertions with `addToAssertionCount(1)` or
  `assertNotEmpty` where the runtime type was already known.
2026-04-19 17:31:20 +05:30

202 lines
6.0 KiB
PHP

<?php
namespace Tests\Unit\Utopia;
use Appwrite\SDK\Method;
use Appwrite\SDK\Parameter;
use Appwrite\Utopia\Request;
use PHPUnit\Framework\TestCase;
use Swoole\Http\Request as SwooleRequest;
use Tests\Unit\Utopia\Request\Filters\First;
use Tests\Unit\Utopia\Request\Filters\Second;
use Utopia\Http\Route;
class RequestTest extends TestCase
{
protected ?Request $request = null;
public function setUp(): void
{
$this->request = new Request(new SwooleRequest());
}
public function testFilters(): void
{
$this->assertFalse($this->request->hasFilters());
$this->assertEmpty($this->request->getFilters());
$this->request->addFilter(new First());
$this->request->addFilter(new Second());
$this->assertTrue($this->request->hasFilters());
$this->assertCount(2, $this->request->getFilters());
$route = new Route(Request::METHOD_GET, '/test');
$route->label('sdk', new Method(
namespace: 'namespace',
group: 'group',
name: 'method',
description: 'description',
auth: [],
responses: [],
));
// set test header to prevent header populaten inside the request class
$this->request->addHeader('EXAMPLE', 'VALUE');
$this->request->setRoute($route);
$this->request->setQueryString([
'initial' => true,
'first' => false
]);
$output = $this->request->getParams();
$this->assertArrayHasKey('initial', $output);
$this->assertTrue($output['initial']);
$this->assertArrayHasKey('first', $output);
$this->assertTrue($output['first']);
$this->assertArrayHasKey('second', $output);
$this->assertTrue($output['second']);
$this->assertArrayNotHasKey('deleted', $output);
}
public function testGetParamsWithMultipleMethods(): void
{
$this->setupMultiMethodRoute();
// Pass only "foo", should match Method A
$this->request->setQueryString([
'foo' => 'valueFoo',
]);
$params = $this->request->getParams();
$this->assertArrayHasKey('foo', $params);
$this->assertSame('valueFoo', $params['foo']);
$this->assertArrayNotHasKey('baz', $params);
}
public function testGetParamsWithAllRequired(): void
{
$this->setupMultiMethodRoute();
// Pass "foo" and "bar", should match Method A
$this->request->setQueryString([
'foo' => 'valueFoo',
'bar' => 'valueBar',
]);
$params = $this->request->getParams();
$this->assertArrayHasKey('foo', $params);
$this->assertSame('valueFoo', $params['foo']);
$this->assertArrayHasKey('bar', $params);
$this->assertSame('valueBar', $params['bar']);
$this->assertArrayNotHasKey('baz', $params);
}
public function testGetParamsWithAllOptional(): void
{
$this->setupMultiMethodRoute();
// Pass only "bar", should match Method A
$this->request->setQueryString([
'bar' => 'valueBar',
]);
$params = $this->request->getParams();
$this->assertArrayHasKey('bar', $params);
$this->assertSame('valueBar', $params['bar']);
$this->assertArrayNotHasKey('foo', $params);
$this->assertArrayNotHasKey('baz', $params);
}
public function testGetParamsMatchesMethodB(): void
{
$this->setupMultiMethodRoute();
// Pass only "baz", should match Method B
$this->request->setQueryString([
'baz' => 'valueBaz',
]);
$params = $this->request->getParams();
$this->assertArrayHasKey('baz', $params);
$this->assertSame('valueBaz', $params['baz']);
$this->assertArrayNotHasKey('foo', $params);
}
public function testGetParamsFallbackForMixedAndUnknown(): void
{
$this->setupMultiMethodRoute();
// Mixed and unknown should fallback to raw params
$this->request->setQueryString([
'foo' => 'valueFoo',
'baz' => 'valueBaz',
'extra' => 'unexpected',
]);
$params = $this->request->getParams();
$this->assertArrayHasKey('foo', $params);
$this->assertSame('valueFoo', $params['foo']);
$this->assertArrayHasKey('baz', $params);
$this->assertSame('valueBaz', $params['baz']);
$this->assertArrayHasKey('extra', $params);
$this->assertSame('unexpected', $params['extra']);
}
public function testRouteIsScopedToRequestInstance(): void
{
$firstRequest = new Request(new SwooleRequest());
$secondRequest = new Request(new SwooleRequest());
$firstRoute = new Route(Request::METHOD_GET, '/first');
$secondRoute = new Route(Request::METHOD_GET, '/second');
$firstRequest->setRoute($firstRoute);
$secondRequest->setRoute($secondRoute);
$this->assertSame($firstRoute, $firstRequest->getRoute());
$this->assertSame($secondRoute, $secondRequest->getRoute());
}
/**
* Helper to attach a route with multiple SDK methods to the request.
*/
private function setupMultiMethodRoute(): void
{
$route = new Route(Request::METHOD_GET, '/multi');
$methodA = new Method(
namespace: 'namespace',
group: 'group',
name: 'methodA',
description: 'desc',
auth: [],
responses: [],
parameters: [
new Parameter('foo'),
new Parameter('bar', optional: true),
],
);
$methodB = new Method(
namespace: 'namespace',
group: 'group',
name: 'methodB',
description: 'desc',
auth: [],
responses: [],
parameters: [
new Parameter('baz'),
],
);
$route->label('sdk', [$methodA, $methodB]);
$this->request->addFilter(new First());
$this->request->addFilter(new Second());
$this->request->setRoute($route);
}
}