diff --git a/src/Command/CheckCommand.php b/src/Command/CheckCommand.php index 6e9f76d..c1cbf97 100644 --- a/src/Command/CheckCommand.php +++ b/src/Command/CheckCommand.php @@ -43,6 +43,12 @@ private function printResult(SymfonyStyle $io, $result, $previousGroup = null, $ $rows = []; $subs = []; foreach ($items as $subGroup => $row) { + if (is_array($row)) { + $subs[$subGroup] = $row; + + continue; + } + if ($row instanceof AbstractSensor) { $rows[] = [ sprintf('%s', $row->getState()->getCliColor(), $row->getState()->getIcon()), @@ -55,8 +61,6 @@ private function printResult(SymfonyStyle $io, $result, $previousGroup = null, $ $row->getName(), $row->getValue(), ]; - } elseif (is_array($row)) { - $subs[$subGroup] = $row; } } diff --git a/src/Manager/MonitoringManager.php b/src/Manager/MonitoringManager.php index 7545a5b..a1aaca7 100644 --- a/src/Manager/MonitoringManager.php +++ b/src/Manager/MonitoringManager.php @@ -7,7 +7,8 @@ use whatwedo\MonitorBundle\Enums\MetricStateEnum; use whatwedo\MonitorBundle\Enums\SensorStateEnum; use whatwedo\MonitorBundle\Monitoring\AttributeInterface; -use whatwedo\MonitorBundle\Monitoring\Sensor\AbstractSensor; +use whatwedo\MonitorBundle\Monitoring\Metric\MetricStateInterface; +use whatwedo\MonitorBundle\Monitoring\Sensor\SensorStateInterface; class MonitoringManager { @@ -106,15 +107,27 @@ public function getAttribute(string $className): AttributeInterface private function wasSuccessful(AttributeInterface $attribute): bool { - return $attribute instanceof AbstractSensor - ? $attribute->getState() === SensorStateEnum::SUCCESSFUL - : $attribute->getState() === MetricStateEnum::OK; + if ($attribute instanceof SensorStateInterface) { + return $attribute->getState() === SensorStateEnum::SUCCESSFUL; + } + + if ($attribute instanceof MetricStateInterface) { + return $attribute->getState() === MetricStateEnum::OK; + } + + return false; } - private function wasWarning(AttributeInterface $abstract): bool + private function wasWarning(AttributeInterface $attribute): bool { - return $abstract instanceof AbstractSensor - ? $abstract->getState() === SensorStateEnum::WARNING - : $abstract->getState() === MetricStateEnum::WARNING; + if ($attribute instanceof SensorStateInterface) { + return $attribute->getState() === SensorStateEnum::WARNING; + } + + if ($attribute instanceof MetricStateInterface) { + return $attribute->getState() === MetricStateEnum::WARNING; + } + + return false; } } diff --git a/src/Monitoring/Metric/AbstractMetric.php b/src/Monitoring/Metric/AbstractMetric.php index edcfbb4..ffdb522 100644 --- a/src/Monitoring/Metric/AbstractMetric.php +++ b/src/Monitoring/Metric/AbstractMetric.php @@ -7,7 +7,7 @@ use whatwedo\MonitorBundle\Enums\MetricStateEnum; use whatwedo\MonitorBundle\Monitoring\AttributeInterface; -abstract class AbstractMetric implements AttributeInterface +abstract class AbstractMetric implements AttributeInterface, MetricStateInterface { public null|int|float $value = null; @@ -16,7 +16,7 @@ abstract class AbstractMetric implements AttributeInterface public function getState(): MetricStateEnum { if ($this->state === null) { - throw new \RuntimeException(static::class.'::$state is not set.'); + throw new \LogicException(static::class.'::$state is not set.'); } return $this->state; diff --git a/src/Monitoring/Metric/MetricStateInterface.php b/src/Monitoring/Metric/MetricStateInterface.php new file mode 100644 index 0000000..d326f1e --- /dev/null +++ b/src/Monitoring/Metric/MetricStateInterface.php @@ -0,0 +1,15 @@ +state === null) { - throw new \RuntimeException(__CLASS__.'::$state is not set.'); + throw new \LogicException(static::class.'::$state is not set.'); } return $this->state; diff --git a/src/Monitoring/Sensor/SensorStateInterface.php b/src/Monitoring/Sensor/SensorStateInterface.php new file mode 100644 index 0000000..d067e17 --- /dev/null +++ b/src/Monitoring/Sensor/SensorStateInterface.php @@ -0,0 +1,15 @@ + $object->getName(), ]; + try { + if ($object instanceof MetricStateInterface || $object instanceof SensorStateInterface) { + $data['state'] = $this->normalizer->normalize($object->getState()); + } + } catch (\LogicException) { + } + if ($object instanceof AbstractSensor) { - $data['state'] = $object->getState(); $data['details'] = $object->getDetails(); } if ($object instanceof AbstractMetric) { - $data['state'] = $object->getState(); $data['value'] = $object->getValue(); } diff --git a/tests/Command/CheckCommandTest.php b/tests/Command/CheckCommandTest.php index 5e0df83..2375879 100644 --- a/tests/Command/CheckCommandTest.php +++ b/tests/Command/CheckCommandTest.php @@ -92,24 +92,24 @@ public function testCriticalCustomExitCode(): void self::assertEquals(-1, $commandTester->getStatusCode()); } - public function testRuntimeError(): void + public function testLogicError(): void { $kernel = self::bootKernel([ 'config' => static function (TestKernel $kernel) { - $kernel->addTestConfig(__DIR__.'/../config/dummy_runtime_error.yml'); + $kernel->addTestConfig(__DIR__.'/../config/dummy_logic_error.yml'); }, ]); $application = new Application($kernel); $command = $application->find('whatwedo:monitor:check'); $commandTester = new CommandTester($command); - $runtimeException = false; + $logicException = false; try { $commandTester->execute([]); - } catch (\RuntimeException $e) { - $runtimeException = true; + } catch (\LogicException $e) { + $logicException = true; } finally { - self::assertTrue($runtimeException); + self::assertTrue($logicException); } } } diff --git a/tests/Monitoring/Metric/Dummy/RuntimeErrorDummyMetric.php b/tests/Monitoring/Metric/Dummy/LogicErrorDummyMetric.php similarity index 95% rename from tests/Monitoring/Metric/Dummy/RuntimeErrorDummyMetric.php rename to tests/Monitoring/Metric/Dummy/LogicErrorDummyMetric.php index 62855df..cca4c07 100644 --- a/tests/Monitoring/Metric/Dummy/RuntimeErrorDummyMetric.php +++ b/tests/Monitoring/Metric/Dummy/LogicErrorDummyMetric.php @@ -31,11 +31,11 @@ use whatwedo\MonitorBundle\Monitoring\Metric\AbstractMetric; -class RuntimeErrorDummyMetric extends AbstractMetric +class LogicErrorDummyMetric extends AbstractMetric { public function getName(): string { - return 'Runtime Error Test'; + return 'Logic Error Test'; } public function isEnabled(): bool diff --git a/tests/Monitoring/Sensor/Dummy/RuntimeErrorDummySensor.php b/tests/Monitoring/Sensor/Dummy/LogicErrorDummySensor.php similarity index 95% rename from tests/Monitoring/Sensor/Dummy/RuntimeErrorDummySensor.php rename to tests/Monitoring/Sensor/Dummy/LogicErrorDummySensor.php index 5a68b1e..275113d 100644 --- a/tests/Monitoring/Sensor/Dummy/RuntimeErrorDummySensor.php +++ b/tests/Monitoring/Sensor/Dummy/LogicErrorDummySensor.php @@ -31,11 +31,11 @@ use whatwedo\MonitorBundle\Monitoring\Sensor\AbstractSensor; -class RuntimeErrorDummySensor extends AbstractSensor +class LogicErrorDummySensor extends AbstractSensor { public function getName(): string { - return 'Runtime Error Test'; + return 'Logic Error Test'; } public function isEnabled(): bool diff --git a/tests/Normalizer/AttributeNormalizerTest.php b/tests/Normalizer/AttributeNormalizerTest.php new file mode 100644 index 0000000..65e46b7 --- /dev/null +++ b/tests/Normalizer/AttributeNormalizerTest.php @@ -0,0 +1,106 @@ + [ + 'attribute' => new CriticalDummyMetric(), + 'expectedNormalizedBody' => [ + 'name' => 'Critical Test', + 'state' => 'critical', + 'value' => 24, + ], + ], + OkDummyMetric::class => [ + 'attribute' => new OkDummyMetric(), + 'expectedNormalizedBody' => [ + 'name' => 'Ok Test', + 'state' => 'ok', + 'value' => 80, + ], + ], + WarningDummyMetric::class => [ + 'attribute' => new WarningDummyMetric(), + 'expectedNormalizedBody' => [ + 'name' => 'Warning Test', + 'state' => 'warning', + 'value' => 125, + ], + ], + + CriticalDummySensor::class => [ + 'attribute' => new CriticalDummySensor(), + 'expectedNormalizedBody' => [ + 'name' => 'Critical Test', + 'state' => 'critical', + 'details' => [], + ], + ], + SuccessfulDummySensor::class => [ + 'attribute' => new SuccessfulDummySensor(), + 'expectedNormalizedBody' => [ + 'name' => 'Successful Test', + 'state' => 'successful', + 'details' => [], + ], + ], + WarningDummySensor::class => [ + 'attribute' => new WarningDummySensor(), + 'expectedNormalizedBody' => [ + 'name' => 'Warning Test', + 'state' => 'warning', + 'details' => [], + ], + ], + + LogicErrorDummyMetric::class => [ + 'attribute' => new LogicErrorDummyMetric(), + 'expectedNormalizedBody' => [ + 'name' => 'Logic Error Test', + 'value' => null, + ], + ], + LogicErrorDummySensor::class => [ + 'attribute' => new LogicErrorDummySensor(), + 'expectedNormalizedBody' => [ + 'name' => 'Logic Error Test', + 'details' => [], + ], + ], + ]; + } + + /** + * @dataProvider provideDummyMetricsAndSensors + */ + public function testNormalizeWorksAsExpected(AttributeInterface $attribute, array $expectedNormalizedBody): void + { + $attribute->run(); + + self::assertSame( + $expectedNormalizedBody, + $this->getContainer()->get(NormalizerInterface::class)->normalize($attribute) + ); + } +} diff --git a/tests/config/dummy_logic_error.yml b/tests/config/dummy_logic_error.yml new file mode 100644 index 0000000..344d842 --- /dev/null +++ b/tests/config/dummy_logic_error.yml @@ -0,0 +1,6 @@ +services: + whatwedo\MonitorBundle\Tests\Monitoring\Sensor\Dummy\LogicErrorDummySensor: + tags: [ 'whatwedo_monitor.attribute' ] + + whatwedo\MonitorBundle\Tests\Monitoring\Metric\Dummy\LogicErrorDummyMetric: + tags: [ 'whatwedo_monitor.attribute' ] diff --git a/tests/config/dummy_runtime_error.yml b/tests/config/dummy_runtime_error.yml deleted file mode 100644 index 4c58ac7..0000000 --- a/tests/config/dummy_runtime_error.yml +++ /dev/null @@ -1,6 +0,0 @@ -services: - whatwedo\MonitorBundle\Tests\Monitoring\Sensor\Dummy\RuntimeErrorDummySensor: - tags: [ 'whatwedo_monitor.attribute' ] - - whatwedo\MonitorBundle\Tests\Monitoring\Metric\Dummy\RuntimeErrorDummyMetric: - tags: [ 'whatwedo_monitor.attribute' ]