Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fab2s committed Sep 18, 2022
1 parent 90ece24 commit 8c30f50
Show file tree
Hide file tree
Showing 23 changed files with 533 additions and 68 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/qa.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
name: QA
on: [pull_request]
on:
push:
branches:
- master
pull_request:
branches:
- *
jobs:
tests:
name: YaEtl QA
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
],
"require": {
"php": "^7.2|^8.0",
"fab2s/nodalflow": "^2.0",
"fab2s/nodalflow": "dev-add_tests",
"fab2s/opinhelpers": "^1.0"
},
"require-dev": {
Expand Down Expand Up @@ -72,6 +72,7 @@
],
"post-install-cmd": [
"rm -f .*.cache"
]
],
"fix": "php-cs-fixer fix --config=./.php-cs-fixer.dist.php -vvv"
}
}
18 changes: 14 additions & 4 deletions src/Extractors/File/FileExtractorAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,17 @@ protected function readBom(): bool
*/
protected function getNextNonEmptyLine(): ?string
{
while (false !== ($line = fgets($this->handle))) {
do {
if (false === ($line = fgets($this->handle))) {
return null;
}

if ('' === ($line = trim($line))) {
continue;
}

return $line;
}
} while (!feof($this->handle));

return null;
}
Expand All @@ -89,8 +93,14 @@ protected function getNextNonEmptyChars(): ? string
if (false === ($char = fread($this->handle, 1))) {
return null;
}
} while (trim($char) === '');

return $char ?? null;
if ('' === ($char = trim($char))) {
continue;
}

return $char;
} while (!feof($this->handle));

return null;
}
}
6 changes: 2 additions & 4 deletions src/Extractors/NullExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class NullExtractor extends ExtractorBatchLimitAbstract
*
* @var int
*/
protected $limit = 5000;
protected $limit = 50;

/**
* Triggers an extract
Expand All @@ -40,11 +40,9 @@ public function extract($param = null): bool
/**
* Return the dumbest Generator ever
*
* @param mixed $param
*
* @return \Generator
*/
public function getTraversable($param = null): iterable
public function getExtracted(): iterable
{
yield null;
}
Expand Down
83 changes: 83 additions & 0 deletions src/Qualifiers/LimitQualifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

/*
* This file is part of YaEtl
* (c) Fabrice de Stefanis / https://github.com/fab2s/YaEtl
* This source file is licensed under the MIT license which you will
* find in the LICENSE file or at https://opensource.org/licenses/MIT
*/

namespace fab2s\YaEtl\Qualifiers;

use fab2s\NodalFlow\Flows\InterrupterInterface;
use fab2s\NodalFlow\Interrupter;

/**
* Class LimitQualifier
*/
class LimitQualifier extends QualifierAbstract
{
/**
* @var int|null
*/
protected $limit;

/**
* @var int
*/
protected $count = 0;

/**
* @var string
*/
protected $target;

public function __construct(?int $limit = null, string $target = InterrupterInterface::TARGET_SELF)
{
parent::__construct();

$this->setLimit($limit)
->setTarget($target);
}

/**
* @param int|null $limit
*
* @return LimitQualifier
*/
public function setLimit(?int $limit): LimitQualifier
{
$this->limit = $limit;

return $this;
}

/**
* @return string
*/
public function getTarget(): string
{
return $this->target;
}

/**
* @param string $target
*
* @return LimitQualifier
*/
public function setTarget(string $target): LimitQualifier
{
$this->target = $target === InterrupterInterface::TARGET_TOP ? InterrupterInterface::TARGET_TOP : InterrupterInterface::TARGET_SELF;

return $this;
}

public function qualify($param)
{
if ($this->limit && ++$this->count >= $this->limit + 1) {
return new Interrupter($this->getTarget(), null, InterrupterInterface::TYPE_BREAK);
}

return true;
}
}
2 changes: 1 addition & 1 deletion src/Qualifiers/QualifierAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,6 @@ public function exec($param = null)
return;
}

throw new YaEtlException('Qualifier returned wrong type, only Boolean and InterrupterInterface are allowed');
throw new YaEtlException('Qualifier returned wrong type, only Boolean, nullish and InterrupterInterface are allowed');
}
}
9 changes: 1 addition & 8 deletions src/Transformers/Arrays/ArrayWalkRecursiveTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

use fab2s\NodalFlow\NodalFlowException;
use fab2s\YaEtl\Transformers\TransformerAbstract;
use fab2s\YaEtl\YaEtlException;

/**
* Class ArrayWalkRecursiveTransformer
Expand Down Expand Up @@ -49,17 +48,11 @@ public function __construct(callable $callable, $userData = null)
*
* @param array $record
*
* @throws YaEtlException
*
* @return array
*/
public function exec($record = null)
{
if (!\array_walk_recursive($record, $this->callable, $this->userData)) {
throw new YaEtlException('array_walk_recursive call failed', 1, null, [
'record' => $record,
]);
}
\array_walk_recursive($record, $this->callable, $this->userData);

return $record;
}
Expand Down
9 changes: 1 addition & 8 deletions src/Transformers/Arrays/ArrayWalkTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

use fab2s\NodalFlow\NodalFlowException;
use fab2s\YaEtl\Transformers\TransformerAbstract;
use fab2s\YaEtl\YaEtlException;

/**
* Class ArrayWalkTransformer
Expand Down Expand Up @@ -49,17 +48,11 @@ public function __construct(callable $callable, $userData = null)
*
* @param array $record
*
* @throws YaEtlException
*
* @return array
*/
public function exec($record = null)
{
if (!\array_walk($record, $this->callable, $this->userData)) {
throw new YaEtlException('array_walk call failed', 1, null, [
'record' => $record,
]);
}
\array_walk($record, $this->callable, $this->userData);

return $record;
}
Expand Down
16 changes: 6 additions & 10 deletions src/YaEtl.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,31 +318,27 @@ protected function aggregateTo(ExtractorInterface $extractor, ExtractorInterface
throw new YaEtlException('Cannot aggregate with orphaned Node:' . \get_class($aggregateWith));
}

/** @var TraversableNodeInterface $aggregateWithNode */
$aggregateWithNode = $this->nodes[$aggregateWithIdx];
if ($aggregateWithNode instanceof AggregateNodeInterface) {
/* @var TraversableNodeInterface $aggregateWithNode */
if (isset($this->reverseAggregateTable[$aggregateWithNodeId])) {
/** @var AggregateNodeInterface $aggregateWithNode */
$aggregateWithNode = $this->reverseAggregateTable[$aggregateWithNodeId];
$aggregateWithNode->addTraversable($extractor);
$this->reverseAggregateTable[$extractor->getId()] = $aggregateWithIdx;

return $this;
}

$aggregateNode = new AggregateExtractor(true);
// keep track of this extractor before we bury it in the aggregate
$this->reverseAggregateTable[$aggregateWithNode->getId()] = $aggregateWithIdx;
$this->reverseAggregateTable[$aggregateWith->getId()] = $aggregateNode;
// now replace its slot in the main tree
$this->replace($aggregateWithIdx, $aggregateNode);
$aggregateNode->addTraversable($aggregateWithNode)
$aggregateNode->addTraversable($aggregateWith)
->addTraversable($extractor);

// adjust counters as we did remove the $aggregateWith Extractor from this flow
$reg = &$this->registry->get($this->getId());
--$reg['flowStats']['num_extractor'];

// aggregate node did take care of setting carrier
$this->reverseAggregateTable[$aggregateNode->getId()] = $aggregateWithIdx;
$this->reverseAggregateTable[$extractor->getId()] = $aggregateWithIdx;

return $this;
}

Expand Down
13 changes: 13 additions & 0 deletions tests/Laravel/DbExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,24 @@

use fab2s\YaEtl\Extractors\ExtractorAbstract;
use fab2s\YaEtl\Laravel\Extractors\DbExtractor;
use fab2s\YaEtl\YaEtlException;

class DbExtractorTest extends LaravelTestCase
{
use ExtractionTestTrait;

public function testDbExtractorException()
{
$this->expectException(YaEtlException::class);
(new DbExtractor)->setExtractQuery(null);
}

public function testDbExtractorExceptionType()
{
$this->expectException(YaEtlException::class);
(new DbExtractor())->setExtractQuery(TestModel::query());
}

protected function getTestQuery()
{
return TestModel::getQuery()->orderBy('id', 'asc');
Expand Down
13 changes: 13 additions & 0 deletions tests/Laravel/ModelExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,24 @@

use fab2s\YaEtl\Extractors\ExtractorAbstract;
use fab2s\YaEtl\Laravel\Extractors\ModelQueryExtractor;
use fab2s\YaEtl\YaEtlException;

class ModelExtractorTest extends LaravelTestCase
{
use ExtractionTestTrait;

public function testModelExtractorException()
{
$this->expectException(YaEtlException::class);
(new ModelQueryExtractor)->setExtractQuery(null);
}

public function testModelExtractorExceptionType()
{
$this->expectException(YaEtlException::class);
(new ModelQueryExtractor)->setExtractQuery(TestModel::getQuery());
}

protected function getTestQuery()
{
return TestModel::orderBy('id', 'asc');
Expand Down
2 changes: 1 addition & 1 deletion tests/Lib/Arrays/ArrayWalkRecursiveTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ArrayWalkRecursiveTransformerTest extends TestBase
* @throws NodalFlowException
* @throws YaEtlException
*/
public function testArrayWalkTransformer(callable $callable, array $data, $expected, $arg = null)
public function testArrayWalkRecursiveTransformer(callable $callable, array $data, $expected, $arg = null)
{
$transformer = new ArrayWalkRecursiveTransformer($callable, $arg);

Expand Down

0 comments on commit 8c30f50

Please sign in to comment.