Skip to content

Commit f257a4a

Browse files
author
Stanislav Idolov
authored
ENGCOM-3172: [Backport] throw exception InvalidArgumentException during validate scheme #18554
2 parents acd8d51 + b5b43d1 commit f257a4a

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

lib/internal/Magento/Framework/Communication/Config/Validator.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public function __construct(
3838
}
3939

4040
/**
41+
* Validate response schema definition for topic
42+
*
4143
* @param string $responseSchema
4244
* @param string $topicName
4345
* @return void
@@ -46,6 +48,12 @@ public function validateResponseSchemaType($responseSchema, $topicName)
4648
{
4749
try {
4850
$this->validateType($responseSchema);
51+
} catch (\InvalidArgumentException $e) {
52+
throw new \LogicException(
53+
'Response schema definition has service class with wrong annotated methods',
54+
$e->getCode(),
55+
$e
56+
);
4957
} catch (\Exception $e) {
5058
throw new \LogicException(
5159
sprintf(
@@ -59,6 +67,8 @@ public function validateResponseSchemaType($responseSchema, $topicName)
5967
}
6068

6169
/**
70+
* Validate request schema definition for topic
71+
*
6272
* @param string $requestSchema
6373
* @param string $topicName
6474
* @return void
@@ -67,6 +77,12 @@ public function validateRequestSchemaType($requestSchema, $topicName)
6777
{
6878
try {
6979
$this->validateType($requestSchema);
80+
} catch (\InvalidArgumentException $e) {
81+
throw new \LogicException(
82+
'Request schema definition has service class with wrong annotated methods',
83+
$e->getCode(),
84+
$e
85+
);
7086
} catch (\Exception $e) {
7187
throw new \LogicException(
7288
sprintf(
@@ -80,6 +96,8 @@ public function validateRequestSchemaType($requestSchema, $topicName)
8096
}
8197

8298
/**
99+
* Validate service method specified in the definition of handler
100+
*
83101
* @param string $serviceName
84102
* @param string $methodName
85103
* @param string $handlerName
@@ -109,6 +127,7 @@ public function validateResponseHandlersType($serviceName, $methodName, $handler
109127
* @param string $typeName
110128
* @return $this
111129
* @throws \Exception In case when type is invalid
130+
* @throws \InvalidArgumentException if methods don't have annotation
112131
*/
113132
protected function validateType($typeName)
114133
{

lib/internal/Magento/Framework/Reflection/MethodsMap.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ public function getMethodReturnType($typeName, $methodName)
9494
* 'validatePassword' => 'boolean'
9595
* ]
9696
* </pre>
97+
* @throws \InvalidArgumentException if methods don't have annotation
98+
* @throws \ReflectionException for missing DocBock or invalid reflection class
9799
*/
98100
public function getMethodsMap($interfaceName)
99101
{
@@ -148,6 +150,8 @@ public function getMethodParams($serviceClassName, $serviceMethodName)
148150
*
149151
* @param string $interfaceName
150152
* @return array
153+
* @throws \ReflectionException for missing DocBock or invalid reflection class
154+
* @throws \InvalidArgumentException if methods don't have annotation
151155
*/
152156
private function getMethodMapViaReflection($interfaceName)
153157
{
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Framework\Test\Unit\Communication\Config;
9+
10+
use Magento\Framework\Communication\Config\Validator;
11+
use Magento\Framework\Reflection\MethodsMap;
12+
use Magento\Framework\Reflection\TypeProcessor;
13+
14+
/**
15+
* Unit test for \Magento\Framework\Communication\Config\Validator class
16+
*/
17+
class ValidatorTest extends \PHPUnit\Framework\TestCase
18+
{
19+
/**
20+
* @var TypeProcessor|\PHPUnit_Framework_MockObject_MockObject
21+
*/
22+
protected $typeProcessor;
23+
24+
/**
25+
* @var MethodsMap|\PHPUnit_Framework_MockObject_MockObject
26+
*/
27+
protected $methodsMap;
28+
29+
public function setUp()
30+
{
31+
$this->methodsMap = $this->createMock(MethodsMap::class);
32+
33+
$this->methodsMap->expects(static::any())
34+
->method('getMethodsMap')
35+
->will($this->throwException(new \InvalidArgumentException('message', 333)));
36+
37+
$this->typeProcessor = $this->createMock(TypeProcessor::class);
38+
$this->typeProcessor->expects(static::any())
39+
->method('isTypeSimple')
40+
->willReturn(false);
41+
42+
$this->typeProcessor->expects(static::any())
43+
->method('isTypeSimple')
44+
->willReturn(false);
45+
}
46+
47+
/**
48+
* @expectedException \LogicException
49+
* @expectedExceptionCode 333
50+
* @expectedExceptionMessage Response schema definition has service class with wrong annotated methods
51+
*/
52+
public function testValidateResponseSchemaType()
53+
{
54+
/** @var Validator $validator */
55+
$validator = new Validator($this->typeProcessor, $this->methodsMap);
56+
$validator->validateResponseSchemaType('123', '123');
57+
}
58+
59+
/**
60+
* @expectedException \LogicException
61+
* @expectedExceptionCode 333
62+
* @expectedExceptionMessage Request schema definition has service class with wrong annotated methods
63+
*/
64+
public function testValidateRequestSchemaType()
65+
{
66+
/** @var Validator $validator */
67+
$validator = new Validator($this->typeProcessor, $this->methodsMap);
68+
$validator->validateRequestSchemaType('123', '123');
69+
}
70+
}

0 commit comments

Comments
 (0)