1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AbacaphiliacFeature\AwsSdk\ClaimCheck\Bootstrap\ContextTrait; |
4
|
|
|
|
5
|
|
|
use Abacaphiliac\AwsSdk\ClaimCheck\Exception\ExceptionInterface; |
6
|
|
|
use Abacaphiliac\AwsSdk\ClaimCheck\ClaimCheckFactory; |
7
|
|
|
use Abacaphiliac\AwsSdk\ClaimCheck\Sqs\SqsExtendedClientConfiguration; |
8
|
|
|
use Abacaphiliac\AwsSdk\ClaimCheck\Sqs\SqsExtendedClient; |
9
|
|
|
use Aws\Result; |
10
|
|
|
use Aws\S3\S3Client; |
11
|
|
|
use Aws\Sqs\SqsClient; |
12
|
|
|
|
13
|
|
|
trait SqsContextTrait |
14
|
|
|
{ |
15
|
|
|
/** @var SqsClient */ |
16
|
|
|
private $sqsClient; |
17
|
|
|
|
18
|
|
|
/** @var SqsExtendedClientConfiguration */ |
19
|
|
|
private $sqsExtendedClientConfiguration; |
20
|
|
|
|
21
|
|
|
/** @var string */ |
22
|
|
|
private $queueUrl; |
23
|
|
|
|
24
|
|
|
/** @var string[] */ |
25
|
|
|
private $queueUrls = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @return S3Client |
29
|
|
|
*/ |
30
|
|
|
abstract public function getS3Client(); |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return string |
34
|
|
|
*/ |
35
|
|
|
abstract public function getBucketName(); |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
|
|
abstract public function getMessage(); |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return mixed[] |
44
|
|
|
*/ |
45
|
|
|
abstract public function getAwsServiceConfig(); |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return ClaimCheckFactory |
49
|
|
|
*/ |
50
|
|
|
abstract public function createClaimCheckFactory(); |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return SqsClient |
54
|
|
|
* @throws ExceptionInterface |
55
|
|
|
*/ |
56
|
|
|
public function getSqsClient() |
57
|
|
|
{ |
58
|
|
|
if (!$this->sqsClient) { |
59
|
|
|
$config = $this->getAwsServiceConfig(); |
60
|
|
|
$client = new SqsClient($config); |
61
|
|
|
$this->sqsClient = new SqsExtendedClient($client, $this->getSqsExtendedClientConfiguration()); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $this->sqsClient; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return SqsExtendedClientConfiguration |
69
|
|
|
* @throws ExceptionInterface |
70
|
|
|
*/ |
71
|
|
|
public function getSqsExtendedClientConfiguration() |
72
|
|
|
{ |
73
|
|
|
if (!$this->sqsExtendedClientConfiguration) { |
74
|
|
|
$s3Client = $this->getS3Client(); |
75
|
|
|
|
76
|
|
|
$s3BucketName = $this->getBucketName(); |
77
|
|
|
|
78
|
|
|
$this->sqsExtendedClientConfiguration = new SqsExtendedClientConfiguration($s3Client, $s3BucketName); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $this->sqsExtendedClientConfiguration; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @Given /^a queue named "([^"]*)"$/ |
86
|
|
|
* @param string $name |
87
|
|
|
* @return string |
88
|
|
|
* @throws ExceptionInterface |
89
|
|
|
*/ |
90
|
|
|
public function getQueueFixture($name) |
91
|
|
|
{ |
92
|
|
|
$this->queueUrl = $this->getQueueUrl($name); |
|
|
|
|
93
|
|
|
|
94
|
|
|
return $this->queueUrl; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param string $name |
99
|
|
|
* @return string|bool |
100
|
|
|
* @throws ExceptionInterface |
101
|
|
|
*/ |
102
|
|
|
public function getQueueUrl($name) |
103
|
|
|
{ |
104
|
|
|
if (array_key_exists($name, $this->queueUrls)) { |
105
|
|
|
return $this->queueUrls[$name]; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$result = $this->getSqsClient()->getQueueUrl([ |
109
|
|
|
'QueueName' => $name, |
110
|
|
|
]); |
111
|
|
|
|
112
|
|
|
$this->queueUrls[$name] = $result->get('QueueUrl'); |
113
|
|
|
|
114
|
|
|
return $this->queueUrls[$name]; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param string $name |
119
|
|
|
* @return string |
120
|
|
|
* @throws ExceptionInterface |
121
|
|
|
*/ |
122
|
|
|
public function getQueueArn($name) |
123
|
|
|
{ |
124
|
|
|
$queueUrl = $this->getQueueUrl($name); |
125
|
|
|
|
126
|
|
|
return $this->getSqsClient()->getQueueArn($queueUrl); |
|
|
|
|
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @When /^I send the message to a queue named "([^"]*)"$/ |
131
|
|
|
* @param string $name |
132
|
|
|
* @return Result |
133
|
|
|
* @throws ExceptionInterface |
134
|
|
|
*/ |
135
|
|
|
public function iSendTheMessageToQueue($name) |
136
|
|
|
{ |
137
|
|
|
$sqsClient = $this->getSqsClient(); |
138
|
|
|
$queueUrl = $this->getQueueUrl($name); |
139
|
|
|
$message = $this->getMessage(); |
140
|
|
|
|
141
|
|
|
return $sqsClient->sendMessage([ |
142
|
|
|
'QueueUrl' => $queueUrl, |
143
|
|
|
'MessageBody' => $message, |
144
|
|
|
]); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @Then /^I can fetch the message from a queue named "([^"]*)"$/ |
149
|
|
|
* @param string $name |
150
|
|
|
* @return Result |
151
|
|
|
* @throws ExceptionInterface |
152
|
|
|
* @throws \RuntimeException |
153
|
|
|
* @throws \PHPUnit_Framework_AssertionFailedError |
154
|
|
|
*/ |
155
|
|
|
public function iCanFetchTheMessageFromQueue($name) |
156
|
|
|
{ |
157
|
|
|
$expectedMessage = $this->getMessage(); |
158
|
|
|
\PHPUnit_Framework_Assert::assertNotEmpty($expectedMessage); |
159
|
|
|
|
160
|
|
|
$sqsClient = $this->getSqsClient(); |
161
|
|
|
|
162
|
|
|
$queueUrl = $this->getQueueUrl($name); |
163
|
|
|
|
164
|
|
|
$result = $sqsClient->receiveMessage([ |
165
|
|
|
'QueueUrl' => $queueUrl, |
166
|
|
|
'MaxNumberOfMessages' => 1, |
167
|
|
|
]); |
168
|
|
|
|
169
|
|
|
$actualMessage = $result->search('Messages[].Body|[0]'); |
170
|
|
|
\PHPUnit_Framework_Assert::assertNotEmpty($actualMessage); |
171
|
|
|
|
172
|
|
|
\PHPUnit_Framework_Assert::assertEquals( |
173
|
|
|
$expectedMessage, |
174
|
|
|
$actualMessage, |
175
|
|
|
'Purge the queues before running the feature suite.' |
176
|
|
|
); |
177
|
|
|
|
178
|
|
|
$sqsClient->deleteMessage([ |
179
|
|
|
'QueueUrl' => $queueUrl, |
180
|
|
|
'ReceiptHandle' => $result->search('Messages[].ReceiptHandle|[0]'), |
181
|
|
|
]); |
182
|
|
|
|
183
|
|
|
return $result; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.