Skip to content

Commit

Permalink
Enforce using DSL in test suite (#1341)
Browse files Browse the repository at this point in the history
* Configure rector to enforce DSL in test suite

* Fixed importing names

* Added more rules to recto test config

* Moved whole testsuite to use DSL instead of object oriented approach

* Added rector to static analysis script

* CS Fixes

* Fixed static analyze workflow

* Fixed static analyze workflow
  • Loading branch information
norberttech authored Jan 7, 2025
1 parent c083a21 commit 1eeb7c2
Show file tree
Hide file tree
Showing 188 changed files with 1,897 additions and 2,652 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/static-analyze.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,6 @@ jobs:

- name: "Static Analyze - PHPStan"
run: "composer static:analyze:phpstan -- --error-format=github"

- name: "Static Analyze - Rector"
run: "composer static:analyze:rector"
3 changes: 3 additions & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
__DIR__ . '/src/adapter/**/tests',
__DIR__ . '/src/lib/**/src',
__DIR__ . '/src/lib/**/tests',
__DIR__ . '/src/tools/**/src',
__DIR__ . '/src/tools/**/tests',
__DIR__ . '/web/**/src',
__DIR__ . '/web/**/tests',
__DIR__ . '/examples',
__DIR__ . '/tools/rector/src',
]);

if (!\file_exists(__DIR__ . '/var')) {
Expand Down
13 changes: 10 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -372,11 +372,16 @@
"test:monorepo": "tools/monorepo/vendor/bin/monorepo-builder validate",
"static:analyze": [
"@static:analyze:cs-fixer",
"@static:analyze:phpstan"
"@static:analyze:phpstan",
"@static:analyze:rector"
],
"static:analyze:phpstan": [
"tools/phpstan/vendor/bin/phpstan analyze -c phpstan.neon --memory-limit=-1"
],
"static:analyze:rector": [
"tools/rector/vendor/bin/rector -c ./rector.tests.php --dry-run",
"tools/rector/vendor/bin/rector -c ./rector.src.php --dry-run"
],
"static:analyze:cs-fixer": [
"tools/cs-fixer/vendor/bin/php-cs-fixer fix --dry-run"
],
Expand Down Expand Up @@ -413,7 +418,8 @@
"composer install --working-dir=./tools/monorepo",
"composer install --working-dir=./tools/phpbench",
"composer install --working-dir=./tools/phpstan",
"composer install --working-dir=./tools/phpunit"
"composer install --working-dir=./tools/phpunit",
"composer install --working-dir=./tools/rector"
],
"tools:update": [
"composer update --working-dir=./tools/blackfire",
Expand All @@ -423,7 +429,8 @@
"composer update --working-dir=./tools/monorepo",
"composer update --working-dir=./tools/phpbench",
"composer update --working-dir=./tools/phpstan",
"composer update --working-dir=./tools/phpunit"
"composer update --working-dir=./tools/phpunit",
"composer update --working-dir=./tools/rector"
]
}
}
1 change: 1 addition & 0 deletions rector.src.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
__DIR__ . '/src/bridge/*/*/src',
__DIR__ . '/src/tools/*/*/src',
])
->withCache(__DIR__ . '/var/rector/src')
->withSets([
LevelSetList::UP_TO_PHP_82
]);
174 changes: 173 additions & 1 deletion rector.tests.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,56 @@
<?php

use Flow\ETL\Config;
use Flow\ETL\FlowContext;
use Flow\ETL\PHP\Type\Logical\DateTimeType;
use Flow\ETL\PHP\Type\Logical\DateType;
use Flow\ETL\PHP\Type\Logical\JsonType;
use Flow\ETL\PHP\Type\Logical\List\ListElement;
use Flow\ETL\PHP\Type\Logical\ListType;
use Flow\ETL\PHP\Type\Logical\Map\MapKey;
use Flow\ETL\PHP\Type\Logical\Map\MapValue;
use Flow\ETL\PHP\Type\Logical\MapType;
use Flow\ETL\PHP\Type\Logical\Structure\StructureElement;
use Flow\ETL\PHP\Type\Logical\StructureType;
use Flow\ETL\PHP\Type\Logical\TimeType;
use Flow\ETL\PHP\Type\Logical\UuidType;
use Flow\ETL\PHP\Type\Logical\XMLElementType;
use Flow\ETL\PHP\Type\Logical\XMLType;
use Flow\ETL\PHP\Type\Native\ArrayType;
use Flow\ETL\PHP\Type\Native\BooleanType;
use Flow\ETL\PHP\Type\Native\CallableType;
use Flow\ETL\PHP\Type\Native\EnumType;
use Flow\ETL\PHP\Type\Native\FloatType;
use Flow\ETL\PHP\Type\Native\IntegerType;
use Flow\ETL\PHP\Type\Native\NullType;
use Flow\ETL\PHP\Type\Native\ObjectType;
use Flow\ETL\PHP\Type\Native\ResourceType;
use Flow\ETL\PHP\Type\Native\StringType;
use Flow\ETL\Row;
use Flow\ETL\Row\Entry\BooleanEntry;
use Flow\ETL\Row\Entry\DateEntry;
use Flow\ETL\Row\Entry\DatetimeEntry;
use Flow\ETL\Row\Entry\EnumEntry;
use Flow\ETL\Row\Entry\FloatEntry;
use Flow\ETL\Row\Entry\IntegerEntry;
use Flow\ETL\Row\Entry\JsonEntry;
use Flow\ETL\Row\Entry\ListEntry;
use Flow\ETL\Row\Entry\MapEntry;
use Flow\ETL\Row\Entry\StringEntry;
use Flow\ETL\Row\Entry\StructureEntry;
use Flow\ETL\Row\Entry\TimeEntry;
use Flow\ETL\Row\Entry\UuidEntry;
use Flow\ETL\Row\Entry\XMLElementEntry;
use Flow\ETL\Row\Entry\XMLEntry;
use Flow\ETL\Row\Schema\Definition;
use Flow\ETL\Rows;
use Flow\Tools\Rector\NewObjectToFunction;
use Flow\Tools\Rector\NewToFunctionCallRector;
use Rector\Config\RectorConfig;
use Rector\DeadCode\Rector\StaticCall\RemoveParentCallWithoutParentRector;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Transform\Rector\StaticCall\StaticCallToFuncCallRector;
use \Rector\Transform\ValueObject\StaticCallToFuncCall;

return RectorConfig::configure()
->withPaths([
Expand All @@ -15,6 +63,130 @@
->withSets([
LevelSetList::UP_TO_PHP_82
])
->withConfiguredRule(
StaticCallToFuncCallRector::class,
[
// Building Blocks
new StaticCallToFuncCall(Flow\ETL\Row::class, 'create', 'Flow\ETL\DSL\row'),
new StaticCallToFuncCall(Config::class, 'default', 'Flow\ETL\DSL\config'),
// Schema
new StaticCallToFuncCall(Definition::class, 'boolean', 'Flow\ETL\DSL\bool_schema'),
new StaticCallToFuncCall(Definition::class, 'date', 'Flow\ETL\DSL\date_schema'),
new StaticCallToFuncCall(Definition::class, 'datetime', 'Flow\ETL\DSL\datetime_schema'),
new StaticCallToFuncCall(Definition::class, 'enum', 'Flow\ETL\DSL\enum_schema'),
new StaticCallToFuncCall(Definition::class, 'float', 'Flow\ETL\DSL\float_schema'),
new StaticCallToFuncCall(Definition::class, 'integer', 'Flow\ETL\DSL\integer_schema'),
new StaticCallToFuncCall(Definition::class, 'json', 'Flow\ETL\DSL\json_schema'),
new StaticCallToFuncCall(Definition::class, 'list', 'Flow\ETL\DSL\list_schema'),
new StaticCallToFuncCall(Definition::class, 'map', 'Flow\ETL\DSL\map_schema'),
new StaticCallToFuncCall(Definition::class, 'string', 'Flow\ETL\DSL\string_schema'),
new StaticCallToFuncCall(Definition::class, 'structure', 'Flow\ETL\DSL\structure_schema'),
new StaticCallToFuncCall(Definition::class, 'time', 'Flow\ETL\DSL\time_schema'),
new StaticCallToFuncCall(Definition::class, 'uuid', 'Flow\ETL\DSL\uuid_schema'),
new StaticCallToFuncCall(Definition::class, 'xml', 'Flow\ETL\DSL\xml_schema'),
new StaticCallToFuncCall(Definition::class, 'xml_element', 'Flow\ETL\DSL\xml_element_schema'),
// Logical Types
new StaticCallToFuncCall(MapKey::class, 'integer', 'Flow\ETL\DSL\type_integer'),
new StaticCallToFuncCall(MapKey::class, 'string', 'Flow\ETL\DSL\type_string'),
new StaticCallToFuncCall(MapValue::class, 'boolean', 'Flow\ETL\DSL\type_boolean'),
new StaticCallToFuncCall(MapValue::class, 'datetime', 'Flow\ETL\DSL\type_datetime'),
new StaticCallToFuncCall(MapValue::class, 'float', 'Flow\ETL\DSL\type_float'),
new StaticCallToFuncCall(MapValue::class, 'integer', 'Flow\ETL\DSL\type_integer'),
new StaticCallToFuncCall(MapValue::class, 'json', 'Flow\ETL\DSL\type_json'),
new StaticCallToFuncCall(MapValue::class, 'list', 'Flow\ETL\DSL\type_list'),
new StaticCallToFuncCall(MapValue::class, 'map', 'Flow\ETL\DSL\type_map'),
new StaticCallToFuncCall(MapValue::class, 'object', 'Flow\ETL\DSL\type_object'),
new StaticCallToFuncCall(MapValue::class, 'string', 'Flow\ETL\DSL\type_string'),
new StaticCallToFuncCall(MapValue::class, 'structure', 'Flow\ETL\DSL\type_structure'),
new StaticCallToFuncCall(MapValue::class, 'uuid', 'Flow\ETL\DSL\type_uuid'),
new StaticCallToFuncCall(MapValue::class, 'xml', 'Flow\ETL\DSL\type_xml'),
new StaticCallToFuncCall(MapValue::class, 'xmlElement', 'Flow\ETL\DSL\type_xml_element'),

new StaticCallToFuncCall(ListElement::class, 'boolean', 'Flow\ETL\DSL\type_boolean'),
new StaticCallToFuncCall(ListElement::class, 'datetime', 'Flow\ETL\DSL\type_datetime'),
new StaticCallToFuncCall(ListElement::class, 'float', 'Flow\ETL\DSL\type_float'),
new StaticCallToFuncCall(ListElement::class, 'integer', 'Flow\ETL\DSL\type_integer'),
new StaticCallToFuncCall(ListElement::class, 'json', 'Flow\ETL\DSL\type_json'),
new StaticCallToFuncCall(ListElement::class, 'list', 'Flow\ETL\DSL\type_list'),
new StaticCallToFuncCall(ListElement::class, 'map', 'Flow\ETL\DSL\type_map'),
new StaticCallToFuncCall(ListElement::class, 'object', 'Flow\ETL\DSL\type_object'),
new StaticCallToFuncCall(ListElement::class, 'string', 'Flow\ETL\DSL\type_string'),
new StaticCallToFuncCall(ListElement::class, 'structure', 'Flow\ETL\DSL\type_structure'),
new StaticCallToFuncCall(ListElement::class, 'uuid', 'Flow\ETL\DSL\type_uuid'),
new StaticCallToFuncCall(ListElement::class, 'xml', 'Flow\ETL\DSL\type_xml'),
new StaticCallToFuncCall(ListElement::class, 'xmlElement', 'Flow\ETL\DSL\type_xml_element'),
]
)
->withConfiguredRule(
NewToFunctionCallRector::class,
[
// Building Blocks
new NewObjectToFunction(Rows::class, 'Flow\ETL\DSL\rows'),
new NewObjectToFunction(Config::class, 'Flow\ETL\DSL\config'),
new NewObjectToFunction(FlowContext::class, 'Flow\ETL\DSL\flow_context'),
new NewObjectToFunction(Flow\ETL\Row\Schema::class, 'Flow\ETL\DSL\schema'),

// Entries
new NewObjectToFunction(BooleanEntry::class, 'Flow\ETL\DSL\boolean_entry'),
new NewObjectToFunction(DateEntry::class, 'Flow\ETL\DSL\date_entry'),
new NewObjectToFunction(DatetimeEntry::class, 'Flow\ETL\DSL\datetime_entry'),
new NewObjectToFunction(EnumEntry::class, 'Flow\ETL\DSL\enum_entry'),
new NewObjectToFunction(FloatEntry::class, 'Flow\ETL\DSL\float_entry'),
new NewObjectToFunction(IntegerEntry::class, 'Flow\ETL\DSL\integer_entry'),
new NewObjectToFunction(JsonEntry::class, 'Flow\ETL\DSL\json_entry'),
new NewObjectToFunction(ListEntry::class, 'Flow\ETL\DSL\list_entry'),
new NewObjectToFunction(MapEntry::class, 'Flow\ETL\DSL\map_entry'),
new NewObjectToFunction(StringEntry::class, 'Flow\ETL\DSL\string_entry'),
new NewObjectToFunction(StructureEntry::class, 'Flow\ETL\DSL\structure_entry'),
new NewObjectToFunction(TimeEntry::class, 'Flow\ETL\DSL\time_entry'),
new NewObjectToFunction(UuidEntry::class, 'Flow\ETL\DSL\uuid_entry'),
new NewObjectToFunction(XMLElementEntry::class, 'Flow\ETL\DSL\xml_element_entry'),
new NewObjectToFunction(XMLEntry::class, 'Flow\ETL\DSL\xml_entry'),

// Native Types
new NewObjectToFunction(ArrayType::class, 'Flow\ETL\DSL\type_array'),
new NewObjectToFunction(BooleanType::class, 'Flow\ETL\DSL\type_boolean'),
new NewObjectToFunction(CallableType::class, 'Flow\ETL\DSL\type_callable'),
new NewObjectToFunction(EnumType::class, 'Flow\ETL\DSL\type_enum'),
new NewObjectToFunction(FloatType::class, 'Flow\ETL\DSL\type_float'),
new NewObjectToFunction(IntegerType::class, 'Flow\ETL\DSL\type_integer'),
new NewObjectToFunction(NullType::class, 'Flow\ETL\DSL\type_null'),
new NewObjectToFunction(ObjectType::class, 'Flow\ETL\DSL\type_object'),
new NewObjectToFunction(ResourceType::class, 'Flow\ETL\DSL\type_resource'),
new NewObjectToFunction(StringType::class, 'Flow\ETL\DSL\type_string'),

// Logical Types
new NewObjectToFunction(DateTimeType::class, 'Flow\ETL\DSL\type_datetime'),
new NewObjectToFunction(DateType::class, 'Flow\ETL\DSL\type_date'),
new NewObjectToFunction(JsonType::class, 'Flow\ETL\DSL\type_json'),
new NewObjectToFunction(ListType::class, 'Flow\ETL\DSL\type_list'),
new NewObjectToFunction(MapType::class, 'Flow\ETL\DSL\type_map'),
new NewObjectToFunction(StructureType::class, 'Flow\ETL\DSL\type_structure'),
new NewObjectToFunction(TimeType::class, 'Flow\ETL\DSL\type_time'),
new NewObjectToFunction(UuidType::class, 'Flow\ETL\DSL\type_uuid'),
new NewObjectToFunction(XMLElementType::class, 'Flow\ETL\DSL\type_xml_element'),
new NewObjectToFunction(XMLType::class, 'Flow\ETL\DSL\type_xml'),
new NewObjectToFunction(StructureElement::class, 'Flow\ETL\DSL\structure_element'),

// Extractors
new NewObjectToFunction(Flow\ETL\Extractor\CacheExtractor::class, 'from_cache'),
new NewObjectToFunction(Flow\ETL\Extractor\RowsExtractor::class, 'from_rows'),
new NewObjectToFunction(Flow\ETL\Extractor\ArrayExtractor::class, 'from_array'),
new NewObjectToFunction(Flow\ETL\Extractor\ChainExtractor::class, 'from_all'),
new NewObjectToFunction(Flow\ETL\Extractor\MemoryExtractor::class, 'from_memory'),
new NewObjectToFunction(Flow\ETL\Extractor\ChunkExtractor::class, 'chunks_from'),
new NewObjectToFunction(Flow\ETL\Extractor\PipelineExtractor::class, 'from_pipeline'),
new NewObjectToFunction(Flow\ETL\Extractor\DataFrameExtractor::class, 'from_data_frame'),

]
)
->withSkip([
RemoveParentCallWithoutParentRector::class
]);
])
->withCache(__DIR__ . '/var/rector/tests')
->withImportNames(
importNames: true,
importDocBlockNames: true,
importShortClasses: false,
removeUnusedImports: true
);
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
namespace Flow\ETL\Adapter\Avro\Tests\Integration;

use function Flow\ETL\DSL\Adapter\Avro\{to_avro};
use function Flow\ETL\DSL\{config, flow_context};
use Flow\ETL\Adapter\Avro\FlixTech\AvroExtractor;
use Flow\ETL\Extractor\Signal;
use Flow\ETL\{Config, FlowContext, Tests\FlowTestCase};
use Flow\ETL\{Tests\FlowTestCase};
use Flow\Filesystem\Path;

final class AvroTest extends FlowTestCase
Expand All @@ -24,15 +25,15 @@ public function test_limit() : void

self::assertCount(
2,
\iterator_to_array($extractor->extract(new FlowContext(Config::default())))
\iterator_to_array($extractor->extract(flow_context(config())))
);
}

public function test_signal_stop() : void
{
$extractor = new AvroExtractor(Path::realpath(__DIR__ . '/../Fixtures/orders_flow.avro'));

$generator = $extractor->extract(new FlowContext(Config::default()));
$generator = $extractor->extract(flow_context(config()));

self::assertTrue($generator->valid());
$generator->next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
namespace Flow\ETL\Adapter\CSV\Tests\Benchmark;

use function Flow\ETL\Adapter\CSV\from_csv;
use Flow\ETL\{Config, FlowContext};
use function Flow\ETL\DSL\{config, flow_context};
use Flow\ETL\{FlowContext};
use PhpBench\Attributes\Groups;

#[Groups(['extractor'])]
Expand All @@ -15,7 +16,7 @@

public function __construct()
{
$this->context = new FlowContext(Config::default());
$this->context = flow_context(config());
}

public function bench_extract_10k() : void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
namespace Flow\ETL\Adapter\CSV\Tests\Benchmark;

use function Flow\ETL\Adapter\CSV\{from_csv, to_csv};
use Flow\ETL\{Config, FlowContext, Rows};
use function Flow\ETL\DSL\{config, flow_context};
use Flow\ETL\{FlowContext, Rows};
use PhpBench\Attributes\Groups;

#[Groups(['loader'])]
Expand All @@ -19,9 +20,9 @@ final class CSVLoaderBench

public function __construct()
{
$this->context = new FlowContext(Config::default());
$this->context = flow_context(config());
$this->outputPath = \tempnam(\sys_get_temp_dir(), 'etl_csv_loader_bench') . '.csv';
$this->rows = new Rows();
$this->rows = \Flow\ETL\DSL\rows();

foreach (from_csv(__DIR__ . '/Fixtures/orders_flow.csv')->extract($this->context) as $rows) {
$this->rows = $this->rows->merge($rows);
Expand Down
Loading

0 comments on commit 1eeb7c2

Please sign in to comment.