From efc55a4b3d555d4608bd320ee9087455f91c818c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Fejfar?= Date: Mon, 18 Mar 2019 14:10:05 +0100 Subject: [PATCH 1/2] Make "run" default action if missing in config --- src/Config/BaseConfig.php | 2 +- tests/BaseComponentTest.php | 23 +++++++++++++++++++ tests/Config/BaseConfigTest.php | 2 +- .../empty-config-file/config.json | 2 ++ 4 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 tests/fixtures/base-component-data-dir/empty-config-file/config.json diff --git a/src/Config/BaseConfig.php b/src/Config/BaseConfig.php index 4e69df3a..4b351638 100644 --- a/src/Config/BaseConfig.php +++ b/src/Config/BaseConfig.php @@ -136,7 +136,7 @@ public function getAuthorization(): array */ public function getAction(): string { - return $this->getValue(['action'], ''); + return $this->getValue(['action'], 'run'); } /** diff --git a/tests/BaseComponentTest.php b/tests/BaseComponentTest.php index da0cc3d3..6e000828 100644 --- a/tests/BaseComponentTest.php +++ b/tests/BaseComponentTest.php @@ -102,6 +102,29 @@ protected function run(): void $this->assertTrue($handler->hasAlert('Log message from run')); } + public function testWillNotFailWithEmptyConfigAction(): void + { + $logger = $this->getLogger(); + $handler = new TestHandler(); + $logger->setHandlers([$handler]); + putenv(sprintf( + 'KBC_DATADIR=%s', + __DIR__ . '/fixtures/base-component-data-dir/empty-config-file' + )); + $baseComponent = new class ($logger) extends BaseComponent + { + protected function run(): void + { + echo 'Shitty output'; + $this->getLogger()->alert('Log message from run'); + } + }; + $this->expectOutputString('Shitty output'); + $baseComponent->execute(); + + $this->assertTrue($handler->hasAlert('Log message from run')); + } + public function testLoadInputStateFileEmptyThrowsException(): void { putenv(sprintf( diff --git a/tests/Config/BaseConfigTest.php b/tests/Config/BaseConfigTest.php index 12dd2820..455aa6db 100644 --- a/tests/Config/BaseConfigTest.php +++ b/tests/Config/BaseConfigTest.php @@ -108,7 +108,7 @@ public function testGettersWillNotFailIfKeyIsMissing(): void ], ]); $this->assertSame([], $config->getParameters()); - $this->assertSame('', $config->getAction()); + $this->assertSame('run', $config->getAction()); $this->assertSame([], $config->getAuthorization()); $this->assertSame('', $config->getOAuthApiAppKey()); $this->assertSame('', $config->getOAuthApiAppSecret()); diff --git a/tests/fixtures/base-component-data-dir/empty-config-file/config.json b/tests/fixtures/base-component-data-dir/empty-config-file/config.json new file mode 100644 index 00000000..2c63c085 --- /dev/null +++ b/tests/fixtures/base-component-data-dir/empty-config-file/config.json @@ -0,0 +1,2 @@ +{ +} From 45518801be39314ea6bf4f8be8281954357d2306 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Fejfar?= Date: Tue, 19 Mar 2019 10:32:24 +0100 Subject: [PATCH 2/2] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e63d55a4..1e3ad437 100644 --- a/README.md +++ b/README.md @@ -179,7 +179,7 @@ If any constraint of config definition is not met a `UserException` is thrown. T ## Migration from version 6 to version 7 -The default entrypoint of component (in `index.php`) changed from `BaseComponent::run()` to `BaseComponent::execute()`. While running the component via `run` method is still supported, you need to use `execute()` if you want to take advantage of sync action support. +The default entrypoint of component (in `index.php`) changed from `BaseComponent::run()` to `BaseComponent::execute()`. Please also note, that the `run` method can no longer be public and can only be called from inside the component now. ## More reading