Skip to content

Commit

Permalink
[TASK] Avoid phpunit @requires annotation
Browse files Browse the repository at this point in the history
Substitute with a PHP attribute.

Resolves: #103219
Releases: main
Change-Id: Ie589404f797a880fc8e27480a2dd95c3f0420427
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/83152
Tested-by: core-ci <typo3@b13.com>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: Oliver Klee <typo3-coding@oliverklee.de>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Reviewed-by: Oliver Klee <typo3-coding@oliverklee.de>
  • Loading branch information
lolli42 committed Feb 27, 2024
1 parent 09789e6 commit d2964e3
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 79 deletions.
5 changes: 1 addition & 4 deletions Build/Scripts/annotationChecker.php
Expand Up @@ -29,9 +29,6 @@

class NodeVisitor extends NodeVisitorAbstract
{
/**
* @var array
*/
public array $matches = [];

public function enterNode(Node $node)
Expand All @@ -54,7 +51,7 @@ public function enterNode(Node $node)
// PHPDocumentor 2 tags
'author', 'category', 'copyright', 'deprecated', 'example', 'internal', 'license', 'link', 'param', 'property', 'return', 'see', 'since', 'throws', 'todo', 'TODO', 'var', 'version',
// PHPUnit & codeception tags
'depends', 'requires', 'env',
'depends', 'env',
// PHPCheckStyle
'SuppressWarnings', 'noinspection',
// Extbase related
Expand Down
Expand Up @@ -17,6 +17,7 @@

namespace TYPO3\CMS\Core\Tests\Functional\Cache\Backend;

use PHPUnit\Framework\Attributes\RequiresPhpExtension;
use PHPUnit\Framework\Attributes\Test;
use Psr\Log\LoggerInterface;
use TYPO3\CMS\Core\Cache\Backend\RedisBackend;
Expand All @@ -25,15 +26,7 @@
use TYPO3\CMS\Core\Utility\StringUtility;
use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;

/**
* Test case for the cache to redis backend
*
* Warning:
* These functional tests use and flush redis database numbers 0 and 1 on the
* redis host specified by environment variable typo3RedisHost
*
* @requires extension redis
*/
#[RequiresPhpExtension('redis')]
final class RedisBackendTest extends FunctionalTestCase
{
protected bool $initializeDatabase = false;
Expand All @@ -49,9 +42,6 @@ protected function setUp(): void
parent::setUp();
}

/**
* Sets up the redis cache backend used for testing
*/
protected function setUpSubject(array $backendOptions = []): RedisBackend
{
// We know this env is set, otherwise setUp() would skip the tests
Expand All @@ -72,17 +62,13 @@ protected function setUpSubject(array $backendOptions = []): RedisBackend
return $subject;
}

/**
* Sets up a test-internal redis connection to check implementation details
*/
protected function setUpRedis(): \Redis
private function setUpRedis(): \Redis
{
// We know this env is set, otherwise setUp() would skip the tests
$redisHost = getenv('typo3TestingRedisHost');
// If typo3TestingRedisPort env is set, use it, otherwise fall back to standard port
$env = getenv('typo3TestingRedisPort');
$redisPort = is_string($env) ? (int)$env : 6379;

$redis = new \Redis();
$redis->connect($redisHost, $redisPort);
return $redis;
Expand All @@ -93,7 +79,6 @@ public function setDatabaseThrowsExceptionIfGivenDatabaseNumberIsNotAnInteger():
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionCode(1279763057);

$this->setUpSubject(['database' => 'foo']);
}

Expand All @@ -102,7 +87,6 @@ public function setDatabaseThrowsExceptionIfGivenDatabaseNumberIsNegative(): voi
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionCode(1279763534);

$this->setUpSubject(['database' => -1]);
}

Expand All @@ -111,7 +95,6 @@ public function setCompressionThrowsExceptionIfCompressionParameterIsNotOfTypeBo
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionCode(1289679153);

$this->setUpSubject(['compression' => 'foo']);
}

Expand All @@ -120,7 +103,6 @@ public function setCompressionLevelThrowsExceptionIfCompressionLevelIsNotInteger
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionCode(1289679154);

$this->setUpSubject(['compressionLevel' => 'foo']);
}

Expand All @@ -129,7 +111,6 @@ public function setCompressionLevelThrowsExceptionIfCompressionLevelIsNotBetween
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionCode(1289679155);

$this->setUpSubject(['compressionLevel' => 11]);
}

Expand All @@ -138,7 +119,6 @@ public function setConnectionTimeoutThrowsExceptionIfConnectionTimeoutIsNotInteg
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionCode(1487849315);

$this->setUpSubject(['connectionTimeout' => 'foo']);
}

Expand All @@ -147,7 +127,6 @@ public function setConnectionTimeoutThrowsExceptionIfConnectionTimeoutIsNegative
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionCode(1487849326);

$this->setUpSubject(['connectionTimeout' => -1]);
}

Expand All @@ -156,7 +135,6 @@ public function setThrowsExceptionIfIdentifierIsNotAString(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionCode(1377006651);

$subject = $this->setUpSubject();
$subject->set([], 'data');
}
Expand All @@ -166,7 +144,6 @@ public function setThrowsExceptionIfDataIsNotAString(): void
{
$this->expectException(InvalidDataException::class);
$this->expectExceptionCode(1279469941);

$subject = $this->setUpSubject();
$subject->set(StringUtility::getUniqueId('identifier'), []);
}
Expand All @@ -176,7 +153,6 @@ public function setThrowsExceptionIfLifetimeIsNegative(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionCode(1279487573);

$subject = $this->setUpSubject();
$subject->set(StringUtility::getUniqueId('identifier'), 'data', [], -42);
}
Expand All @@ -186,7 +162,6 @@ public function setThrowsExceptionIfLifetimeIsNotNullOrAnInteger(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionCode(1279488008);

$subject = $this->setUpSubject();
$subject->set(StringUtility::getUniqueId('identifier'), 'data', [], []);
}
Expand Down
Expand Up @@ -17,68 +17,50 @@

namespace TYPO3\CMS\Core\Tests\Functional\Session\Backend;

use PHPUnit\Framework\Attributes\RequiresPhpExtension;
use PHPUnit\Framework\Attributes\Test;
use TYPO3\CMS\Core\Session\Backend\Exception\SessionNotCreatedException;
use TYPO3\CMS\Core\Session\Backend\Exception\SessionNotFoundException;
use TYPO3\CMS\Core\Session\Backend\Exception\SessionNotUpdatedException;
use TYPO3\CMS\Core\Session\Backend\RedisSessionBackend;
use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;

/**
* @requires extension redis
*/
#[RequiresPhpExtension('redis')]
final class RedisSessionBackendTest extends FunctionalTestCase
{
protected bool $initializeDatabase = false;

/**
* @var RedisSessionBackend Prepared and connected redis test subject
*/
protected $subject;

/**
* @var \Redis
*/
protected $redis;
private RedisSessionBackend $subject;
private \Redis $redis;

/**
* @var array
*/
protected $testSessionRecord = [
protected array $testSessionRecord = [
// RedisSessionBackend::hash('randomSessionId') with encryption key 12345
'ses_id' => '21c0e911565a67315cdc384889c470fd291feafbfa62e31ecf7409430640bc7a',
'ses_userid' => 1,
// serialize(['foo' => 'bar', 'boo' => 'far'])
'ses_data' => 'a:2:{s:3:"foo";s:3:"bar";s:3:"boo";s:3:"far";}',
];

/**
* Set configuration for RedisSessionBackend
*/
protected function setUp(): void
{
parent::setUp();
$GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] = '12345';

if (!getenv('typo3TestingRedisHost')) {
self::markTestSkipped('environment variable "typo3TestingRedisHost" must be set to run this test');
}
// Note we assume that if that typo3TestingRedisHost env is set, we can use that for testing,
// there is no test to see if the daemon is actually up and running. Tests will fail if env
// is set but daemon is down.

// We know this env is set, otherwise setUp() would skip the tests
$redisHost = getenv('typo3TestingRedisHost');
// If typo3TestingRedisPort env is set, use it, otherwise fall back to standard port
$env = getenv('typo3TestingRedisPort');
$redisPort = is_string($env) ? (int)$env : 6379;

$this->redis = new \Redis();
$this->redis->connect($redisHost, $redisPort);
$this->redis->select(0);
// Clear db to ensure no sessions exist currently
$this->redis->flushDB();

$this->subject = new RedisSessionBackend();
$this->subject->initialize(
'default',
Expand Down Expand Up @@ -108,9 +90,7 @@ public function canValidateSessionBackend(): void
public function sessionDataIsStoredProperly(): void
{
$record = $this->subject->set('randomSessionId', $this->testSessionRecord);

$expected = array_merge($this->testSessionRecord, ['ses_tstamp' => $GLOBALS['EXEC_TIME']]);

self::assertEquals($record, $expected);
$result = $this->subject->get('randomSessionId');
self::assertSame($expected, $result);
Expand All @@ -120,9 +100,7 @@ public function sessionDataIsStoredProperly(): void
public function anonymousSessionDataIsStoredProperly(): void
{
$record = $this->subject->set('randomSessionId', array_merge($this->testSessionRecord, ['ses_userid' => 0]));

$expected = array_merge($this->testSessionRecord, ['ses_userid' => 0, 'ses_tstamp' => $GLOBALS['EXEC_TIME']]);

self::assertEquals($record, $expected);
self::assertSame($expected, $this->subject->get('randomSessionId'));
}
Expand All @@ -139,7 +117,6 @@ public function throwExceptionOnNonExistingSessionId(): void
public function mergeSessionDataWithNewData(): void
{
$this->subject->set('randomSessionId', $this->testSessionRecord);

$updateData = [
'ses_data' => serialize(['foo' => 'baz', 'idontwantto' => 'set the world on fire']),
'ses_tstamp' => $GLOBALS['EXEC_TIME'],
Expand All @@ -155,9 +132,7 @@ public function existingSessionMustNotBeOverridden(): void
{
$this->expectException(SessionNotCreatedException::class);
$this->expectExceptionCode(1481895647);

$this->subject->set('randomSessionId', $this->testSessionRecord);

$newData = array_merge($this->testSessionRecord, ['ses_data' => serialize(['foo' => 'baz', 'idontwantto' => 'set the world on fire'])]);
$this->subject->set('randomSessionId', $newData);
}
Expand All @@ -166,16 +141,12 @@ public function existingSessionMustNotBeOverridden(): void
public function cannotChangeSessionId(): void
{
$this->subject->set('randomSessionId', $this->testSessionRecord);

$newSessionId = 'newRandomSessionId';
$newData = array_merge($this->testSessionRecord, ['ses_id' => $newSessionId]);

// old session id has to exist, no exception must be thrown at this point
$this->subject->get('randomSessionId');

// Change session id
$this->subject->update('randomSessionId', $newData);

// no session with key newRandomSessionId should exist
$this->expectException(SessionNotFoundException::class);
$this->expectExceptionCode(1481885583);
Expand All @@ -186,10 +157,8 @@ public function cannotChangeSessionId(): void
public function sessionGetsDestroyed(): void
{
$this->subject->set('randomSessionId', $this->testSessionRecord);

// Remove session
self::assertTrue($this->subject->remove('randomSessionId'));

// Check if session was really removed
$this->expectException(SessionNotFoundException::class);
$this->expectExceptionCode(1481885583);
Expand All @@ -201,7 +170,6 @@ public function canLoadAllSessions(): void
{
$this->subject->set('randomSessionId', $this->testSessionRecord);
$this->subject->set('randomSessionId2', $this->testSessionRecord);

// Check if session was really removed
self::assertCount(2, $this->subject->getAll());
}
Expand All @@ -212,10 +180,8 @@ public function canCollectGarbage(): void
$GLOBALS['EXEC_TIME'] = 150;
$authenticatedSession = array_merge($this->testSessionRecord, ['ses_id' => 'authenticatedSession']);
$anonymousSession = array_merge($this->testSessionRecord, ['ses_id' => 'anonymousSession', 'ses_userid' => 0]);

$this->subject->set('authenticatedSession', $authenticatedSession);
$this->subject->set('anonymousSession', $anonymousSession);

// Assert that we set authenticated session correctly
self::assertSame(
$authenticatedSession['ses_data'],
Expand All @@ -225,18 +191,15 @@ public function canCollectGarbage(): void
$authenticatedSession['ses_userid'],
$this->subject->get('authenticatedSession')['ses_userid']
);

// assert that we set anonymous session correctly
self::assertSame(
$anonymousSession['ses_data'],
$this->subject->get('anonymousSession')['ses_data']
);

// Run the garbage collection
$GLOBALS['EXEC_TIME'] = 200;
// 150 + 10 < 200 but 150 + 60 >= 200
$this->subject->collectGarbage(60, 10);

// Authenticated session should still be there
self::assertSame(
$authenticatedSession['ses_data'],
Expand All @@ -246,7 +209,6 @@ public function canCollectGarbage(): void
$authenticatedSession['ses_userid'],
$this->subject->get('authenticatedSession')['ses_userid']
);

// Non-authenticated session should be removed
$this->expectException(SessionNotFoundException::class);
$this->expectExceptionCode(1481885583);
Expand Down
Expand Up @@ -17,17 +17,16 @@

namespace TYPO3\CMS\Core\Tests\Unit\Locking;

use PHPUnit\Framework\Attributes\RequiresFunction;
use PHPUnit\Framework\Attributes\Test;
use TYPO3\CMS\Core\Locking\SemaphoreLockStrategy;
use TYPO3\CMS\Core\Locking\SimpleLockStrategy;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;

/**
* @requires function sem_get
*/
final class SemaphoreLockStrategyTest extends UnitTestCase
{
#[Test]
#[RequiresFunction('sem_get')]
public function acquireGetsSemaphore(): void
{
$lock = new SemaphoreLockStrategy('99999');
Expand All @@ -46,7 +45,6 @@ public function getPriorityReturnsDefaultPriority(): void
public function setPriority(): void
{
$GLOBALS['TYPO3_CONF_VARS']['SYS']['locking']['strategies'][SemaphoreLockStrategy::class]['priority'] = 10;

self::assertEquals(10, SemaphoreLockStrategy::getPriority());
unset($GLOBALS['TYPO3_CONF_VARS']['SYS']['locking']['strategies'][SemaphoreLockStrategy::class]['priority']);
}
Expand Down

0 comments on commit d2964e3

Please sign in to comment.