Skip to content

Commit

Permalink
feat:
Browse files Browse the repository at this point in the history
- update from nimut/testing-framework to typo/testing-framework
- updates for php 8.2
- updated code-styles
  • Loading branch information
tlayh committed Mar 4, 2024
1 parent 22f59a6 commit a3d9e09
Show file tree
Hide file tree
Showing 15 changed files with 182 additions and 356 deletions.
48 changes: 10 additions & 38 deletions Classes/Sequenzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@

/**
* Sequenzer is used to generate system wide independent IDs
*
* @package Aoe\AoeDbSequenzer
*/
class Sequenzer
{
Expand All @@ -42,54 +40,37 @@ class Sequenzer
*/
public const SEQUENZER_TABLE = 'tx_aoedbsequenzer_sequenz';

/**
* @var integer
*/
private $defaultStart = 0;
private int $defaultStart = 0;

/**
* @var integer
*/
private $defaultOffset = 1;
private int $defaultOffset = 1;

/**
* @var integer in seconds
* @var int in seconds
*/
private $checkInterval = 120;
private int $checkInterval = 120;

/**
* @param integer $defaultStart to set
*/
public function setDefaultStart($defaultStart): void
public function setDefaultStart(int $defaultStart): void
{
$this->defaultStart = $defaultStart;
}

/**
* @param integer $defaultOffset to set
*/
public function setDefaultOffset($defaultOffset): void
public function setDefaultOffset(int $defaultOffset): void
{
$this->defaultOffset = $defaultOffset;
}

/**
* returns next free id in the sequenz of the table
*
* @param string $table
* @param int $depth
*
* @return int
* @throws \Exception
*/
public function getNextIdForTable($table, $depth = 0)
public function getNextIdForTable(string $table, int $depth = 0): int
{
if ($depth > 99) {
throw new \Exception(
'The sequenzer cannot return IDs for this table -' . $table . ' Too many recursions - maybe to much load?',
1512378158
);
}

/** @var Connection $databaseConnection */
$databaseConnection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable(self::SEQUENZER_TABLE);
$row = $databaseConnection->select(['*'], self::SEQUENZER_TABLE, ['tablename' => $table])->fetch();
Expand Down Expand Up @@ -126,13 +107,8 @@ public function getNextIdForTable($table, $depth = 0)

/**
* Gets the default start value for a given table.
*
* @param string $table
*
* @return int
* @throws \Exception
*/
private function getDefaultStartValue($table)
private function getDefaultStartValue(string $table): int
{
/** @var Connection $databaseConnection */
$databaseConnection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable(self::SEQUENZER_TABLE);
Expand All @@ -146,12 +122,8 @@ private function getDefaultStartValue($table)
/**
* if no scheduler entry for the table yet exists, this method initialises the sequenzer to fit offest and start and current max value
* in the table
*
* @param string $table
*
* @throws \Exception
*/
private function initSequenzerForTable($table): void
private function initSequenzerForTable(string $table): void
{
$start = $this->getDefaultStartValue($table);

Expand Down
23 changes: 5 additions & 18 deletions Classes/Service/Typo3Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,9 @@
use TYPO3\CMS\Core\SingletonInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
* @package Aoe\AoeDbSequenzer
*/
class Typo3Service implements SingletonInterface
{
/**
* @var Sequenzer
*/
private $sequenzer;
private Sequenzer $sequenzer;

/**
* @var array
Expand All @@ -54,16 +48,14 @@ class Typo3Service implements SingletonInterface

/**
* array of configured tables that should call the sequenzer
*
* @var array
*/
private $supportedTables = [];
private array $supportedTables;

public function __construct(Sequenzer $sequenzer)
{
$this->conf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('aoe_dbsequenzer');

$this->logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
$this->logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(self::class);

$this->sequenzer = $sequenzer;
$this->sequenzer->setDefaultOffset((int) $this->conf['offset']);
Expand All @@ -75,10 +67,8 @@ public function __construct(Sequenzer $sequenzer)

/**
* Modify a TYPO3 insert array (key -> value) , and adds the uid that should be forced during INSERT
*
* @param string $tableName
*/
public function modifyInsertFields($tableName, array $fields_values): array
public function modifyInsertFields(string $tableName, array $fields_values): array
{
if (!$this->needsSequenzer($tableName)) {
return $fields_values;
Expand All @@ -104,11 +94,8 @@ public function modifyInsertFields($tableName, array $fields_values): array

/**
* If a table is configured to use the sequenzer
*
* @param string $tableName
* @return boolean
*/
public function needsSequenzer($tableName)
public function needsSequenzer(string $tableName): bool
{
return in_array($tableName, $this->supportedTables);
}
Expand Down
12 changes: 2 additions & 10 deletions Classes/Xclass/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,9 @@
use TYPO3\CMS\Core\Database\Query\BulkInsertQuery;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
* @package Aoe\AoeDbSequenzer\Xclass
*/
class Connection extends CoreConnection
{
/**
* @var Typo3Service
*/
private $typo3Service;
private ?Typo3Service $typo3Service = null;

/**
* Inserts a table row with specified data.
Expand Down Expand Up @@ -102,10 +96,8 @@ public function update($tableName, array $data, array $identifier, array $types
* Why we do this?
* Because some unittests backup the variable $GLOBALS (and so, also the variable $GLOBALS['TYPO3_DB']), which means, that this
* object/class will be serialized/unserialized, so the instance of Typo3Service will be null after unserialization!
*
* @return Typo3Service
*/
protected function getTypo3Service()
protected function getTypo3Service(): Typo3Service
{
if (!isset($this->typo3Service)) {
$this->typo3Service = GeneralUtility::makeInstance(Typo3Service::class, new Sequenzer());
Expand Down
6 changes: 2 additions & 4 deletions Classes/Xclass/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@

use Aoe\AoeDbSequenzer\Sequenzer;
use Aoe\AoeDbSequenzer\Service\Typo3Service;
use mysql_xdevapi\Table;
use PDO;
use TYPO3\CMS\Core\Database\Query\QueryBuilder as CoreQueryBuilder;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
* @package Aoe\AoeDbSequenzer\Xclass
*/
class QueryBuilder extends CoreQueryBuilder
{
/**
Expand Down Expand Up @@ -88,7 +86,7 @@ protected function getTypo3Service()
protected function sanitizeTableName(string $tableName): string
{
$mark = '`';
if (!empty($tableName) && $tableName[0] === $mark && $tableName[strlen($tableName) - 1] === $mark) {
if ($tableName !== '' && $tableName !== '0' && $tableName[0] === $mark && $tableName[strlen($tableName) - 1] === $mark) {
return str_replace($mark, '', $tableName);
}

Expand Down
51 changes: 16 additions & 35 deletions Tests/Functional/SequenzerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

use Aoe\AoeDbSequenzer\Sequenzer;
use Exception;
use Nimut\TestingFramework\TestCase\FunctionalTestCase;
use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
use ReflectionException;
use ReflectionMethod;
use TYPO3\CMS\Core\Database\ConnectionPool;
Expand All @@ -42,30 +42,26 @@ class SequenzerTest extends FunctionalTestCase
*/
protected $subject;

protected $testExtensionsToLoad = ['typo3conf/ext/aoe_dbsequenzer'];
protected array $testExtensionsToLoad = ['typo3conf/ext/aoe_dbsequenzer'];

public function setUp(): void
protected function setUp(): void
{
parent::setUp();
$this->subject = new Sequenzer();
}

/**
* @test
* @throws Exception
*/
public function getNextIdForTable_throwsExceptionDepthToHigh()
public function testGetNextIdForTable_throwsExceptionDepthToHigh(): void
{
$this->expectException(Exception::class);
$this->expectExceptionCode(1512378158);

$this->subject->getNextIdForTable('tableName', 1000);
}

/**
* @test
*/
public function getNextIdForTable_NoPagesAdded()
public function testGetNextIdForTable_NoPagesAdded(): void
{
$this->subject->setDefaultOffset(10);
$this->subject->setDefaultStart(4);
Expand All @@ -76,73 +72,58 @@ public function getNextIdForTable_NoPagesAdded()
);
}

/**
* @test
*/
public function getNextIdForTable_OutDatedSequencerInformation()
public function testGetNextIdForTable_OutDatedSequencerInformation(): void
{
// Offset is set in Fixture (20)
// Current is set in Fixture (5)
$this->importDataSet(__DIR__ . '/Fixtures/tx_aoedbsequenzer_seqeunz.xml');
$this->importDataSet('ntf://Database/pages.xml');
$this->importDataSet('PACKAGE:typo3/testing-framework/Resources/Core/Functional/Fixtures/pages.xml');

$this->assertSame(
28,
$this->subject->getNextIdForTable('pages')
);
}

/**
* @test
*/
public function getDefaultStartValue_withoutOffsetConfigured()
public function testGetDefaultStartValue_withoutOffsetConfigured(): void
{
// Imports 7 pages, therefor the expected StartValue should be 8
$this->importDataSet('ntf://Database/pages.xml');
$this->importDataSet('PACKAGE:typo3/testing-framework/Resources/Core/Functional/Fixtures/pages.xml');
$method = $this->getPrivateMethod($this->subject, 'getDefaultStartValue');

$result = $method->invokeArgs($this->subject, ['pages']);
$this->assertSame(8, $result);
}

/**
* @test
*/
public function getDefaultStartValue_withOffsetConfigured()
public function testGetDefaultStartValue_withOffsetConfigured(): void
{
$this->subject->setDefaultOffset(6);
$this->importDataSet('ntf://Database/pages.xml');
$this->importDataSet('PACKAGE:typo3/testing-framework/Resources/Core/Functional/Fixtures/pages.xml');
$method = $this->getPrivateMethod($this->subject, 'getDefaultStartValue');
$result = $method->invokeArgs($this->subject, ['pages']);

// As the current largest uid is 7, and an offset of 6, the next free value would be 12
$this->assertSame(12, $result);
}

/**
* @test
*/
public function getDefaultStartValue_withOffsetAndDefaultStartValueConfigured()
public function testGetDefaultStartValue_withOffsetAndDefaultStartValueConfigured(): void
{
$this->subject->setDefaultOffset(14);
$this->subject->setDefaultStart(20);
$this->importDataSet('ntf://Database/pages.xml');
$this->importDataSet('PACKAGE:typo3/testing-framework/Resources/Core/Functional/Fixtures/pages.xml');
$method = $this->getPrivateMethod($this->subject, 'getDefaultStartValue');
$result = $method->invokeArgs($this->subject, ['pages']);

// As the current largest uid is 20, and an offset of 14, the next free value would be 34
$this->assertSame(34, $result);
}

/**
* @test
*/
public function initSequenzerForTable_isDataInsert()
public function testInitSequenzerForTable_isDataInsert(): void
{
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable(Sequenzer::SEQUENZER_TABLE);

// Before Init
$this->assertEquals(
$this->assertSame(
0,
$queryBuilder->count('*', Sequenzer::SEQUENZER_TABLE, ['tablename' => 'pages'])
);
Expand All @@ -151,7 +132,7 @@ public function initSequenzerForTable_isDataInsert()
$method->invokeArgs($this->subject, ['pages']);

// After Init
$this->assertEquals(
$this->assertSame(
1,
$queryBuilder->count('*', Sequenzer::SEQUENZER_TABLE, ['tablename' => 'pages'])
);
Expand Down
15 changes: 6 additions & 9 deletions Tests/Functional/Xclass/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Aoe\AoeDbSequenzer\Xclass\Connection;
use Doctrine\DBAL\Driver\PDOMySql\Driver;
use InvalidArgumentException;
use Nimut\TestingFramework\TestCase\FunctionalTestCase;
use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
use TYPO3\CMS\Core\Utility\GeneralUtility;

class ConnectionTest extends FunctionalTestCase
Expand All @@ -25,7 +25,7 @@ class ConnectionTest extends FunctionalTestCase
*/
protected $subject;

public function setUp(): void
protected function setUp(): void
{
parent::setUp();
$params = [];
Expand All @@ -35,18 +35,15 @@ public function setUp(): void

$typo3Service = $this->getMockBuilder(Typo3Service::class)
->disableOriginalConstructor()
->setMethods(['needsSequenzer'])
->onlyMethods(['needsSequenzer'])
->getMock();

$typo3Service->expects($this->any())->method('needsSequenzer')->willReturn($this->returnValue(true));
$typo3Service->method('needsSequenzer')->willReturn(true);

$this->inject($this->subject, 'typo3Service', $typo3Service);
GeneralUtility::setSingletonInstance(Typo3Service::class, $typo3Service);
}

/**
* @test
*/
public function updateThrowsExceptionWhenUidInFieldArray()
public function testUpdateThrowsExceptionWhenUidInFieldArray(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionCode(1564122222);
Expand Down
Loading

0 comments on commit a3d9e09

Please sign in to comment.