DummyException
last analyzed

Complexity

Total Complexity 0

Size/Duplication

Total Lines 4
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 0
lcom 0
cbo 0
dl 0
loc 4
c 0
b 0
f 0
1
<?php
2
3
namespace spec\Port\Steps;
4
5
use Port\Exception;
6
use Port\Reader;
7
use Port\Reader\ArrayReader;
8
use Port\Steps\Step;
9
use Port\Writer;
10
use Psr\Log\LoggerInterface;
11
use PhpSpec\ObjectBehavior;
12
use Prophecy\Argument;
13
14
class StepAggregatorSpec extends ObjectBehavior
15
{
16
    function let(Reader $reader)
17
    {
18
        $this->beConstructedWith($reader);
19
    }
20
21
    function it_is_initializable()
22
    {
23
        $this->shouldHaveType('Port\Steps\StepAggregator');
24
    }
25
26
    function it_adds_a_step(Step $step)
27
    {
28
        $this->addStep($step)->shouldReturn($this);
29
    }
30
31
    function it_adds_a_writer(Writer $writer)
32
    {
33
        $this->addWriter($writer)->shouldReturn($this);
34
    }
35
36
    function it_processes_a_workflow(Writer $writer)
37
    {
38
        $this->beConstructedWith($this->getReader(), 'Test workflow');
39
40
        $writer->prepare()->shouldBeCalled();
41
        $writer->writeItem(Argument::type('array'))->shouldBeCalledTimes(3);
42
        $writer->finish()->shouldBeCalled();
43
44
        $this->addWriter($writer);
45
46
        $result = $this->process();
47
48
        $result->shouldHaveType('Port\Result');
49
        $result->getStartTime()->shouldHaveType('DateTime');
50
        $result->getEndTime()->shouldHaveType('DateTime');
51
        $result->getElapsed()->shouldHaveType('DateInterval');
52
        $result->getTotalProcessedCount()->shouldReturn(3);
53
        $result->getSuccessCount()->shouldReturn(3);
54
        $result->getErrorCount()->shouldReturn(0);
55
        $result->hasErrors()->shouldReturn(false);
56
        $result->getExceptions()->shouldHaveType('SplObjectStorage');
57
        $result->getName()->shouldReturn('Test workflow');
58
    }
59
60
    function it_catches_exception(Writer $writer, LoggerInterface $logger)
61
    {
62
        $this->beConstructedWith($this->getReader());
63
64
        $e1 = new DummyException('Error 1');
65
        $e2 = new DummyException('Error 2');
66
        $e3 = new DummyException('Error 3');
67
        $writer->prepare()->shouldBeCalled();
68
        $writer->writeItem(['first' => 'James', 'last'  => 'Bond'])->willThrow($e1);
69
        $writer->writeItem(['first' => 'Miss', 'last'  => 'Moneypenny'])->willThrow($e2);
70
        $writer->writeItem(['first' => null, 'last'  => 'Doe'])->willThrow($e3);
71
        $writer->finish()->shouldBeCalled();
72
        $logger->error('Error 1')->shouldBeCalled();
73
        $logger->error('Error 2')->shouldBeCalled();
74
        $logger->error('Error 3')->shouldBeCalled();
75
76
        $this->addWriter($writer);
77
        $this->setLogger($logger);
78
        $this->setSkipItemOnFailure(true);
79
80
        $result = $this->process();
81
82
        $result->getTotalProcessedCount()->shouldReturn(3);
83
        $result->getSuccessCount()->shouldReturn(0);
84
        $result->getErrorCount()->shouldReturn(3);
85
        $result->hasErrors()->shouldReturn(true);
86
        $exceptions = $result->getExceptions();
87
        $exceptions->contains($e1)->shouldReturn(true);
88
        $exceptions->contains($e2)->shouldReturn(true);
89
        $exceptions->contains($e3)->shouldReturn(true);
90
    }
91
92
    function it_throws_an_exception_during_process(Writer $writer)
93
    {
94
        $this->beConstructedWith($this->getReader());
95
96
        $e = new DummyException('Error');
97
        $writer->prepare()->shouldBeCalled();
98
        $writer->writeItem(Argument::type('array'))->willThrow($e);
99
        $writer->finish()->shouldNotBeCalled();
100
101
        $this->addWriter($writer);
102
103
        $this->shouldThrow($e)->duringProcess();
104
    }
105
106
    function it_executes_the_writers_in_the_same_order_that_the_insertion(Writer $writerFoo, Writer $writerBar, Writer $writerBaz)
107
    {
108
        $this->beConstructedWith(new ArrayReader([['test' => 'test']]));
109
        $writerFoo->prepare()->shouldBeCalled();
110
        $writerBar->prepare()->shouldBeCalled();
111
        $writerBaz->prepare()->shouldBeCalled();
112
        $writerFoo->writeItem(Argument::type('array'))->will(function () use ($writerBar, $writerBaz) {
113
            $writerBar->writeItem(Argument::type('array'))->will(function () use ($writerBaz) {
114
                $writerBaz->writeItem(Argument::type('array'))->shouldBeCalled();
115
            })->shouldBeCalled();
116
        })->shouldBeCalled();
117
        $writerFoo->finish()->shouldBeCalled();
118
        $writerBar->finish()->shouldBeCalled();
119
        $writerBaz->finish()->shouldBeCalled();
120
121
        $this->addWriter($writerFoo);
122
        $this->addWriter($writerBar);
123
        $this->addWriter($writerBaz);
124
125
        $this->process();
0 ignored issues
show
Bug introduced by
The method process() does not exist on spec\Port\Steps\StepAggregatorSpec. Did you maybe mean it_processes_a_workflow()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
126
    }
127
128
    protected function getReader()
129
    {
130
        return new ArrayReader([
131
            [
132
                'first' => 'James',
133
                'last'  => 'Bond',
134
            ],
135
            [
136
                'first' => 'Miss',
137
                'last'  => 'Moneypenny',
138
            ],
139
            [
140
                'first' => null,
141
                'last'  => 'Doe',
142
            ],
143
        ]);
144
    }
145
}
146
147
class DummyException extends \Exception implements Exception
148
{
149
150
}
151