Skip to content

Commit

Permalink
PhpStan warnings have been resolved
Browse files Browse the repository at this point in the history
  • Loading branch information
batumibiz committed Dec 3, 2024
1 parent 1c1ee4e commit 2578828
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 61 deletions.
3 changes: 3 additions & 0 deletions src/Db/PdoFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public function __invoke(ContainerInterface $container): PDO
return $this->connect($config);
}

/**
* @param array<string, mixed> $config
*/
private function connect(array $config): PDO
{
try {
Expand Down
3 changes: 3 additions & 0 deletions src/Http/IpAndUserAgentMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ public function determineIpViaProxyAddress(ServerRequestInterface $request): ?st
return null;
}

/**
* @param array<array<string>> $vars
*/
private function extractIp(ServerRequestInterface $request, array $vars): ?string
{
/** @var string $ip */
Expand Down
2 changes: 1 addition & 1 deletion src/View/EngineFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function __invoke(ContainerInterface $container): Engine

/**
* @var string $namespace
* @var array $pathArray
* @var array<string> $pathArray
*/
foreach ($paths as $namespace => $pathArray) {
/** @var string $path */
Expand Down
7 changes: 6 additions & 1 deletion tests/unit/App/ApplicationFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,14 @@ public function testCreate(bool $debug): void
]
)
);
$this->assertInstanceOf(Application::class, $app);

/** @phpstan-ignore staticMethod.alreadyNarrowedType */
self::assertInstanceOf(Application::class, $app);
}

/**
* @return array<string, array<bool>>
*/
public static function debugDataProvider(): array
{
return [
Expand Down
19 changes: 5 additions & 14 deletions tests/unit/Db/PdoFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Mobicms\Config\ConfigInterface;
use Mobicms\Testutils\ConfigLoader;
use Mobicms\Testutils\MysqlTestCase;
use PDO;
use PHPUnit\Framework\MockObject\Exception;
use Psr\Container\ContainerInterface;

class PdoFactoryTest extends MysqlTestCase
Expand All @@ -23,19 +23,6 @@ public function setUp(): void
$this->config = new ConfigLoader();
}

public function testFactoryReturnsInstanceOfPdo(): void
{
$config = [
'host' => $this->config->host(),
'port' => $this->config->port(),
'dbname' => $this->config->dbName(),
'user' => $this->config->user(),
'pass' => $this->config->password(),
];

$this->assertInstanceOf(PDO::class, (new PdoFactory())($this->getContainer($config)));
}

public function testInvalidPasswordThrowInvalidCredentialsException(): void
{
$config = [
Expand Down Expand Up @@ -106,6 +93,10 @@ public function testInvalidPortThrowUnableToConnectException(): void
(new PdoFactory())($this->getContainer($config));
}

/**
* @param array<mixed> $values
* @throws Exception
*/
private function getContainer(array $values): ContainerInterface
{
$config = $this->createMock(ConfigInterface::class);
Expand Down
11 changes: 7 additions & 4 deletions tests/unit/ErrorHandler/ErrorHandlerMiddlewareFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace MobicmsTest\ErrorHandler;

use HttpSoft\ErrorHandler\ErrorHandlerMiddleware;
use Mobicms\Container\Container;
use Mobicms\Container\Exception\NotFoundException;
use Mobicms\ErrorHandler\ErrorHandlerMiddlewareFactory;
Expand All @@ -27,7 +26,8 @@ public function testCreate(bool $debug): void
->willReturnCallback(
fn(string $val) => match ($val) {
'log_file' => 'test.log',
'debug' => $debug
'debug' => $debug,
default => null,
}
);

Expand All @@ -41,8 +41,8 @@ public function testCreate(bool $debug): void
$container->setFactory(LoggerInterface::class, LoggerFactory::class);

$errorHandler = (new ErrorHandlerMiddlewareFactory())($container);
$this->assertInstanceOf(MiddlewareInterface::class, $errorHandler);
$this->assertInstanceOf(ErrorHandlerMiddleware::class, $errorHandler);
/** @phpstan-ignore staticMethod.alreadyNarrowedType */
self::assertInstanceOf(MiddlewareInterface::class, $errorHandler);
}

public function testCreateThrowNotFoundExceptionIfConfigIsNotSet(): void
Expand All @@ -57,6 +57,9 @@ public function testCreateThrowNotFoundExceptionIfLoggerInterfaceIsNotSet(): voi
(new ErrorHandlerMiddlewareFactory())(new Container(['debug' => true, 'log_file' => 'test.log']));
}

/**
* @return array<string, array<bool>>
*/
public static function debugDataProvider(): array
{
return [
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/ErrorHandler/NotFoundHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ public function testHandleWithoutDebugMode(): void
{
$handler = new NotFoundHandler($this->responseFactory, $this->renderer, 'template-not-found', false);
$response = $handler->handle($this->request);
$this->assertSame('404 Not Found', trim((string) $response->getBody()));
self::assertSame('404 Not Found', trim((string) $response->getBody()));
}

public function testHandleWithDebugMode(): void
{
$handler = new NotFoundHandler($this->responseFactory, $this->renderer, 'template-not-found', true);
$response = $handler->handle($this->request);
$this->assertSame('DEBUG: 404', trim((string) $response->getBody()));
self::assertSame('DEBUG: 404', trim((string) $response->getBody()));
}
}
14 changes: 7 additions & 7 deletions tests/unit/ErrorHandler/WhoopsErrorResponseGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ public function testGenerateWithDefaultPrettyPageHandler(): void
$exception = new InternalServerErrorHttpException();
$response = $generator->generate($exception, new ServerRequest());

$this->assertSame($exception->getStatusCode(), $response->getStatusCode());
$this->assertSame($exception->getReasonPhrase(), $response->getReasonPhrase());
$this->assertSame('text/html', $response->getHeaderLine('content-type'));
self::assertSame($exception->getStatusCode(), $response->getStatusCode());
self::assertSame($exception->getReasonPhrase(), $response->getReasonPhrase());
self::assertSame('text/html', $response->getHeaderLine('content-type'));
}

public function testGenerateWithPassedJsonResponseHandler(): void
Expand All @@ -42,9 +42,9 @@ public function testGenerateWithPassedJsonResponseHandler(): void
$exception = new ForbiddenHttpException();
$response = $generator->generate($exception, new ServerRequest());

$this->assertSame($exception->getStatusCode(), $response->getStatusCode());
$this->assertSame($exception->getReasonPhrase(), $response->getReasonPhrase());
$this->assertSame('application/json', $response->getHeaderLine('content-type'));
self::assertSame($exception->getStatusCode(), $response->getStatusCode());
self::assertSame($exception->getReasonPhrase(), $response->getReasonPhrase());
self::assertSame('application/json', $response->getHeaderLine('content-type'));

$exceptionData = json_encode(
[
Expand All @@ -58,6 +58,6 @@ public function testGenerateWithPassedJsonResponseHandler(): void
]
);

$this->assertSame($exceptionData, (string) $response->getBody());
self::assertSame($exceptionData, (string) $response->getBody());
}
}
56 changes: 32 additions & 24 deletions tests/unit/Http/IpAndUserAgentMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace MobicmsTest\Http;

use Mobicms\Http\IpAndUserAgentMiddleware;
use PHPUnit\Framework\MockObject\Exception;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
Expand All @@ -13,8 +14,14 @@

class IpAndUserAgentMiddlewareTest extends TestCase
{
/**
* @var MockObject|ServerRequestInterface
*/
private ServerRequestInterface|MockObject $request;

/**
* @throws Exception
*/
public function setUp(): void
{
$this->request = $this->createMock(ServerRequestInterface::class);
Expand All @@ -23,11 +30,11 @@ public function setUp(): void
public function testDetermineIpAddress(): void
{
$this->request
->expects($this->once())
->expects(self::once())
->method('getServerParams')
->willReturn(['REMOTE_ADDR' => '31.23.209.1']);
$middleware = new IpAndUserAgentMiddleware();
$this->assertSame('31.23.209.1', $middleware->determineIpAddress($this->request));
self::assertSame('31.23.209.1', $middleware->determineIpAddress($this->request));
}

public function testDetermineIpAddressWithInvalidIpReturnsNull(): void
Expand All @@ -36,74 +43,74 @@ public function testDetermineIpAddressWithInvalidIpReturnsNull(): void
->method('getServerParams')
->willReturn(['REMOTE_ADDR' => '392.268.0.9']);
$middleware = new IpAndUserAgentMiddleware();
$this->assertNull($middleware->determineIpAddress($this->request));
self::assertNull($middleware->determineIpAddress($this->request));
}

public function testDetermineIpAddressWithoutIp(): void
{
$this->request
->expects($this->once())
->expects(self::once())
->method('getServerParams')
->willReturn([]);
$middleware = new IpAndUserAgentMiddleware();
$this->assertNull($middleware->determineIpAddress($this->request));
self::assertNull($middleware->determineIpAddress($this->request));
}

public function testDetermineIpViaProxyAddress(): void
{
$this->request
->expects($this->once())
->expects(self::once())
->method('hasHeader')
->with('Forwarded')
->willReturn(true);
$this->request
->expects($this->once())
->expects(self::once())
->method('getHeaderLine')
->with('Forwarded')
->willReturn('212.58.119.76, 91.221.6.36');
$this->request
->method('getServerParams')
->willReturn(['REMOTE_ADDR' => '31.23.209.1']);
$middleware = new IpAndUserAgentMiddleware();
$this->assertSame('212.58.119.76', $middleware->determineIpViaProxyAddress($this->request));
self::assertSame('212.58.119.76', $middleware->determineIpViaProxyAddress($this->request));
}

public function testDetermineIpViaProxyAddressSkipPrivateNetworks(): void
{
$this->request
->expects($this->once())
->expects(self::once())
->method('hasHeader')
->with('Forwarded')
->willReturn(true);
$this->request
->expects($this->once())
->expects(self::once())
->method('getHeaderLine')
->with('Forwarded')
->willReturn('10.0.0.1, 172.16.0.1, 192.168.0.1, 212.58.119.76');
$this->request
->method('getServerParams')
->willReturn(['REMOTE_ADDR' => '31.23.209.1']);
$middleware = new IpAndUserAgentMiddleware();
$this->assertSame('212.58.119.76', $middleware->determineIpViaProxyAddress($this->request));
self::assertSame('212.58.119.76', $middleware->determineIpViaProxyAddress($this->request));
}

public function testDetermineIpViaProxyAddressSkipSameIpAsRemote(): void
{
$this->request
->expects($this->once())
->expects(self::once())
->method('hasHeader')
->with('Forwarded')
->willReturn(true);
$this->request
->expects($this->once())
->expects(self::once())
->method('getHeaderLine')
->with('Forwarded')
->willReturn('31.23.209.1, 212.58.119.76');
$this->request
->method('getServerParams')
->willReturn(['REMOTE_ADDR' => '31.23.209.1']);
$middleware = new IpAndUserAgentMiddleware();
$this->assertSame('212.58.119.76', $middleware->determineIpViaProxyAddress($this->request));
self::assertSame('212.58.119.76', $middleware->determineIpViaProxyAddress($this->request));
}

public function testDetermineIpViaProxyAddressWithoutValidIp(): void
Expand All @@ -115,7 +122,7 @@ public function testDetermineIpViaProxyAddressWithoutValidIp(): void
->method('getHeaderLine')
->willReturn('331.23.209.1, test');
$middleware = new IpAndUserAgentMiddleware();
$this->assertNull($middleware->determineIpViaProxyAddress($this->request));
self::assertNull($middleware->determineIpViaProxyAddress($this->request));
}

public function testDetermineIpViaProxyAddressWithoutRequiredHeaders(): void
Expand All @@ -124,23 +131,23 @@ public function testDetermineIpViaProxyAddressWithoutRequiredHeaders(): void
->method('hasHeader')
->willReturn(false);
$middleware = new IpAndUserAgentMiddleware();
$this->assertNull($middleware->determineIpViaProxyAddress($this->request));
self::assertNull($middleware->determineIpViaProxyAddress($this->request));
}

public function testDetermineUserAgent(): void
{
$this->request
->expects($this->once())
->expects(self::once())
->method('hasHeader')
->with('User-Agent')
->willReturn(true);
$this->request
->expects($this->once())
->expects(self::once())
->method('getHeaderLine')
->with('User-Agent')
->willReturn('Test User Agent');
$middleware = new IpAndUserAgentMiddleware();
$this->assertSame('Test User Agent', $middleware->determineUserAgent($this->request));
self::assertSame('Test User Agent', $middleware->determineUserAgent($this->request));
}

public function testDetermineUserAgentTrimLongStringTo255Symbols(): void
Expand All @@ -154,7 +161,7 @@ public function testDetermineUserAgentTrimLongStringTo255Symbols(): void
->with('User-Agent')
->willReturn(str_repeat('a', 300));
$middleware = new IpAndUserAgentMiddleware();
$this->assertSame(str_repeat('a', 255), $middleware->determineUserAgent($this->request));
self::assertSame(str_repeat('a', 255), $middleware->determineUserAgent($this->request));
}

public function testDetermineUserAgentWithoutRequiredHeaders(): void
Expand All @@ -164,11 +171,11 @@ public function testDetermineUserAgentWithoutRequiredHeaders(): void
->with('User-Agent')
->willReturn(false);
$this->request
->expects($this->never())
->expects(self::never())
->method('getHeaderLine');

$middleware = new IpAndUserAgentMiddleware();
$this->assertNull($middleware->determineUserAgent($this->request));
self::assertNull($middleware->determineUserAgent($this->request));
}

public function testProcess(): void
Expand Down Expand Up @@ -198,9 +205,10 @@ public function testProcess(): void
);

$this->request
->expects($this->exactly(3))
->expects(self::exactly(3))
->method('withAttribute')
->willReturnCallback(
/** @phpstan-ignore match.unhandled */
fn($key, $value) => match ([$key, $value]) {
[IpAndUserAgentMiddleware::IP_ADDR, '192.168.0.9'],
[IpAndUserAgentMiddleware::USER_AGENT, 'Test User Agent'],
Expand All @@ -211,7 +219,7 @@ public function testProcess(): void

$handler = $this->createMock(RequestHandlerInterface::class);
$handler
->expects($this->once())
->expects(self::once())
->method('handle')
->with($this->request)
->willReturn($this->createMock(ResponseInterface::class));
Expand Down
Loading

0 comments on commit 2578828

Please sign in to comment.