Skip to content

Commit 3b7e6f2

Browse files
committed
added: action tests
1 parent bf8cde2 commit 3b7e6f2

9 files changed

+261
-9
lines changed

src/Component/Action/ExpandAction.php

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Misery\Component\Action;
44

5-
use Misery\Component\Common\Functions\ArrayFunctions;
65
use Misery\Component\Common\Options\OptionsInterface;
76
use Misery\Component\Common\Options\OptionsTrait;
87

src/Component/Action/FilterAction.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class FilterAction implements OptionsInterface
2222

2323
public function apply($item)
2424
{
25-
$field = $this->getOption('key', $this->getOption('field'));
25+
$field = $this->getOption('key', $this->getOption('field')); # legacy support
2626
$listItem = $item[$field] ?? null;
2727
if (empty($listItem) || empty($field)) {
2828
return $item;

src/Component/Action/FormatAction.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,9 @@
77
use Misery\Component\Reader\ItemReaderAwareInterface;
88
use Misery\Component\Reader\ItemReaderAwareTrait;
99

10-
class FormatAction implements OptionsInterface, ItemReaderAwareInterface
10+
class FormatAction implements OptionsInterface
1111
{
1212
use OptionsTrait;
13-
use ItemReaderAwareTrait;
14-
15-
private $mapper;
1613

1714
public const NAME = 'format';
1815

src/Component/Action/HashAction.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@ class HashAction implements OptionsInterface
1313
public const NAME = 'hash';
1414

1515
private $options = [
16-
'key' => null
16+
'key' => null,
17+
'field' => null,
1718
];
1819

1920
public function apply(array $item): array
2021
{
21-
if (array_key_exists($this->options['key'], $item)) {
22-
$item[$this->getOption('key')] = (string) crc32($item[$this->getOption('key')]);
22+
$field = $this->getOption('field', $this->getOption('key')); # legacy key
23+
24+
if (array_key_exists($field, $item)) {
25+
$item[$field] = (string) crc32($item[$field]);
2326
}
2427

2528
return $item;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Tests\Misery\Component\Action;
4+
5+
use Misery\Component\Action\FormatAction;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class FormatActionTest extends TestCase
9+
{
10+
public function testApplyReplaceFunction()
11+
{
12+
$item = ['field' => 'abc123'];
13+
14+
$action = new FormatAction();
15+
$action->setOptions([
16+
'field' => 'field',
17+
'functions' => ['replace'],
18+
'search' => '123',
19+
'replace' => '456'
20+
]);
21+
22+
$result = $action->apply($item);
23+
24+
$this->assertEquals(['field' => 'abc456'], $result);
25+
}
26+
27+
public function testApplyNumberFunction()
28+
{
29+
$item = ['field' => 12345.6789];
30+
31+
$action = new FormatAction();
32+
$action->setOptions([
33+
'field' => 'field',
34+
'functions' => ['number'],
35+
'decimals' => 2,
36+
'decimal_sep' => ',',
37+
'mille_sep' => '.'
38+
]);
39+
40+
$result = $action->apply($item);
41+
42+
$this->assertEquals(['field' => '12.345,68'], $result);
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace Tests\Misery\Component\Action;
4+
5+
use Misery\Component\Action\GenerateIdAction;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class GenerateIdActionTest extends TestCase
9+
{
10+
public function testApplyAutoIncrement()
11+
{
12+
$item = ['some_field' => 'value'];
13+
14+
$action = new GenerateIdAction();
15+
$action->setOption('start_id', 1);
16+
$action->setOption('field', 'id');
17+
$result = $action->apply($item);
18+
19+
$this->assertEquals(['some_field' => 'value', 'id' => 1], $result);
20+
21+
// Apply the action again to test auto-increment
22+
$result = $action->apply($item);
23+
$this->assertEquals(['some_field' => 'value', 'id' => 2], $result);
24+
}
25+
26+
public function testApplyAutoIncrementWithStartId()
27+
{
28+
$item = ['some_field' => 'value'];
29+
30+
$action = new GenerateIdAction();
31+
$action->setOption('start_id', 10000);
32+
$action->setOption('field', 'id');
33+
$result = $action->apply($item);
34+
35+
$this->assertEquals(['some_field' => 'value', 'id' => 10000], $result);
36+
37+
// Apply the action again to test auto-increment
38+
$result = $action->apply($item);
39+
$this->assertEquals(['some_field' => 'value', 'id' => 10001], $result);
40+
}
41+
42+
public function testApplyAutoSequenceWithFormat()
43+
{
44+
$item = ['attribute' => 'color', 'field2' => 'value2'];
45+
46+
$action = new GenerateIdAction();
47+
$action->setOptions([
48+
'format' => '%attribute%_%auto-sequence%',
49+
'format_fields' => ['attribute'],
50+
'field' => 'sequence',
51+
]);
52+
$result = $action->apply($item);
53+
54+
$this->assertEquals(['attribute' => 'color', 'field2' => 'value2', 'sequence' => 'color_1'], $result);
55+
56+
// Apply the action again to test auto-sequence
57+
$result = $action->apply($item);
58+
$this->assertEquals(['attribute' => 'color', 'field2' => 'value2', 'sequence' => 'color_2'], $result);
59+
60+
// Apply the action again to test auto-sequence
61+
$result = $action->apply(['attribute' => 'brand', 'field2' => 'value2']);
62+
$this->assertEquals(['attribute' => 'brand', 'field2' => 'value2', 'sequence' => 'brand_1'], $result);
63+
}
64+
}
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Tests\Misery\Component\Action;
4+
5+
use Misery\Component\Action\HashAction;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class HashActionTest extends TestCase
9+
{
10+
public function testApplyWithKeyExists()
11+
{
12+
$item = ['key' => 'value'];
13+
14+
$action = new HashAction();
15+
$action->setOption('field', 'key');
16+
$result = $action->apply($item);
17+
18+
$this->assertEquals(['key' => crc32('value')], $result);
19+
}
20+
21+
public function testApplyWithKeyMissing()
22+
{
23+
$item = ['other_key' => 'value'];
24+
25+
$action = new HashAction();
26+
$action->setOption('field', 'key');
27+
$result = $action->apply($item);
28+
29+
$this->assertEquals(['other_key' => 'value'], $result);
30+
}
31+
}
+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace Tests\Misery\Component\Action;
4+
5+
use Misery\Component\Action\SkipAction;
6+
use Misery\Component\Common\Pipeline\Exception\SkipPipeLineException;
7+
use PHPUnit\Framework\TestCase;
8+
9+
class SkipActionTest extends TestCase
10+
{
11+
public function testSkipEmptyField()
12+
{
13+
$item = ['field' => ''];
14+
15+
$action = new SkipAction();
16+
$action->setOptions([
17+
'field' => 'field',
18+
'state' => 'EMPTY',
19+
'skip_message' => 'Field is empty'
20+
]);
21+
22+
$this->expectException(SkipPipeLineException::class);
23+
$this->expectExceptionMessage('Field is empty');
24+
$action->apply($item);
25+
}
26+
27+
public function testSkipMatchingState()
28+
{
29+
$item = ['field' => 'SKIP'];
30+
31+
$action = new SkipAction();
32+
$action->setOptions([
33+
'field' => 'field',
34+
'state' => 'SKIP',
35+
'skip_message' => 'Field matches state'
36+
]);
37+
38+
$this->expectException(SkipPipeLineException::class);
39+
$this->expectExceptionMessage('Field matches state');
40+
$action->apply($item);
41+
}
42+
43+
public function testNoSkip()
44+
{
45+
$item = ['field' => 'value'];
46+
47+
$action = new SkipAction();
48+
$action->setOptions([
49+
'field' => 'field',
50+
'state' => 'EMPTY',
51+
'skip_message' => 'Field is empty'
52+
]);
53+
54+
$result = $action->apply($item);
55+
56+
$this->assertEquals($item, $result);
57+
}
58+
59+
public function testSkipWithForceSkip()
60+
{
61+
$item = ['field' => ''];
62+
63+
$action = new SkipAction();
64+
$action->setOptions([
65+
'field' => 'field',
66+
'state' => 'EMPTY',
67+
'skip_message' => 'Field is empty',
68+
'force_skip' => true
69+
]);
70+
71+
$this->expectException(SkipPipeLineException::class);
72+
$this->expectExceptionMessage('Field is empty');
73+
$action->apply($item);
74+
}
75+
}
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Tests\Misery\Component\Action;
4+
5+
use Misery\Component\Action\UnsetAction;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class UnsetActionTest extends TestCase
9+
{
10+
public function testApplyUnsetKeys()
11+
{
12+
$item = ['field' => ['key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3']];
13+
14+
$action = new UnsetAction();
15+
$action->setOptions([
16+
'field' => 'field',
17+
'list' => ['key1', 'key3']
18+
]);
19+
20+
$result = $action->apply($item);
21+
22+
$this->assertEquals(['field' => ['key2' => 'value2']], $result);
23+
}
24+
25+
public function testApplyWithNonArrayField()
26+
{
27+
$item = ['field' => 'value'];
28+
29+
$action = new UnsetAction();
30+
$action->setOptions([
31+
'field' => 'field',
32+
'list' => ['key1', 'key2']
33+
]);
34+
35+
$result = $action->apply($item);
36+
37+
$this->assertEquals(['field' => 'value'], $result);
38+
}
39+
}

0 commit comments

Comments
 (0)