Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
MessageValidatorTest.php
Go to the documentation of this file.
1 <?php
8 
9 use Doctrine\Instantiator\Exception\InvalidArgumentException;
10 use Magento\Framework\Communication\ConfigInterface as CommunicationConfig;
12 
17 class MessageValidatorTest extends \PHPUnit\Framework\TestCase
18 {
20  protected $model;
21 
24 
25  protected function setUp()
26  {
27  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
28 
29  $this->model = $objectManager->getObject(MessageValidator::class);
30  $this->communicationConfigMock = $this->getMockBuilder(CommunicationConfig::class)
31  ->disableOriginalConstructor()
32  ->getMock();
33  $objectManager->setBackwardCompatibleProperty(
34  $this->model,
35  'communicationConfig',
36  $this->communicationConfigMock
37  );
38  }
39 
44  public function testValidateInvalidTopic()
45  {
46  $this->model->validate('customer.created', 'Some message', true);
47  }
48 
49  public function testValidateValidObjectType()
50  {
51  $this->communicationConfigMock->expects($this->any())->method('getTopic')->willReturn(
52  $this->getQueueConfigDataObjectType()
53  );
54  $object = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
55  ->disableOriginalConstructor()
56  ->setMethods([])
57  ->getMock();
58 
59  $this->model->validate('customer.created', $object, true);
60  }
61 
62  public function testValidateValidMethodType()
63  {
64  $this->communicationConfigMock->expects($this->any())->method('getTopic')->willReturn(
65  $this->getQueueConfigDataMethodType()
66  );
67  $object = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
68  ->disableOriginalConstructor()
69  ->setMethods([])
70  ->getMock();
71 
72  $this->model->validate('customer.created', [$object, 'password', 'redirect'], true);
73  }
74 
76  {
77  $this->communicationConfigMock->expects($this->any())->method('getTopic')->willReturn(
78  $this->getQueueConfigDataObjectType()
79  );
80  $this->model->validate('customer.created', [], true);
81  }
82 
88  {
89  $this->communicationConfigMock->expects($this->any())->method('getTopic')->willReturn(
90  $this->getQueueConfigDataMethodType()
91  );
92  $this->model->validate('customer.created', [1, 2, 3], true);
93  }
94 
100  private function getQueueConfigDataObjectType()
101  {
102  return [
103  CommunicationConfig::TOPIC_REQUEST_TYPE => CommunicationConfig::TOPIC_REQUEST_TYPE_CLASS,
104  CommunicationConfig::TOPIC_REQUEST => \Magento\Customer\Api\Data\CustomerInterface::class
105  ];
106  }
107 
113  private function getQueueConfigDataMethodType()
114  {
115  return [
116  CommunicationConfig::TOPIC_REQUEST_TYPE => CommunicationConfig::TOPIC_REQUEST_TYPE_METHOD,
117  CommunicationConfig::TOPIC_REQUEST => [
118  [
119  'param_name' => 'customer',
120  'param_position' => 0,
121  'is_required' => true,
122  'param_type' => \Magento\Customer\Api\Data\CustomerInterface::class,
123  ],
124  [
125  'param_name' => 'password',
126  'param_position' => 1,
127  'is_required' => false,
128  'param_type' => 'string',
129  ],
130  [
131  'param_name' => 'redirectUrl',
132  'param_position' => 2,
133  'is_required' => false,
134  'param_type' => 'string',
135  ],
136  ]
137  ];
138  }
139 
143  public function testInvalidMessageType($requestType, $message, $expectedResult = null)
144  {
145  $this->communicationConfigMock->expects($this->any())->method('getTopic')->willReturn($requestType);
146  if ($expectedResult) {
147  $this->expectException('InvalidArgumentException');
148  $this->expectExceptionMessage($expectedResult);
149  }
150  $this->model->validate('topic', $message);
151  }
152 
156  public function getQueueConfigRequestType()
157  {
158  $customerMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
159  ->disableOriginalConstructor()
160  ->setMethods([])
161  ->getMock();
162  $customerMockTwo = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
163  ->disableOriginalConstructor()
164  ->setMethods([])
165  ->getMock();
166 
167  return [
168  [
169  [
170  CommunicationConfig::TOPIC_REQUEST_TYPE => CommunicationConfig::TOPIC_REQUEST_TYPE_CLASS,
171  CommunicationConfig::TOPIC_REQUEST => 'string'
172  ],
173  'valid string',
174  null
175  ],
176  [
177  [
178  CommunicationConfig::TOPIC_REQUEST_TYPE => CommunicationConfig::TOPIC_REQUEST_TYPE_CLASS,
179  CommunicationConfig::TOPIC_REQUEST => 'string'
180  ],
181  1,
182  'Data in topic "topic" must be of type "string". "int" given.'
183  ],
184  [
185  [
186  CommunicationConfig::TOPIC_REQUEST_TYPE => CommunicationConfig::TOPIC_REQUEST_TYPE_CLASS,
187  CommunicationConfig::TOPIC_REQUEST => 'string[]'
188  ],
189  ['string1', 'string2'],
190  null
191  ],
192  [
193  [
194  CommunicationConfig::TOPIC_REQUEST_TYPE => CommunicationConfig::TOPIC_REQUEST_TYPE_CLASS,
195  CommunicationConfig::TOPIC_REQUEST => 'string[]'
196  ],
197  [],
198  null
199  ],
200  [
201  [
202  CommunicationConfig::TOPIC_REQUEST_TYPE => CommunicationConfig::TOPIC_REQUEST_TYPE_CLASS,
203  CommunicationConfig::TOPIC_REQUEST => 'string[]'
204  ],
205  'single string',
206  'Data in topic "topic" must be of type "string[]". "string" given.'
207  ],
208  [
209  [
210  CommunicationConfig::TOPIC_REQUEST_TYPE => CommunicationConfig::TOPIC_REQUEST_TYPE_CLASS,
211  CommunicationConfig::TOPIC_REQUEST => \Magento\Customer\Api\Data\CustomerInterface::class
212  ],
213  $customerMock,
214  null
215  ],
216  [
217  [
218  CommunicationConfig::TOPIC_REQUEST_TYPE => CommunicationConfig::TOPIC_REQUEST_TYPE_CLASS,
219  CommunicationConfig::TOPIC_REQUEST => \Magento\Customer\Api\Data\CustomerInterface::class
220  ],
221  'customer',
222  'Data in topic "topic" must be of type "Magento\Customer\Api\Data\CustomerInterface". "string" given.'
223  ],
224  [
225  [
226  CommunicationConfig::TOPIC_REQUEST_TYPE => CommunicationConfig::TOPIC_REQUEST_TYPE_CLASS,
227  CommunicationConfig::TOPIC_REQUEST => 'Magento\Customer\Api\Data\CustomerInterface[]'
228  ],
229  [$customerMock, $customerMockTwo],
230  null
231  ],
232  [
233  [
234  CommunicationConfig::TOPIC_REQUEST_TYPE => CommunicationConfig::TOPIC_REQUEST_TYPE_CLASS,
235  CommunicationConfig::TOPIC_REQUEST => 'Magento\Customer\Api\Data\CustomerInterface[]'
236  ],
237  [],
238  null
239  ],
240  [
241  [
242  CommunicationConfig::TOPIC_REQUEST_TYPE => CommunicationConfig::TOPIC_REQUEST_TYPE_CLASS,
243  CommunicationConfig::TOPIC_REQUEST => 'Magento\Customer\Api\Data\CustomerInterface[]'
244  ],
245  'customer',
246  'Data in topic "topic" must be of type "Magento\Customer\Api\Data\CustomerInterface[]". "string" given.'
247  ],
248  [
249  [
250  CommunicationConfig::TOPIC_REQUEST_TYPE => CommunicationConfig::TOPIC_REQUEST_TYPE_CLASS,
251  CommunicationConfig::TOPIC_REQUEST => 'Magento\Customer\Api\Data\CustomerInterface[]'
252  ],
253  $customerMock,
254  'Data in topic "topic" must be of type "Magento\Customer\Api\Data\CustomerInterface[]". '
255  ],
256  ];
257  }
258 }
$objectManager
Definition: bootstrap.php:17
$message
testInvalidMessageType($requestType, $message, $expectedResult=null)