Skip to content

Commit

Permalink
FRW-8851 Introduced a flag to allow process caching disabling. (#11059)
Browse files Browse the repository at this point in the history
FRW-8851 Introduced a flag to allow process caching disabling.
  • Loading branch information
spryker-release-bot authored Sep 19, 2024
1 parent 52bd9a8 commit f324d7e
Show file tree
Hide file tree
Showing 8 changed files with 219 additions and 5 deletions.
10 changes: 10 additions & 0 deletions src/Spryker/Shared/Oms/OmsConstants.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,14 @@ interface OmsConstants
* @var string
*/
public const PROCESS_CACHE_PATH = 'OMS:PROCESS_CACHE_PATH';

/**
* Specification:
* - Defines if automatic processes caching is enabled.
*
* @api
*
* @var string
*/
public const ENABLE_PROCESS_CACHE = 'OMS:ENABLE_PROCESS_CACHE';
}
1 change: 1 addition & 0 deletions src/Spryker/Zed/Oms/Business/OmsBusinessFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ public function createOrderStateMachineBuilder()
$this->getConfig()->getProcessDefinitionLocation(),
$this->createProcessCacheReader(),
$this->createProcessCacheWriter(),
$this->getConfig(),
$this->getConfig()->getSubProcessPrefixDelimiter(),
);
}
Expand Down
28 changes: 23 additions & 5 deletions src/Spryker/Zed/Oms/Business/OrderStateMachine/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Spryker\Zed\Oms\Business\Process\TransitionInterface;
use Spryker\Zed\Oms\Business\Reader\ProcessCacheReaderInterface;
use Spryker\Zed\Oms\Business\Writer\ProcessCacheWriterInterface;
use Spryker\Zed\Oms\OmsConfig;
use Symfony\Component\Finder\Finder as SymfonyFinder;

class Builder implements BuilderInterface
Expand Down Expand Up @@ -70,6 +71,11 @@ class Builder implements BuilderInterface
*/
protected ProcessCacheWriterInterface $processCacheWriter;

/**
* @var \Spryker\Zed\Oms\OmsConfig
*/
protected OmsConfig $config;

/**
* @param \Spryker\Zed\Oms\Business\Process\EventInterface $event
* @param \Spryker\Zed\Oms\Business\Process\StateInterface $state
Expand All @@ -78,6 +84,7 @@ class Builder implements BuilderInterface
* @param array|string $processDefinitionLocation
* @param \Spryker\Zed\Oms\Business\Reader\ProcessCacheReaderInterface $processCacheReader
* @param \Spryker\Zed\Oms\Business\Writer\ProcessCacheWriterInterface $processCacheWriter
* @param \Spryker\Zed\Oms\OmsConfig $config
* @param string $subProcessPrefixDelimiter
*/
public function __construct(
Expand All @@ -88,6 +95,7 @@ public function __construct(
$processDefinitionLocation,
ProcessCacheReaderInterface $processCacheReader,
ProcessCacheWriterInterface $processCacheWriter,
OmsConfig $config,
$subProcessPrefixDelimiter = ' - '
) {
$this->event = $event;
Expand All @@ -96,6 +104,7 @@ public function __construct(
$this->process = $process;
$this->processCacheReader = $processCacheReader;
$this->processCacheWriter = $processCacheWriter;
$this->config = $config;
$this->subProcessPrefixDelimiter = $subProcessPrefixDelimiter;

$this->setProcessDefinitionLocation($processDefinitionLocation);
Expand All @@ -112,17 +121,26 @@ public function createProcess($processName)
return static::$processBuffer[$processName];
}

if ($this->processCacheReader->hasProcess($processName)) {
static::$processBuffer[$processName] = $this->processCacheReader->getProcess($processName);
if (!$this->config->isProcessCacheEnabled()) {
$mainProcess = $this->createMainProcess($processName);

static::$processBuffer[$processName] = $mainProcess;

return static::$processBuffer[$processName];
}

$mainProcess = $this->createMainProcess($processName);
$isProcessCached = $this->processCacheReader->hasProcess($processName);

static::$processBuffer[$processName] = $mainProcess;
if ($isProcessCached) {
$process = $this->processCacheReader->getProcess($processName);
}

if (!$isProcessCached) {
$process = $this->createMainProcess($processName);
$this->processCacheWriter->cacheProcess($process, $processName);
}

$this->processCacheWriter->cacheProcess($mainProcess, $processName);
static::$processBuffer[$processName] = $process;

return static::$processBuffer[$processName];
}
Expand Down
13 changes: 13 additions & 0 deletions src/Spryker/Zed/Oms/OmsConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,19 @@ public function getProcessCachePath(): string
);
}

/**
* Specification:
* - Defines if automatic processes caching is enabled.
*
* @api
*
* @return bool
*/
public function isProcessCacheEnabled(): bool
{
return (bool)$this->get(OmsConstants::ENABLE_PROCESS_CACHE, true);
}

/**
* Specification:
* - Returns process cache file permission.
Expand Down
156 changes: 156 additions & 0 deletions tests/SprykerTest/Zed/Oms/Business/OrderStateMachine/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
use Spryker\Zed\Oms\Business\Process\ProcessInterface;
use Spryker\Zed\Oms\Business\Process\StateInterface;
use Spryker\Zed\Oms\Business\Process\TransitionInterface;
use Spryker\Zed\Oms\Business\Reader\ProcessCacheReader;
use Spryker\Zed\Oms\Business\Reader\ProcessCacheReaderInterface;
use Spryker\Zed\Oms\Business\Util\DrawerInterface;
use Spryker\Zed\Oms\Business\Writer\ProcessCacheWriter;
use Spryker\Zed\Oms\OmsConfig;
use SprykerTest\Zed\Oms\OmsBusinessTester;

/**
Expand Down Expand Up @@ -117,6 +121,157 @@ public function testGetProcess(): void
$this->assertInstanceOf(ProcessInterface::class, $result);
}

/**
* @dataProvider omsProcessCachingReaderDataProvider
*
* @param bool $cacheIsEnabled
* @param int $expectedReaderCalls
*
* @return void
*/
public function testCreateProcessShouldReadFromCacheIfCacheIsEnabled(
bool $cacheIsEnabled,
int $expectedReaderCalls
): void {
// Arrange
$process = $this->getProcess();
$omsConfigMock = $this->createMock(OmsConfig::class);
$omsConfigMock
->method('isProcessCacheEnabled')
->willReturn($cacheIsEnabled);

$processCacheReaderMock = $this->getMockBuilder(ProcessCacheReader::class)
->setConstructorArgs([new OmsConfig()])
->onlyMethods(['getProcess'])
->getMock();

if (file_exists($processCacheReaderMock->getFullFilename('process-a'))) {
unlink($processCacheReaderMock->getFullFilename('process-a'));
}

$builder = new Builder(
$this->getEventMock(),
$this->getStateMock(),
$this->getTransitionMock(),
$process,
[$this->getProcessLocationA()],
$processCacheReaderMock,
$this->tester->createProcessCacheWriter(),
$omsConfigMock,
);

// Assert
$processCacheReaderMock
->expects($this->exactly($expectedReaderCalls))
->method('getProcess')
->with('process-a')
->willReturn($process);

// Act
$this->tester->resetProcessBuffer();
$builder->createProcess('process-a');

$this->tester->resetProcessBuffer();
$builder->createProcess('process-a');

$this->tester->resetProcessBuffer();
$builder->createProcess('process-a');
}

/**
* @dataProvider omsProcessCachingWriterDataProvider
*
* @param bool $cacheIsEnabled
* @param int $expectedReaderCalls
* @param int $expectedWriterCalls
*
* @return void
*/
public function testCreateProcessShouldWriteToCacheIfCacheIsEnabled(
bool $cacheIsEnabled,
int $expectedReaderCalls,
int $expectedWriterCalls
): void {
// Arrange
$omsConfigMock = $this->createMock(OmsConfig::class);
$omsConfigMock
->method('isProcessCacheEnabled')
->willReturn($cacheIsEnabled);

$processCacheReaderMock = $this->createMock(ProcessCacheReaderInterface::class);

$processCacheWriterMock = $this->getMockBuilder(ProcessCacheWriter::class)
->setConstructorArgs([$omsConfigMock, $this->tester->createProcessCacheReader()])
->onlyMethods(['cacheProcess'])
->getMock();

// Assert
$processCacheReaderMock
->expects($this->exactly($expectedReaderCalls))
->method('hasProcess')
->withConsecutive(['process-a'], ['process-a'])
->willReturnOnConsecutiveCalls(false, true);

$processCacheWriterMock
->expects($this->exactly($expectedWriterCalls))
->method('cacheProcess')
->willReturn($this->tester->createProcessCacheReader()->getFullFilename('process-a'));

$builder = new Builder(
$this->getEventMock(),
$this->getStateMock(),
$this->getTransitionMock(),
$this->getProcess(),
[$this->getProcessLocationA()],
$processCacheReaderMock,
$processCacheWriterMock,
$omsConfigMock,
);

// Act
$this->tester->resetProcessBuffer();
$builder->createProcess('process-a');

$this->tester->resetProcessBuffer();
$builder->createProcess('process-a');
}

/**
* @return array<string, array<string|mixed>>
*/
protected function omsProcessCachingWriterDataProvider(): array
{
return [
'test when cache is enabled' => [
'cacheIsEnabled' => true,
'expectedReaderCalls' => 2,
'expectedWriterCalls' => 1,
],
'test when cache is disabled' => [
'cacheIsEnabled' => false,
'expectedReaderCalls' => 0,
'expectedWriterCalls' => 0,
],
];
}

/**
* @return array<string, array<string|mixed>>
*/
protected function omsProcessCachingReaderDataProvider(): array
{
return [
'test when cache is enabled' => [
'cacheIsEnabled' => true,
'expectedReaderCalls' => 2,
],
'test when cache is disabled' => [
'cacheIsEnabled' => false,
'expectedReaderCalls' => 0,
],
];
}

/**
* @return \PHPUnit\Framework\MockObject\MockObject|\Spryker\Zed\Oms\Business\Process\EventInterface
*/
Expand Down Expand Up @@ -195,6 +350,7 @@ private function createBuilder(array|string|null $processDefinitionLocation = nu
$processDefinitionLocation,
$this->tester->createProcessCacheReader(),
$this->tester->createProcessCacheWriter(),
new OmsConfig(),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Spryker\Zed\Oms\Business\Process\State;
use Spryker\Zed\Oms\Business\Process\Transition;
use Spryker\Zed\Oms\Business\Util\DrawerInterface;
use Spryker\Zed\Oms\OmsConfig;
use Spryker\Zed\Oms\Persistence\OmsQueryContainerInterface;
use SprykerTest\Zed\Oms\OmsBusinessTester;

Expand Down Expand Up @@ -159,6 +160,7 @@ protected function createBuilder(DrawerInterface $drawerMock): Builder
$this->getProcessLocation(),
$this->tester->createProcessCacheReader(),
$this->tester->createProcessCacheWriter(),
new OmsConfig(),
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Spryker\Zed\Oms\Business\Util\ActiveProcessFetcherInterface;
use Spryker\Zed\Oms\Business\Util\DrawerInterface;
use Spryker\Zed\Oms\Business\Util\ReadOnlyArrayObject;
use Spryker\Zed\Oms\OmsConfig;
use SprykerTest\Zed\Oms\OmsBusinessTester;

/**
Expand Down Expand Up @@ -140,6 +141,7 @@ protected function createBuilder(DrawerInterface $drawerMock): BuilderInterface
[$this->getProcessLocation()],
$this->tester->createProcessCacheReader(),
$this->tester->createProcessCacheWriter(),
new OmsConfig(),
);
}

Expand Down
12 changes: 12 additions & 0 deletions tests/SprykerTest/Zed/Oms/_support/OmsBusinessTester.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use Spryker\Zed\MessageBroker\MessageBrokerDependencyProvider;
use Spryker\Zed\Oms\Business\Lock\LockerInterface;
use Spryker\Zed\Oms\Business\Lock\TriggerLocker;
use Spryker\Zed\Oms\Business\OrderStateMachine\Builder;
use Spryker\Zed\Oms\Business\OrderStateMachine\LockedOrderStateMachine;
use Spryker\Zed\Oms\Business\OrderStateMachine\OrderStateMachineInterface;
use Spryker\Zed\Oms\Business\Reader\ProcessCacheReader;
Expand Down Expand Up @@ -520,4 +521,15 @@ public function createProcessCacheWriter(): ProcessCacheWriterInterface
{
return new ProcessCacheWriter(new OmsConfig(), $this->createProcessCacheReader());
}

/**
* @return void
*/
public function resetProcessBuffer(): void
{
$reflection = new ReflectionClass(Builder::class);
$property = $reflection->getProperty('processBuffer');
$property->setAccessible(true);
$property->setValue(null, []);
}
}

0 comments on commit f324d7e

Please sign in to comment.