Skip to content

Commit e9e3cee

Browse files
Merge pull request #20501 from kamil-tekiela/Fix-test-inefficiencies
Fix test inefficiencies
2 parents 1a7bf60 + 7bcd75c commit e9e3cee

4 files changed

Lines changed: 47 additions & 11 deletions

File tree

‎src/Controllers/Import/StatusController.php‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
use function session_start;
2323
use function session_write_close;
2424
use function sprintf;
25-
use function time;
2625
use function usleep;
2726

2827
/**
@@ -61,7 +60,7 @@ public function __invoke(ServerRequest $request): Response
6160
usleep(self::$sleepMicroseconds);
6261

6362
$maximumTime = ini_get('max_execution_time');
64-
$timestamp = time();
63+
$timestamp = $this->clock->now()->getTimestamp();
6564
// wait until message is available
6665
while (($_SESSION['Import_message']['message'] ?? null) == null) {
6766
// close session before sleeping
@@ -71,7 +70,7 @@ public function __invoke(ServerRequest $request): Response
7170
// reopen session
7271
session_start();
7372

74-
if (time() - $timestamp > $maximumTime) {
73+
if ($this->clock->now()->getTimestamp() - $timestamp > $maximumTime) {
7574
$_SESSION['Import_message']['message'] = Message::error(
7675
__('Could not load the progress of the import.'),
7776
)->getDisplay();

‎tests/unit/Container/ContainerBuilderTest.php‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use PhpMyAdmin\Tests\AbstractTestCase;
1414
use PHPUnit\Framework\Attributes\CoversClass;
1515
use PHPUnit\Framework\Attributes\DataProvider;
16+
use Psr\Container\ContainerInterface;
1617
use Symfony\Component\DependencyInjection\ContainerBuilder as SymfonyContainerBuilder;
1718
use Symfony\Component\DependencyInjection\Loader\Configurator\ServicesConfigurator;
1819
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
@@ -25,6 +26,15 @@
2526
#[CoversClass(ContainerBuilder::class)]
2627
final class ContainerBuilderTest extends AbstractTestCase
2728
{
29+
private static ContainerInterface $container;
30+
31+
public static function setUpBeforeClass(): void
32+
{
33+
parent::setUpBeforeClass();
34+
35+
self::$container = ContainerBuilder::getContainer();
36+
}
37+
2838
public function testGetContainer(): void
2939
{
3040
ContainerBuilder::$container = null;
@@ -41,8 +51,7 @@ public function testContainerEntries(string $service): void
4151
{
4252
Current::$lang = 'en';
4353
DatabaseInterface::$instance = $this->createDatabaseInterface();
44-
$container = ContainerBuilder::getContainer();
45-
self::assertInstanceOf($service, $container->get($service));
54+
self::assertInstanceOf($service, self::$container->get($service));
4655
ContainerBuilder::$container = null;
4756
}
4857

‎tests/unit/Controllers/Import/StatusControllerTest.php‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace PhpMyAdmin\Tests\Controllers\Import;
66

7+
use DateTimeImmutable;
78
use Fig\Http\Message\StatusCodeInterface;
89
use PhpMyAdmin\Clock\Clock;
910
use PhpMyAdmin\Config;
@@ -15,6 +16,7 @@
1516
use PhpMyAdmin\Template;
1617
use PhpMyAdmin\Tests\AbstractTestCase;
1718
use PHPUnit\Framework\Attributes\CoversClass;
19+
use Psr\Clock\ClockInterface;
1820
use Random\Engine\Mt19937;
1921
use Random\Randomizer;
2022
use ReflectionProperty;
@@ -108,9 +110,19 @@ public function testMessageProgressTimeout(): void
108110
'message' => '1',
109111
]);
110112

113+
// Advance one second per call so the timeout triggers on the first retry without waiting for the wall clock.
114+
$clock = new class implements ClockInterface {
115+
private int $seconds = 0;
116+
117+
public function now(): DateTimeImmutable
118+
{
119+
return new DateTimeImmutable('@' . $this->seconds++);
120+
}
121+
};
122+
111123
$controller = new StatusController(
112124
new Template(new Config()),
113-
new Clock(),
125+
$clock,
114126
new Ajax(new Randomizer(new Mt19937(42))),
115127
);
116128
$response = $controller($request);

‎tests/unit/Http/Factory/ResponseFactoryTest.php‎

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use Nyholm\Psr7\Factory\Psr17Factory;
1212
use PhpMyAdmin\Http\Factory\ResponseFactory;
1313
use PhpMyAdmin\Http\Response;
14-
use PHPUnit\Framework\Attributes\BackupStaticProperties;
1514
use PHPUnit\Framework\Attributes\CoversClass;
1615
use PHPUnit\Framework\Attributes\DataProvider;
1716
use PHPUnit\Framework\TestCase;
@@ -26,6 +25,25 @@
2625
#[CoversClass(ResponseFactory::class)]
2726
final class ResponseFactoryTest extends TestCase
2827
{
28+
private ReflectionProperty $providersProperty;
29+
30+
private mixed $originalProviders;
31+
32+
protected function setUp(): void
33+
{
34+
parent::setUp();
35+
36+
$this->providersProperty = new ReflectionProperty(ResponseFactory::class, 'providers');
37+
$this->originalProviders = $this->providersProperty->getValue();
38+
}
39+
40+
protected function tearDown(): void
41+
{
42+
$this->providersProperty->setValue(null, $this->originalProviders);
43+
44+
parent::tearDown();
45+
}
46+
2947
/**
3048
* @psalm-param class-string<ResponseFactoryInterface> $provider
3149
* @psalm-param class-string<ResponseInterface> $expectedResponse
@@ -54,11 +72,10 @@ public static function providerForTestCreateResponse(): iterable
5472

5573
/** @psalm-param class-string<ResponseFactoryInterface> $provider */
5674
#[DataProvider('providerForTestCreate')]
57-
#[BackupStaticProperties(true)]
5875
public function testCreate(string $provider): void
5976
{
6077
$this->skipIfNotAvailable($provider);
61-
(new ReflectionProperty(ResponseFactory::class, 'providers'))->setValue(null, [$provider]);
78+
$this->providersProperty->setValue(null, [$provider]);
6279
$responseFactory = ResponseFactory::create();
6380
$actual = (new ReflectionProperty(ResponseFactory::class, 'responseFactory'))->getValue($responseFactory);
6481
self::assertInstanceOf($provider, $actual);
@@ -74,10 +91,9 @@ public static function providerForTestCreate(): iterable
7491
yield 'httpsoft/http-message' => [HttpSoftResponseFactory::class];
7592
}
7693

77-
#[BackupStaticProperties(true)]
7894
public function testCreateWithoutProvider(): void
7995
{
80-
(new ReflectionProperty(ResponseFactory::class, 'providers'))->setValue(null, ['InvalidResponseFactoryClass']);
96+
$this->providersProperty->setValue(null, ['InvalidResponseFactoryClass']);
8197
$this->expectException(RuntimeException::class);
8298
$this->expectExceptionMessage('No HTTP response factories found.');
8399
ResponseFactory::create();

0 commit comments

Comments
 (0)