ResultValidatorImpl::descriptionOf()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 0
cts 4
cp 0
rs 9.4286
cc 1
eloc 4
nc 1
nop 1
crap 2
1
<?php
2
3
/*
4
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15
 *
16
 * The software is based on the Axon Framework project which is
17
 * licensed under the Apache 2.0 license. For more information on the Axon Framework
18
 * see <http://www.axonframework.org/>.
19
 * 
20
 * This software consists of voluntary contributions made by many individuals
21
 * and is licensed under the MIT license. For more information, see
22
 * <http://www.governor-framework.org/>.
23
 */
24
25
namespace Governor\Framework\Test;
26
27
use Hamcrest\Matcher;
28
use Hamcrest\StringDescription;
29
use Hamcrest\Core\IsNull;
30
use Governor\Framework\Test\Matchers\EqualFieldsMatcher;
31
use Governor\Framework\CommandHandling\CommandCallbackInterface;
32
33
/**
34
 * Description of ResultValidatorImpl
35
 *
36
 * @author david
37
 */
38
class ResultValidatorImpl implements ResultValidatorInterface, CommandCallbackInterface
39
{
40
41
    private $storedEvents;
42
    private $publishedEvents;
43
    private $actualReturnValue;
44
    private $actualException;
45
    private $reporter;
46
47 3
    function __construct(array &$storedEvents, array &$publishedEvents)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
48
    {
49 3
        $this->storedEvents = &$storedEvents;
50 3
        $this->publishedEvents = &$publishedEvents;
51 3
        $this->reporter = new Reporter();
52 3
    }
53
54 1 View Code Duplication
    public function expectEvents(array $expectedEvents)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55
    {
56 1
        if (count($this->publishedEvents) !== count($this->storedEvents)) {
57
            $this->reporter->reportDifferenceInStoredVsPublished($this->storedEvents,
58
                $this->publishedEvents, $this->actualException);
59
        }
60
61 1
        return $this->expectPublishedEvents($expectedEvents);
62
    }
63
64 View Code Duplication
    public function expectEventsMatching(Matcher $matcher)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
65
    {
66
        if (count($this->publishedEvents) !== count($this->storedEvents)) {
67
            $this->reporter->reportDifferenceInStoredVsPublished($this->storedEvents,
68
                $this->publishedEvents, $this->actualException);
69
        }
70
71
        return $this->expectPublishedEventsMatching($matcher);
72
    }
73
74
    public function expectException(Matcher $matcher)
75
    {
76
        $description = new StringDescription();
77
        $matcher->describeTo($description);
78
79
        if (null === $this->actualException) {
80
            $this->reporter->reportUnexpectedReturnValue($this->actualReturnValue,
81
                $description);
82
        }
83
        if (!$matcher->matches($this->actualException)) {
84
            $this->reporter->reportWrongException($this->actualException,
85
                $description);
86
        }
87
        return $this;
88
    }
89
90 1 View Code Duplication
    public function expectPublishedEvents(array $expectedEvents)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
91
    {
92 1
        if (count($expectedEvents) !== count($this->publishedEvents)) {
93
            $this->reporter->reportWrongEvent($this->publishedEvents,
94
                $expectedEvents, $this->actualException);
95
        }
96
97 1
        foreach ($expectedEvents as $expectedEvent) {
98 1
            $actualEvent = current($this->publishedEvents);
99 1
            if (!$this->verifyEventEquality($expectedEvent,
100 1
                $actualEvent->getPayload())) {
101
                $this->reporter->reportWrongEvent($this->publishedEvents,
102
                    $expectedEvents, $this->actualException);
103
            }
104
105 1
            next($this->publishedEvents);
106 1
        }
107 1
        return $this;
108
    }
109
110 View Code Duplication
    public function expectPublishedEventsMatching(Matcher $matcher)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
111
    {
112
        if (!$matcher->matches($this->publishedEvents)) {
113
            $this->reporter->reportWrongEventDescription($this->publishedEvents,
114
                $this->descriptionOf($matcher), $this->actualException);
115
        }
116
        return $this;
117
    }
118
119
    private function descriptionOf(Matcher $matcher)
120
    {
121
        $description = new StringDescription();
122
        $matcher->describeTo($description);
123
        return $description;
124
    }
125
126
    public function expectReturnValue(Matcher $matcher = null)
127
    {
128
        if (null === $matcher) {
129
            return $this->expectReturnValue(new IsNull());
130
        }
131
132
        $description = new StringDescription();
133
        $matcher->describeTo($description);
134
135
        if (null !== $this->actualException) {
136
            $this->reporter->reportUnexpectedException($this->actualException,
137
                $description);
138
        } else if (!$matcher->matches($this->actualReturnValue)) {
139
            $this->reporter->reportWrongResult($this->actualReturnValue,
140
                $description);
141
        }
142
        return $this;
143
    }
144
145 View Code Duplication
    public function expectStoredEvents(array $expectedEvents)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
146
    {
147
        if (count($expectedEvents) !== count($this->storedEvents)) {
148
            $this->reporter->reportWrongEvent($this->storedEvents,
149
                $expectedEvents, $this->actualException);
150
        }
151
152
        foreach ($expectedEvents as $expectedEvent) {
153
            $actualEvent = current($this->storedEvents);
154
            if (!$this->verifyEventEquality($expectedEvent,
155
                $actualEvent->getPayload())) {
156
                $this->reporter->reportWrongEvent($this->storedEvents,
157
                    $expectedEvents, $this->actualException);
158
            }
159
160
            next($this->storedEvents);
161
        }
162
        return $this;
163
    }
164
165 View Code Duplication
    public function expectStoredEventsMatching(Matcher $matcher)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
166
    {
167
        if (!$matcher->matches($this->storedEvents)) {
168
            $this->reporter->reportWrongEventDescription($this->storedEvents,
169
                $this->descriptionOf($matcher), $this->actualException);
170
        }
171
        return $this;
172
    }
173
174
    public function expectVoidReturnType()
175
    {
176
        return $this->expectReturnValue(null);
177
    }
178
179 1
    private function verifyEventEquality($expectedEvent, $actualEvent)
180
    {
181 1
        if (0 !== strcmp(get_class($expectedEvent), get_class($actualEvent))) {
182
            return false;
183
        }
184
185 1
        $matcher = new EqualFieldsMatcher($expectedEvent);
186
187 1
        if (!$matcher->matches($actualEvent)) {
188
            $this->reporter->reportDifferentEventContents(get_class($expectedEvent),
189
                $matcher->getFailedField(),
190
                $matcher->getFailedFieldActualValue(),
191
                $matcher->getFailedFieldExpectedValue());
192
        }
193
194 1
        return true;
195
    }
196
197
    public function onFailure(\Exception $cause)
198
    {
199
        $this->actualException = $cause;
200
    }
201
202 3
    public function onSuccess($result)
203
    {
204 3
        $this->actualReturnValue = $result;
205 3
    }
206
207
    /**
208
     * Makes sure the execution phase has finishes without any Errors ir FixtureExecutionExceptions. If an error was
209
     * recorded, it will be thrown immediately. This allow one to distinguish between failed tests, and tests in error.
210
     */
211 3
    public function assertValidRecording()
212
    {
213 3
        if ($this->actualException instanceof \Exception) {
214
            throw $this->actualException;
215
        }
216 3
    }
217
218
}
219