Skip to content

Commit 4ac3205

Browse files
committed
Improve the exception when the APCu extension is not enabled or installed
And fix the tests that where not marked as depending on the extension, tearDown was failing Signed-off-by: William Desportes <williamdes@wdes.fr>
1 parent 8f162cb commit 4ac3205

File tree

5 files changed

+30
-17
lines changed

5 files changed

+30
-17
lines changed

phpstan-baseline.neon

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ parameters:
3131
path: src/Loader.php
3232

3333
-
34-
message: '#^Offset ''lang'' on array\{0\: string, lang\: non\-falsy\-string, 1\: non\-falsy\-string, country\?\: string, 2\?\: string, charset\?\: string, 3\?\: string, modifier\?\: non\-empty\-string, \.\.\.\} on left side of \?\? always exists and is not nullable\.$#'
34+
message: '#^Offset ''lang'' on array\{0\: non\-falsy\-string, lang\: non\-falsy\-string, 1\: non\-falsy\-string, country\?\: string, 2\?\: string, charset\?\: string, 3\?\: string, modifier\?\: non\-empty\-string, \.\.\.\} on left side of \?\? always exists and is not nullable\.$#'
3535
identifier: nullCoalesce.offset
3636
count: 1
3737
path: src/Loader.php
@@ -84,12 +84,6 @@ parameters:
8484
count: 1
8585
path: tests/Cache/ApcuCacheTest.php
8686

87-
-
88-
message: '#^Dynamic call to static method PHPUnit\\Framework\\Assert\:\:markTestSkipped\(\)\.$#'
89-
identifier: staticMethod.dynamicCall
90-
count: 1
91-
path: tests/Cache/ApcuDisabledTest.php
92-
9387
-
9488
message: '#^Dynamic call to static method PHPUnit\\Framework\\Assert\:\:isInstanceOf\(\)\.$#'
9589
identifier: staticMethod.dynamicCall

src/Cache/ApcuCache.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,12 @@ public function __construct(
3232
private bool $reloadOnMiss = true,
3333
private string $prefix = 'mo_',
3434
) {
35-
if (! (function_exists('apcu_enabled') && apcu_enabled())) {
36-
throw new CacheException('APCu extension must be installed and enabled');
35+
if (! function_exists('apcu_enabled')) {
36+
throw new CacheException('The APCu extension must be installed.');
37+
}
38+
39+
if (! apcu_enabled()) {
40+
throw new CacheException('The APCu extension must be enabled (apc.enabled=1) or (apc.enable_cli=1)');
3741
}
3842

3943
$this->ensureTranslationsLoaded();

tests/Cache/ApcuCacheFactoryTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,28 @@
88
use PhpMyAdmin\MoTranslator\Cache\ApcuCacheFactory;
99
use PhpMyAdmin\MoTranslator\MoParser;
1010
use PHPUnit\Framework\Attributes\CoversClass;
11+
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
1112
use PHPUnit\Framework\TestCase;
1213

1314
use function apcu_clear_cache;
1415
use function apcu_delete;
1516
use function apcu_enabled;
1617
use function apcu_fetch;
17-
use function function_exists;
1818
use function sleep;
1919

2020
#[CoversClass(ApcuCacheFactory::class)]
21+
#[RequiresPhpExtension('apcu')]
2122
class ApcuCacheFactoryTest extends TestCase
2223
{
2324
protected function setUp(): void
2425
{
2526
parent::setUp();
2627

27-
if (function_exists('apcu_enabled') && apcu_enabled()) {
28+
if (apcu_enabled()) {
2829
return;
2930
}
3031

31-
$this->markTestSkipped('APCu extension is not installed and enabled for CLI');
32+
$this->markTestSkipped('The APCu extension is not enabled for the CLI (apc.enable_cli=1)');
3233
}
3334

3435
protected function tearDown(): void

tests/Cache/ApcuCacheTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PhpMyAdmin\MoTranslator\Cache\ApcuCache;
88
use PhpMyAdmin\MoTranslator\MoParser;
99
use PHPUnit\Framework\Attributes\CoversClass;
10+
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
1011
use PHPUnit\Framework\TestCase;
1112
use ReflectionMethod;
1213

@@ -17,22 +18,22 @@
1718
use function apcu_fetch;
1819
use function chr;
1920
use function explode;
20-
use function function_exists;
2121
use function implode;
2222
use function sleep;
2323

2424
#[CoversClass(ApcuCache::class)]
25+
#[RequiresPhpExtension('apcu')]
2526
class ApcuCacheTest extends TestCase
2627
{
2728
protected function setUp(): void
2829
{
2930
parent::setUp();
3031

31-
if (function_exists('apcu_enabled') && apcu_enabled()) {
32+
if (apcu_enabled()) {
3233
return;
3334
}
3435

35-
$this->markTestSkipped('APCu extension is not installed and enabled for CLI');
36+
$this->markTestSkipped('The APCu extension is not enabled for the CLI (apc.enable_cli=1)');
3637
}
3738

3839
protected function tearDown(): void

tests/Cache/ApcuDisabledTest.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,34 @@
77
use PhpMyAdmin\MoTranslator\Cache\ApcuCache;
88
use PhpMyAdmin\MoTranslator\CacheException;
99
use PhpMyAdmin\MoTranslator\MoParser;
10+
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
1011
use PHPUnit\Framework\TestCase;
1112

1213
use function apcu_enabled;
1314
use function function_exists;
1415

1516
final class ApcuDisabledTest extends TestCase
1617
{
18+
public function testConstructorApcuNotInstalledThrowsException(): void
19+
{
20+
if (function_exists('apcu_enabled')) {
21+
self::markTestSkipped('ext-apcu is installed.');
22+
}
23+
24+
$this->expectException(CacheException::class);
25+
$this->expectExceptionMessage('The APCu extension must be installed.');
26+
new ApcuCache(new MoParser(null), 'foo', 'bar');
27+
}
28+
29+
#[RequiresPhpExtension('apcu')]
1730
public function testConstructorApcuNotEnabledThrowsException(): void
1831
{
1932
if (function_exists('apcu_enabled') && apcu_enabled()) {
20-
$this->markTestSkipped('ext-apcu is enabled');
33+
self::markTestSkipped('ext-apcu is enabled');
2134
}
2235

2336
$this->expectException(CacheException::class);
24-
$this->expectExceptionMessage('APCu extension must be installed and enabled');
37+
$this->expectExceptionMessage('The APCu extension must be enabled (apc.enabled=1) or (apc.enable_cli=1)');
2538
new ApcuCache(new MoParser(null), 'foo', 'bar');
2639
}
2740
}

0 commit comments

Comments
 (0)