|
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\Saga; |
|
26
|
|
|
|
|
27
|
|
|
use Governor\Framework\Saga\AssociationValue; |
|
28
|
|
|
use Governor\Framework\Test\GovernorAssertionError; |
|
29
|
|
|
use Governor\Framework\Saga\Repository\Memory\InMemorySagaRepository; |
|
30
|
|
|
|
|
31
|
|
|
class RepositoryContentValidator |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* @var InMemorySagaRepository |
|
35
|
|
|
*/ |
|
36
|
|
|
private $sagaRepository; |
|
37
|
|
|
/** |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
private $sagaType; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Initialize the validator to validate contents of the given <code>sagaRepository</code>, which contains Sagas of |
|
44
|
|
|
* the given <code>sagaType</code>. |
|
45
|
|
|
* |
|
46
|
|
|
* @param InMemorySagaRepository $sagaRepository The repository to monitor |
|
47
|
|
|
* @param string $sagaType The type of saga to validate |
|
48
|
|
|
*/ |
|
49
|
5 |
|
public function __construct(InMemorySagaRepository $sagaRepository, $sagaType) |
|
50
|
|
|
{ |
|
51
|
5 |
|
$this->sagaRepository = $sagaRepository; |
|
52
|
5 |
|
$this->sagaType = $sagaType; |
|
53
|
5 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Asserts that an association is present for the given <code>associationKey</code> and |
|
57
|
|
|
* <code>associationValue</code>. |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $associationKey The key of the association |
|
60
|
|
|
* @param string $associationValue The value of the association |
|
61
|
|
|
* @throws GovernorAssertionError |
|
62
|
|
|
*/ |
|
63
|
4 |
View Code Duplication |
public function assertAssociationPresent($associationKey, $associationValue) |
|
|
|
|
|
|
64
|
|
|
{ |
|
65
|
4 |
|
$associatedSagas = $this->sagaRepository->find( |
|
66
|
4 |
|
$this->sagaType, |
|
67
|
4 |
|
new AssociationValue($associationKey, $associationValue) |
|
68
|
4 |
|
); |
|
69
|
|
|
|
|
70
|
4 |
|
if (empty($associatedSagas)) { |
|
71
|
|
|
throw new GovernorAssertionError( |
|
72
|
|
|
sprintf( |
|
73
|
|
|
"Expected a saga to be associated with key:<%s> value:<%s>, but found none", |
|
74
|
|
|
$associationKey, |
|
75
|
|
|
$associationValue |
|
76
|
|
|
) |
|
77
|
|
|
); |
|
78
|
|
|
} |
|
79
|
4 |
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Asserts that <em>no</em> association is present for the given <code>associationKey</code> and |
|
83
|
|
|
* <code>associationValue</code>. |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $associationKey The key of the association |
|
86
|
|
|
* @param string $associationValue The value of the association |
|
87
|
|
|
* @throws GovernorAssertionError |
|
88
|
|
|
*/ |
|
89
|
3 |
View Code Duplication |
public function assertNoAssociationPresent($associationKey, $associationValue) |
|
|
|
|
|
|
90
|
|
|
{ |
|
91
|
3 |
|
$associatedSagas = $this->sagaRepository->find( |
|
92
|
3 |
|
$this->sagaType, |
|
93
|
3 |
|
new AssociationValue($associationKey, $associationValue) |
|
94
|
3 |
|
); |
|
95
|
|
|
|
|
96
|
3 |
|
if (!empty($associatedSagas)) { |
|
97
|
|
|
throw new GovernorAssertionError( |
|
98
|
|
|
sprintf( |
|
99
|
|
|
"Expected a saga to be associated with key:<%s> value:<%s>, but found <%s>", |
|
100
|
|
|
$associationKey, |
|
101
|
|
|
$associationValue |
|
102
|
|
|
) |
|
103
|
|
|
); |
|
104
|
|
|
} |
|
105
|
3 |
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Asserts that the repository contains the given <code>expected</code> amount of active sagas. |
|
109
|
|
|
* |
|
110
|
|
|
* @param int $expected The number of expected sagas. |
|
111
|
|
|
* @throws GovernorAssertionError |
|
112
|
|
|
*/ |
|
113
|
5 |
|
public function assertActiveSagas($expected) |
|
114
|
|
|
{ |
|
115
|
5 |
|
if ($expected !== $this->sagaRepository->size()) { |
|
116
|
|
|
throw new GovernorAssertionError( |
|
117
|
|
|
sprintf("Wrong number of active sagas. Expected <%s>, got <%s>.", |
|
118
|
|
|
$expected, |
|
119
|
|
|
$this->sagaRepository->size() |
|
120
|
|
|
) |
|
121
|
|
|
); |
|
122
|
|
|
} |
|
123
|
5 |
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
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.