From 412d71f7a80a36119d648c0ffb9c93f14068cbf5 Mon Sep 17 00:00:00 2001 From: otsch Date: Wed, 31 Jan 2024 20:44:11 +0100 Subject: [PATCH] Add more tests --- tests/ConfigParamTest.php | 102 +++++++++++++++++++++++++ tests/StepBuilderTest.php | 151 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 253 insertions(+) create mode 100644 tests/ConfigParamTest.php diff --git a/tests/ConfigParamTest.php b/tests/ConfigParamTest.php new file mode 100644 index 0000000..8c21c66 --- /dev/null +++ b/tests/ConfigParamTest.php @@ -0,0 +1,102 @@ +type) + ->toBe(ConfigParamTypes::Bool) + ->and($configParam->name) + ->toBe('foo') + ->and($configParam->value) + ->toBeFalse(); +}); + +it('makes a string config param instance', function () { + $configParam = ConfigParam::string('foo'); + + expect($configParam->type) + ->toBe(ConfigParamTypes::String) + ->and($configParam->name) + ->toBe('foo') + ->and($configParam->value) + ->toBe(''); +}); + +it('makes an int config param instance', function () { + $configParam = ConfigParam::int('foo'); + + expect($configParam->type) + ->toBe(ConfigParamTypes::Int) + ->and($configParam->name) + ->toBe('foo') + ->and($configParam->value) + ->toBe(0); +}); + +it('makes a new instance with a default value', function () { + $instance = new ConfigParam( + ConfigParamTypes::Int, + 'foo', + 0, + 'foo input label', + 'foo description', + ); + + $instance2 = $instance->default(5); + + expect($instance2->toArray()) + ->toBe([ + 'type' => 'Int', + 'name' => 'foo', + 'value' => 5, + 'inputLabel' => 'foo input label', + 'description' => 'foo description', + ]); +}); + +it('makes a new instance with an input label', function () { + $instance = new ConfigParam( + ConfigParamTypes::String, + 'foo', + '', + '', + 'foo description', + ); + + $instance2 = $instance->inputLabel('label label'); + + expect($instance2->toArray()) + ->toBe([ + 'type' => 'String', + 'name' => 'foo', + 'value' => '', + 'inputLabel' => 'label label', + 'description' => 'foo description', + ]); +}); + +it('makes a new instance with a description', function () { + $instance = new ConfigParam( + ConfigParamTypes::Bool, + 'foo', + true, + 'foo input label', + '', + ); + + $instance2 = $instance->description('this is a bool config param'); + + expect($instance2->toArray()) + ->toBe([ + 'type' => 'Bool', + 'name' => 'foo', + 'value' => true, + 'inputLabel' => 'foo input label', + 'description' => 'this is a bool config param', + ]); +}); diff --git a/tests/StepBuilderTest.php b/tests/StepBuilderTest.php index 81dfbf1..262f622 100644 --- a/tests/StepBuilderTest.php +++ b/tests/StepBuilderTest.php @@ -2,8 +2,11 @@ namespace Tests; +use Crwlr\Crawler\Input; +use Crwlr\Crawler\Output; use Crwlr\Crawler\Steps\Step; use Crwlr\Crawler\Steps\StepInterface; +use Crwlr\CrwlExtensionUtils\ConfigParam; use Crwlr\CrwlExtensionUtils\Exceptions\InvalidStepBuilderException; use Crwlr\CrwlExtensionUtils\StepBuilder; use Generator; @@ -31,3 +34,151 @@ protected function invoke(mixed $input): Generator } }; })->throws(InvalidStepBuilderException::class); + +it('correctly gets group when stepId contains a "."', function () { + $builder = new class () extends StepBuilder { + public function stepId(): string + { + return 'foo.bar'; + } + + public function label(): string + { + return 'This is the foo bar step'; + } + + public function configToStep(array $stepConfig): StepInterface + { + return new class () extends Step { + protected function invoke(mixed $input): Generator + { + yield 'yo'; + } + }; + } + }; + + expect($builder->stepId()) + ->toBe('foo.bar') + ->and($builder->group()) + ->toBe('foo'); +}); + +it('gets a value from a step config array', function () { + $builder = new class () extends StepBuilder { + public function stepId(): string + { + return 'test.foo'; + } + + public function label(): string + { + return 'This is a demo step.'; + } + + public function configToStep(array $stepConfig): StepInterface + { + $valueFromConfigArray = $this->getValueFromConfigArray('someKey', $stepConfig); + + return new class ($valueFromConfigArray) extends Step { + public function __construct(private readonly string $value) + { + } + + protected function invoke(mixed $input): Generator + { + yield $this->value; + } + }; + } + }; + + $stepConfig = [ + [ + 'name' => 'someKey', + 'type' => 'String', + 'value' => 'super-duper value', + 'inputLabel' => 'Some Key:', + 'description' => null, + ], + ]; + + $step = $builder->configToStep($stepConfig); + + $results = iterator_to_array($step->invokeStep(new Input('anything'))); + + expect($results) + ->toHaveCount(1) + ->and($results[0]) + ->toBeInstanceOf(Output::class) + ->and($results[0]->get()) + ->toBe('super-duper value'); +}); + +it('turns the config params into an array', function () { + $builder = new class () extends StepBuilder { + public function stepId(): string + { + return 'test.bar'; + } + + public function label(): string + { + return 'This is another demo step.'; + } + + public function configToStep(array $stepConfig): StepInterface + { + return new class () extends Step { + protected function invoke(mixed $input): Generator + { + yield 'foo'; + } + }; + } + + /** + * @return array + */ + public function configParams(): array + { + return [ + ConfigParam::string('foo') + ->inputLabel('foo input label') + ->description('foo description'), + + ConfigParam::int('number') + ->inputLabel('number input label') + ->description('number description'), + + ConfigParam::bool('someBool') + ->inputLabel('Input label for some bool') + ->description('This is a boolean config param') + ]; + } + }; + + expect($builder->config)->toBe([ + [ + 'type' => 'String', + 'name' => 'foo', + 'value' => '', + 'inputLabel' => 'foo input label', + 'description' => 'foo description', + ], + [ + 'type' => 'Int', + 'name' => 'number', + 'value' => 0, + 'inputLabel' => 'number input label', + 'description' => 'number description', + ], + [ + 'type' => 'Bool', + 'name' => 'someBool', + 'value' => false, + 'inputLabel' => 'Input label for some bool', + 'description' => 'This is a boolean config param', + ], + ]); +});