TASK: Adding functional test guarantee basic functionality, small refactoring of SequnceGenerator

This commit is contained in:
Ferdinand Kuhl 2016-06-24 20:35:59 +02:00
parent 52f5040270
commit 09c8e421fa
7 changed files with 89 additions and 14 deletions

View file

@ -1,3 +0,0 @@
new in 1.0:
- taken from DigiComp.Controls

View file

@ -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
}

View file

@ -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
{

View file

@ -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;
}
}

View file

@ -0,0 +1,6 @@
TYPO3:
Flow:
persistence:
backendOptions:
driver: 'pdo_sqlite'
path: %FLOW_PATH_DATA%/Temporary/testing.db

View file

@ -0,0 +1,54 @@
<?php
namespace DigiComp\Sequence\Tests\Functional;
use DigiComp\Sequence\Service\SequenceGenerator;
use TYPO3\Flow\Tests\FunctionalTestCase;
class SequenceTest extends FunctionalTestCase
{
protected static $testablePersistenceEnabled = true;
/**
* @test
*/
public function sequenceTest()
{
$sequenceGenerator = $this->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'));
}
}

View file

@ -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": {