DigiComp.Sequence/Tests/Functional/SequenceTest.php

70 lines
2.1 KiB
PHP
Raw Permalink Normal View History

<?php
2020-03-10 14:48:58 +01:00
2021-09-22 11:34:34 +02:00
declare(strict_types=1);
namespace DigiComp\Sequence\Tests\Functional;
2021-09-22 11:34:34 +02:00
use DigiComp\Sequence\Service\Exception\InvalidSourceException;
use DigiComp\Sequence\Service\SequenceGenerator;
2021-09-21 10:39:03 +02:00
use Doctrine\DBAL\Driver\Exception as DoctrineDBALDriverException;
use Doctrine\DBAL\Exception as DoctrineDBALException;
2017-03-13 16:59:04 +01:00
use Neos\Flow\Tests\FunctionalTestCase;
class SequenceTest extends FunctionalTestCase
{
2017-03-13 16:59:04 +01:00
/**
2021-09-21 10:39:03 +02:00
* @inheritDoc
2017-03-13 16:59:04 +01:00
*/
protected static $testablePersistenceEnabled = true;
/**
* @test
2021-09-21 10:39:03 +02:00
* @throws DoctrineDBALDriverException
* @throws DoctrineDBALException
2021-09-22 11:34:34 +02:00
* @throws InvalidSourceException
*/
2021-09-23 08:55:03 +02:00
public function sequenceTest(): void
{
$sequenceGenerator = $this->objectManager->get(SequenceGenerator::class);
2022-04-20 17:36:31 +02:00
self::assertEquals(0, $sequenceGenerator->getLastNumberFor($sequenceGenerator));
self::assertEquals(1, $sequenceGenerator->getNextNumberFor($sequenceGenerator));
2017-03-13 16:59:04 +01:00
$pIds = [];
for ($i = 0; $i < 10; $i++) {
2021-09-16 15:30:22 +02:00
$pId = \pcntl_fork();
2021-09-22 11:34:34 +02:00
if ($pId > 0) {
2017-03-13 16:59:04 +01:00
$pIds[] = $pId;
2020-03-10 14:48:58 +01:00
} else {
for ($j = 0; $j < 10; $j++) {
$sequenceGenerator->getNextNumberFor($sequenceGenerator);
}
// making a hard exit to avoid phpunit having the tables cleaned up again
exit;
}
}
2017-03-13 16:59:04 +01:00
foreach ($pIds as $pId) {
$status = 0;
2021-09-16 15:30:22 +02:00
\pcntl_waitpid($pId, $status);
}
2017-03-13 16:59:04 +01:00
2022-04-20 17:36:31 +02:00
self::assertEquals(101, $sequenceGenerator->getLastNumberFor($sequenceGenerator));
}
/**
* @test
2021-09-21 10:39:03 +02:00
* @throws DoctrineDBALDriverException
* @throws DoctrineDBALException
2021-09-22 11:34:34 +02:00
* @throws InvalidSourceException
*/
2021-09-23 08:55:03 +02:00
public function setLastNumberForTest(): void
{
$sequenceGenerator = $this->objectManager->get(SequenceGenerator::class);
2021-09-22 11:34:34 +02:00
$sequenceGenerator->setLastNumberFor($sequenceGenerator, 100);
2022-04-20 17:36:31 +02:00
self::assertEquals(100, $sequenceGenerator->getLastNumberFor($sequenceGenerator));
self::assertEquals(0, $sequenceGenerator->getLastNumberFor('otherSequence'));
}
}