Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
strategy:
matrix:
operating-system: [ ubuntu-latest ]
php-versions: [ '8.0', '8.1', '8.2' ]
php-versions: [ '8.1', '8.2', '8.3' ]
redis-version: [ 6 ]

runs-on: ${{ matrix.operating-system }}
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
}
],
"require": {
"php": "^8.0",
"php": "^8.1",
"ext-pdo": "*",
"ext-curl": "*",
"ext-json": "*",
Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
parameters:
phpVersion: 80000
phpVersion: 80100
level: 8

scanFiles:
Expand Down
8 changes: 7 additions & 1 deletion rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use Rector\TypeDeclaration\Rector\StmtsAwareInterface\DeclareStrictTypesRector;
use Rector\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector;
use Rector\CodingStyle\Rector\FuncCall\FunctionFirstClassCallableRector;
use Rector\Php81\Rector\Array_\ArrayToFirstClassCallableRector;
use Rector\Php81\Rector\Property\ReadOnlyPropertyRector;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Set\ValueObject\SetList;
use Rector\Config\RectorConfig;
Expand All @@ -13,12 +16,15 @@
__DIR__ . '/src',
])
->withSets([
LevelSetList::UP_TO_PHP_80,
LevelSetList::UP_TO_PHP_81,
SetList::TYPE_DECLARATION,
])
->withRules([
DeclareStrictTypesRector::class,
])
->withSkip([
ClassPropertyAssignToConstructorPromotionRector::class,
ReadOnlyPropertyRector::class,
FunctionFirstClassCallableRector::class,
ArrayToFirstClassCallableRector::class,
]);
6 changes: 3 additions & 3 deletions src/HttpClient/ResponseHeaders.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function offsetUnset($offset): void
* @param string $offset
* @return mixed|null
*/
public function offsetGet($offset)
public function offsetGet($offset): mixed
{
return $this->data[strtolower($offset)] ?? null;
}
Expand All @@ -95,7 +95,7 @@ public function count(): int
/**
* @return mixed
*/
public function current()
public function current(): mixed
{
return current($this->data);
}
Expand All @@ -108,7 +108,7 @@ public function next(): void
/**
* @return string|null
*/
public function key()
public function key(): mixed
{
$key = key($this->data);

Expand Down
13 changes: 7 additions & 6 deletions src/Session/Adapters/Database/DatabaseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,19 @@ public function destroy($id): bool
}

/**
* @inheritDoc
* Deletes the expired sessions
* @return int|false The number of deleted sessions, or false when they cannot be deleted
*/
public function gc($max_lifetime)
public function gc($max_lifetime): int|false
{
$old = time() - $max_lifetime;

$result = $this->sessionModel->criteria('ttl', '<', $old)->deleteMany();
$expired = $this->sessionModel->criteria('ttl', '<', $old)->count();

if ($result === false) {
return false;
if ($expired === 0) {
return 0;
}

return 0;
return $this->sessionModel->criteria('ttl', '<', $old)->deleteMany() ? $expired : false;
}
}
28 changes: 28 additions & 0 deletions tests/Unit/HttpClient/ResponseHeadersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Quantum\HttpClient\ResponseHeaders;
use Quantum\Tests\Unit\AppTestCase;
use ReflectionMethod;

class ResponseHeadersTest extends AppTestCase
{
Expand Down Expand Up @@ -33,4 +34,31 @@ public function testResponseHeadersCanUnsetCaseInsensitively(): void
$this->assertCount(0, $headers);
$this->assertNull($headers['Content-Type']);
}

/**
* @dataProvider interfaceMethodProvider
*/
public function testResponseHeadersDeclaresTheReturnTypeOfItsInterfaceMethods(string $method): void
{
$this->assertTrue((new ReflectionMethod(ResponseHeaders::class, $method))->hasReturnType());
}

/**
* @return array<string, array{0: string}>
*/
public function interfaceMethodProvider(): array
{
return [
'offsetSet' => ['offsetSet'],
'offsetExists' => ['offsetExists'],
'offsetUnset' => ['offsetUnset'],
'offsetGet' => ['offsetGet'],
'count' => ['count'],
'current' => ['current'],
'next' => ['next'],
'key' => ['key'],
'valid' => ['valid'],
'rewind' => ['rewind'],
];
}
}
99 changes: 99 additions & 0 deletions tests/Unit/Session/Adapters/Database/DatabaseHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

namespace Quantum\Tests\Unit\Session\Adapters\Database;

use Quantum\Session\Adapters\Database\DatabaseHandler;
use Quantum\Database\Adapters\Idiorm\IdiormDbal;
use Quantum\Tests\Unit\Session\TestCaseHelper;
use Quantum\Database\Contracts\DbalInterface;
use Quantum\Model\Factories\ModelFactory;
use Quantum\Tests\Unit\AppTestCase;
use ReflectionMethod;
use Mockery;

class DatabaseHandlerTest extends AppTestCase
{
use TestCaseHelper;

public function setUp(): void
{
parent::setUp();

IdiormDbal::connect(['driver' => 'sqlite', 'database' => ':memory:']);

$this->_createSessionsTable();
}

public function tearDown(): void
{
ModelFactory::createDynamicModel('sessions')->deleteMany();

parent::tearDown();
}

public function testDatabaseHandlerGcDeletesTheExpiredSessionsAndReturnsTheirNumber(): void
{
$this->insertSession('expired-1', time() - 7200);
$this->insertSession('expired-2', time() - 5400);
$this->insertSession('active', time());

$this->assertSame(2, $this->createHandler()->gc(3600));

$this->assertSame([['session_id' => 'active']], IdiormDbal::query('SELECT session_id FROM sessions'));
}

public function testDatabaseHandlerGcReturnsZeroWhenNoSessionHasExpired(): void
{
$this->insertSession('active', time());

$this->assertSame(0, $this->createHandler()->gc(3600));

$this->assertSame([['session_id' => 'active']], IdiormDbal::query('SELECT session_id FROM sessions'));
}

public function testDatabaseHandlerGcReturnsFalseWhenTheSessionsCannotBeDeleted(): void
{
$model = Mockery::mock(DbalInterface::class);
$model->shouldReceive('criteria')->with('ttl', '<', Mockery::type('int'))->andReturnSelf();
$model->shouldReceive('count')->andReturn(2);
$model->shouldReceive('deleteMany')->andReturn(false);

$this->assertFalse((new DatabaseHandler($model))->gc(3600));
}

/**
* @dataProvider interfaceMethodProvider
*/
public function testDatabaseHandlerDeclaresTheReturnTypeOfItsInterfaceMethods(string $method): void
{
$this->assertTrue((new ReflectionMethod(DatabaseHandler::class, $method))->hasReturnType());
}

/**
* A handler serves one session, so every check gets a handler of its own
*/
private function createHandler(): DatabaseHandler
{
return new DatabaseHandler(ModelFactory::createDynamicModel('sessions')->getOrmInstance());
}

private function insertSession(string $id, int $ttl): void
{
IdiormDbal::execute('INSERT INTO sessions (session_id, data, ttl) VALUES (?, ?, ?)', [$id, 'data', $ttl]);
}

/**
* @return array<string, array{0: string}>
*/
public function interfaceMethodProvider(): array
{
return [
'open' => ['open'],
'close' => ['close'],
'read' => ['read'],
'write' => ['write'],
'destroy' => ['destroy'],
'gc' => ['gc'],
];
}
}
Loading