Skip to content

Commit

Permalink
hotfix - ignored interval config in compound unit (#7)
Browse files Browse the repository at this point in the history
* hotfix - ignored interval config in compound unit (&test addition & LF formating update)

* hotfix - ignored interval config in compound unit - single used method removal

* hotfix - reverting down-deps composer.json to up-deps version & compound interval test cases
  • Loading branch information
raneomik authored May 1, 2022
1 parent 3571ea9 commit 00d01c5
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/Watchdog/Unit/Model/Compound.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class Compound implements WatchdogUnitInterface
{
private bool $logicalAndMode = false;
private bool $logicalAndMode;
private array $unitCollection = [];

public function __construct(array $data, bool $logicalAnd = false)
Expand Down
16 changes: 11 additions & 5 deletions src/Watchdog/Unit/UnitProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ class UnitProcessor
{
public function process(array $data): WatchdogUnitInterface
{
if (\array_key_exists('start', $data) || \array_key_exists('end', $data)) {
if (\array_key_exists(WatchdogUnitInterface::START, $data)
|| \array_key_exists(WatchdogUnitInterface::END, $data)
) {
return $this->intervalUnit($data);
}

return $this->unit($data);
}

private static function intervalUnit(array $data): Interval
private function intervalUnit(array $data): Interval
{
if (false === \is_string($start = $data['start'] ?? null)) {
throw new MalformedConfigurationValueException('Missing "start" data for interval');
Expand All @@ -37,12 +39,12 @@ private static function intervalUnit(array $data): Interval
return new Interval($start, $end);
}

private static function compoundUnit(array $data): Compound
private function compoundUnit(array $data): Compound
{
return new Compound($data, true);
}

private static function simpleUnit(string $key, string $value): ?WatchdogUnitInterface
private function simpleUnit(string $key, string $value): ?WatchdogUnitInterface
{
if (WatchdogUnitInterface::RELATIVE === $key) {
return new RelativeDateTime($value);
Expand Down Expand Up @@ -78,7 +80,11 @@ private function unit(array $data): WatchdogUnitInterface
*/
foreach ($data as $key => $value) {
if (\is_array($value)) {
return $this->compoundUnit($value);
if (WatchdogUnitInterface::COMPOUND === $key) {
return $this->compoundUnit($value);
}

return $this->process($value);
}

if (null !== $unit = $this->simpleUnit($key, $value)) {
Expand Down
4 changes: 2 additions & 2 deletions tests/DependencyInjection/WatchdogDependencyInjectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ public function testLoadMultiConfiguration(bool $fakeLegacy): void
'start' => (new \DateTime('-5mins'))->format('Y-m-d H:i'),
'end' => (new \DateTime('+5mins'))->format('Y-m-d H:i'),
],
'relative' => 'now',
['relative' => 'now'],
],
'test_two' => [
'relative' => 'tomorrow',
['relative' => 'tomorrow'],
],
],
]);
Expand Down
5 changes: 4 additions & 1 deletion tests/Integration/config/multi_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ watchdog:
- { start: '20:00', end: '22:30' }

test_two:
- date_time: '2019-12-01 11:00'
- compound:
- date_time: '2019-12-01 11:00'
- { start: '16:00', end: '22:30' }
- relative: 'today'
- start: '16:00'
end: '22:30'
77 changes: 75 additions & 2 deletions tests/Unit/AbstractWatchdogTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Raneomik\WatchdogBundle\Tests\Unit;

use PHPUnit\Framework\TestCase;
use Raneomik\WatchdogBundle\Watchdog\Unit\Model\WatchdogUnitInterface;
use Symfony\Component\Yaml\Yaml;

abstract class AbstractWatchdogTest extends TestCase
{
Expand Down Expand Up @@ -59,8 +61,8 @@ public function notWoofMatchCasesProvider(): \Generator
yield [[['relative' => 'tomorrow']]];
yield [[[
'compound' => [
['relative' => 'tomorrow'],
['start' => $now->format('H:i'), 'end' => $plus2Hours->format('H:i')],
['relative' => 'today'],
['start' => $plus1Hour->format('H:i'), 'end' => $plus2Hours->format('H:i')],
],
]]];
yield [[
Expand All @@ -70,4 +72,75 @@ public function notWoofMatchCasesProvider(): \Generator
['date' => (new \DateTime('+2 days'))->format('Y-m-d')],
]];
}

public function compoundIntervalInYamlProvider(): \Generator
{
$nowMinus1Minute = (new \DateTime('-1 minutes'))->format('H:i');
$nowPlus1Minute = (new \DateTime('+1 minutes'))->format('H:i');
$nowPlus2Minutes = (new \DateTime('+2 minutes'))->format('H:i');

$matchingYamlConfig = <<<YAML
---
- { start: '{plus1Minute}', end: '{plus2Minutes}' }
- compound:
- relative: 'today'
- { start: '{minus1Minute}', end: '{plus1Minute}' }
YAML;

$yamlConfig = Yaml::parse(str_replace(
['{minus1Minute}', '{plus1Minute}', '{plus2Minutes}'],
[$nowMinus1Minute, $nowPlus1Minute, $nowPlus2Minutes],
$matchingYamlConfig
));

$arrayConfig = [
[
WatchdogUnitInterface::START => $nowPlus1Minute,
WatchdogUnitInterface::END => $nowPlus2Minutes,
],
[
WatchdogUnitInterface::COMPOUND => [
[WatchdogUnitInterface::RELATIVE => 'today'],
[
WatchdogUnitInterface::START => $nowMinus1Minute,
WatchdogUnitInterface::END => $nowPlus1Minute,
],
],
],
];

yield [$yamlConfig, $arrayConfig, true];

$notMatchingYamlConfig = <<<YAML
---
- { start: '{plus1Minute}', end: '{plus2Minutes}' }
- compound:
- relative: 'today'
- { start: '{plus1Minute}', end: '{plus2Minutes}' }
YAML;

$yamlConfig = Yaml::parse(str_replace(
['{minus1Minute}', '{plus1Minute}', '{plus2Minutes}'],
[$nowMinus1Minute, $nowPlus1Minute, $nowPlus2Minutes],
$notMatchingYamlConfig
));

$arrayConfig = [
[
WatchdogUnitInterface::START => $nowPlus1Minute,
WatchdogUnitInterface::END => $nowPlus2Minutes,
],
[
WatchdogUnitInterface::COMPOUND => [
[WatchdogUnitInterface::RELATIVE => 'today'],
[
WatchdogUnitInterface::START => $nowPlus1Minute,
WatchdogUnitInterface::END => $nowPlus2Minutes,
],
],
],
];

yield [$yamlConfig, $arrayConfig, false];
}
}
13 changes: 13 additions & 0 deletions tests/Unit/WatchdogTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ public function testKoCases(array $timeRule): void
$this->assertFalse($watchDog->isWoofTime());
}

/**
* @dataProvider compoundIntervalInYamlProvider
*/
public function testYamlToArrayConfig(array $yamlConfig, array $arrayConfig, bool $watchdogWoof): void
{
$this->assertSame($arrayConfig, $yamlConfig);

$yamlWatchdog = new Watchdog($yamlConfig);
$this->assertSame($watchdogWoof, $yamlWatchdog->isWoofTime());
$arrayWatchdog = new Watchdog($arrayConfig);
$this->assertSame($watchdogWoof, $arrayWatchdog->isWoofTime());
}

public function testNotSupportedConfigExceptionCase(): void
{
$this->expectException(NotSupportedConfigurationException::class);
Expand Down
24 changes: 20 additions & 4 deletions tests/Unit/WatchdogUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ public function matchingUnitProvider(): \Generator
yield [new RelativeDateTime('now')];

yield [new Compound([
[WatchdogUnitInterface::RELATIVE => 'now'],
[WatchdogUnitInterface::TIME => $hour],
[WatchdogUnitInterface::DATE => $date],
[WatchdogUnitInterface::DATE_TIME => $nowString],
[WatchdogUnitInterface::RELATIVE => 'today'],
[
WatchdogUnitInterface::START => $nowMinus1Minutes->format('Y-m-d H:i'),
WatchdogUnitInterface::END => $nowPlus1Minutes->format('Y-m-d H:i'),
],
], true)];
yield [new Compound($atLeastOneCompound = [
[WatchdogUnitInterface::RELATIVE => 'tomorrow'],
Expand All @@ -56,6 +57,7 @@ public function notMatchingUnitProvider(): \Generator
{
$now = new \DateTime();
$notNow = new \DateTime('+1 day +1 hour');
$nowMinus2Minutes = new \DateTime('-2 minutes');
$nowPlus2Minutes = new \DateTime('+2 minutes');

if (0 !== (int) ($nowPlus2Minutes->format('d') - $now->format('d'))) {
Expand All @@ -80,6 +82,10 @@ public function notMatchingUnitProvider(): \Generator
[WatchdogUnitInterface::TIME => $hour],
[WatchdogUnitInterface::DATE => $date],
[WatchdogUnitInterface::DATE_TIME => $dateTime],
[
WatchdogUnitInterface::START => $nowPlus2Minutes->format('Y-m-d H:i'),
WatchdogUnitInterface::END => $notNow->format('Y-m-d H:i'),
],
], true)];
yield [new Compound($noneCompound = [
[WatchdogUnitInterface::RELATIVE => 'tomorrow'],
Expand All @@ -94,6 +100,16 @@ public function notMatchingUnitProvider(): \Generator
WatchdogUnitInterface::TIME => $hour,
WatchdogUnitInterface::COMPOUND => $noneCompound,
])];
yield [WatchdogUnitFactory::create([
WatchdogUnitInterface::TIME => $hour,
WatchdogUnitInterface::COMPOUND => new Compound([
[WatchdogUnitInterface::RELATIVE => 'today'],
[
WatchdogUnitInterface::START => $nowMinus2Minutes->format('Y-m-d H:i'),
WatchdogUnitInterface::END => $nowPlus2Minutes->format('Y-m-d H:i'),
],
], true),
])];
}

/**
Expand Down

0 comments on commit 00d01c5

Please sign in to comment.