Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
otsch committed Jan 31, 2024
1 parent 8e74eb5 commit 412d71f
Show file tree
Hide file tree
Showing 2 changed files with 253 additions and 0 deletions.
102 changes: 102 additions & 0 deletions tests/ConfigParamTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

namespace Tests;

use Crwlr\CrwlExtensionUtils\ConfigParam;
use Crwlr\CrwlExtensionUtils\ConfigParamTypes;

it('makes a bool config param instance', function () {
$configParam = ConfigParam::bool('foo');

expect($configParam->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',
]);
});
151 changes: 151 additions & 0 deletions tests/StepBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<int, ConfigParam>
*/
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',
],
]);
});

0 comments on commit 412d71f

Please sign in to comment.