|
| 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 | +} |
0 commit comments