From 09c8e421fab8895216608dce72bba1e00652a85a Mon Sep 17 00:00:00 2001 From: Ferdinand Kuhl Date: Fri, 24 Jun 2016 20:35:59 +0200 Subject: [PATCH] TASK: Adding functional test guarantee basic functionality, small refactoring of SequnceGenerator --- ChangeLog.md | 3 -- .../Command/SequenceCommandController.php | 7 ++- .../DigiComp/Sequence/Domain/Model/Insert.php | 1 + .../Sequence/Service/SequenceGenerator.php | 29 +++++++--- Configuration/Testing/Settings.yaml | 6 +++ Tests/Functional/SequenceTest.php | 54 +++++++++++++++++++ composer.json | 3 +- 7 files changed, 89 insertions(+), 14 deletions(-) delete mode 100644 ChangeLog.md create mode 100644 Configuration/Testing/Settings.yaml create mode 100644 Tests/Functional/SequenceTest.php diff --git a/ChangeLog.md b/ChangeLog.md deleted file mode 100644 index 0047f4a..0000000 --- a/ChangeLog.md +++ /dev/null @@ -1,3 +0,0 @@ -new in 1.0: - - - taken from DigiComp.Controls \ No newline at end of file diff --git a/Classes/DigiComp/Sequence/Command/SequenceCommandController.php b/Classes/DigiComp/Sequence/Command/SequenceCommandController.php index 8d07bab..4f55521 100644 --- a/Classes/DigiComp/Sequence/Command/SequenceCommandController.php +++ b/Classes/DigiComp/Sequence/Command/SequenceCommandController.php @@ -7,13 +7,14 @@ namespace DigiComp\Sequence\Command; * */ use TYPO3\Flow\Annotations as Flow; +use TYPO3\Flow\Cli\CommandController; /** - * A SequenceNumber generator (should be DB-agnostic) + * A database agnostic SequenceNumber generator * * @Flow\Scope("singleton") */ -class SequenceCommandController extends \TYPO3\Flow\Cli\CommandController +class SequenceCommandController extends CommandController { /** @@ -32,4 +33,6 @@ class SequenceCommandController extends \TYPO3\Flow\Cli\CommandController { $this->sequenceGenerator->advanceTo($to, $type); } + + //TODO: make clean up job to delete all but the biggest number to save resources } diff --git a/Classes/DigiComp/Sequence/Domain/Model/Insert.php b/Classes/DigiComp/Sequence/Domain/Model/Insert.php index 3665280..2ebdc0a 100644 --- a/Classes/DigiComp/Sequence/Domain/Model/Insert.php +++ b/Classes/DigiComp/Sequence/Domain/Model/Insert.php @@ -15,6 +15,7 @@ use Doctrine\ORM\Mapping as ORM; * @author fcool * @Flow\Scope("prototype") * @Flow\Entity + * @ORM\Table(indexes={@ORM\Index(name="type_idx", columns={"type"})}) */ class Insert { diff --git a/Classes/DigiComp/Sequence/Service/SequenceGenerator.php b/Classes/DigiComp/Sequence/Service/SequenceGenerator.php index cc072d9..e8452ee 100644 --- a/Classes/DigiComp/Sequence/Service/SequenceGenerator.php +++ b/Classes/DigiComp/Sequence/Service/SequenceGenerator.php @@ -47,12 +47,7 @@ class SequenceGenerator */ public function getNextNumberFor($type) { - if (is_object($type)) { - $type = $this->reflectionService->getClassNameByObject($type); - } - if (!$type) { - throw new Exception('No Type given'); - } + $type = $this->inferTypeFromSource($type); $count = $this->getLastNumberFor($type); //TODO: Check for maximal tries, or similar @@ -89,25 +84,43 @@ class SequenceGenerator public function advanceTo($to, $type) { + $type = $this->inferTypeFromSource($type); return ($this->validateFreeNumber($to, $type)); } /** - * @param $type + * @param string|object $type * * @return int */ public function getLastNumberFor($type) { + $type = $this->inferTypeFromSource($type); /** @var $em \Doctrine\ORM\EntityManager */ $em = $this->entityManager; $result = $em->getConnection()->executeQuery( 'SELECT MAX(number) AS count FROM digicomp_sequence_domain_model_insert WHERE type=:type', - array('type' => $type) + ['type' => $type] ); $count = $result->fetchAll(); $count = $count[0]['count']; return $count; } + + /** + * @param string|object $stringOrObject + * + * @throws Exception + * @return string + */ + protected function inferTypeFromSource($stringOrObject) { + if (is_object($stringOrObject)) { + $stringOrObject = $this->reflectionService->getClassNameByObject($stringOrObject); + } + if (!$stringOrObject) { + throw new Exception('No Type given'); + } + return $stringOrObject; + } } diff --git a/Configuration/Testing/Settings.yaml b/Configuration/Testing/Settings.yaml new file mode 100644 index 0000000..b72d1a0 --- /dev/null +++ b/Configuration/Testing/Settings.yaml @@ -0,0 +1,6 @@ +TYPO3: + Flow: + persistence: + backendOptions: + driver: 'pdo_sqlite' + path: %FLOW_PATH_DATA%/Temporary/testing.db \ No newline at end of file diff --git a/Tests/Functional/SequenceTest.php b/Tests/Functional/SequenceTest.php new file mode 100644 index 0000000..26d02b8 --- /dev/null +++ b/Tests/Functional/SequenceTest.php @@ -0,0 +1,54 @@ +objectManager->get(SequenceGenerator::class); + + $number = $sequenceGenerator->getLastNumberFor($sequenceGenerator); + $this->assertEquals(0, $number); + $this->assertEquals(1, $sequenceGenerator->getNextNumberFor($sequenceGenerator)); + + $pids = []; + for ($i = 0; $i < 10; $i++) { + $pid = pcntl_fork(); + if ($pid) { + $pids[] = $pid; + } else { + for ($j = 0; $j < 10; $j++) { + $sequenceGenerator->getNextNumberFor($sequenceGenerator); + } + //making a hard exit to avoid phpunit having the tables cleaned up again + exit; + } + } + foreach ($pids as $pid) { + $status = 0; + pcntl_waitpid($pid, $status); + } + $this->assertEquals(101, $sequenceGenerator->getLastNumberFor($sequenceGenerator)); + } + + /** + * @test + */ + public function advanceTest() + { + $sequenceGenerator = $this->objectManager->get(SequenceGenerator::class); + + $sequenceGenerator->advanceTo(100, $sequenceGenerator); + $this->assertEquals(100, $sequenceGenerator->getLastNumberFor($sequenceGenerator)); + $this->assertEquals(0, $sequenceGenerator->getLastNumberFor('strangeOtherSequence')); + } +} diff --git a/composer.json b/composer.json index 08889e8..7db1e17 100644 --- a/composer.json +++ b/composer.json @@ -6,7 +6,8 @@ "typo3/flow": "~2.0|~3.0" }, "require-dev": { - "phpunit/phpunit": "3.7.*" + "phpunit/phpunit": "3.7.*", + "ext-pcntl": "*" }, "autoload": { "psr-0": {