Skip to content
19 changes: 19 additions & 0 deletions lib/internal/Magento/Framework/Communication/Config/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public function __construct(
}

/**
* Validate response schema definition for topic
*
* @param string $responseSchema
* @param string $topicName
* @return void
Expand All @@ -46,6 +48,12 @@ public function validateResponseSchemaType($responseSchema, $topicName)
{
try {
$this->validateType($responseSchema);
} catch (\InvalidArgumentException $e) {
throw new \LogicException(
'Response schema definition has service class with wrong annotated methods',
$e->getCode(),
$e
);
} catch (\Exception $e) {
throw new \LogicException(
sprintf(
Expand All @@ -59,6 +67,8 @@ public function validateResponseSchemaType($responseSchema, $topicName)
}

/**
* Validate request schema definition for topic
*
* @param string $requestSchema
* @param string $topicName
* @return void
Expand All @@ -67,6 +77,12 @@ public function validateRequestSchemaType($requestSchema, $topicName)
{
try {
$this->validateType($requestSchema);
} catch (\InvalidArgumentException $e) {
throw new \LogicException(
'Request schema definition has service class with wrong annotated methods',
$e->getCode(),
$e
);
} catch (\Exception $e) {
throw new \LogicException(
sprintf(
Expand All @@ -80,6 +96,8 @@ public function validateRequestSchemaType($requestSchema, $topicName)
}

/**
* Validate service method specified in the definition of handler
*
* @param string $serviceName
* @param string $methodName
* @param string $handlerName
Expand Down Expand Up @@ -109,6 +127,7 @@ public function validateResponseHandlersType($serviceName, $methodName, $handler
* @param string $typeName
* @return $this
* @throws \Exception In case when type is invalid
* @throws \InvalidArgumentException if methods don't have annotation
*/
protected function validateType($typeName)
{
Expand Down
4 changes: 4 additions & 0 deletions lib/internal/Magento/Framework/Reflection/MethodsMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ public function getMethodReturnType($typeName, $methodName)
* 'validatePassword' => 'boolean'
* ]
* </pre>
* @throws \InvalidArgumentException if methods don't have annotation
* @throws \ReflectionException for missing DocBock or invalid reflection class
*/
public function getMethodsMap($interfaceName)
{
Expand Down Expand Up @@ -148,6 +150,8 @@ public function getMethodParams($serviceClassName, $serviceMethodName)
*
* @param string $interfaceName
* @return array
* @throws \ReflectionException for missing DocBock or invalid reflection class
* @throws \InvalidArgumentException if methods don't have annotation
*/
private function getMethodMapViaReflection($interfaceName)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Framework\Test\Unit\Communication\Config;

use Magento\Framework\Communication\Config\Validator;
use Magento\Framework\Reflection\MethodsMap;
use Magento\Framework\Reflection\TypeProcessor;

/**
* Unit test for \Magento\Framework\Communication\Config\Validator class
*/
class ValidatorTest extends \PHPUnit\Framework\TestCase
{
/**
* @var TypeProcessor|\PHPUnit_Framework_MockObject_MockObject
*/
protected $typeProcessor;

/**
* @var MethodsMap|\PHPUnit_Framework_MockObject_MockObject
*/
protected $methodsMap;

public function setUp()
{
$this->methodsMap = $this->createMock(MethodsMap::class);

$this->methodsMap->expects(static::any())
->method('getMethodsMap')
->will($this->throwException(new \InvalidArgumentException('message', 333)));

$this->typeProcessor = $this->createMock(TypeProcessor::class);
$this->typeProcessor->expects(static::any())
->method('isTypeSimple')
->willReturn(false);

$this->typeProcessor->expects(static::any())
->method('isTypeSimple')
->willReturn(false);
}

/**
* @expectedException \LogicException
* @expectedExceptionCode 333
* @expectedExceptionMessage Response schema definition has service class with wrong annotated methods
*/
public function testValidateResponseSchemaType()
{
/** @var Validator $validator */
$validator = new Validator($this->typeProcessor, $this->methodsMap);
$validator->validateResponseSchemaType('123', '123');
}

/**
* @expectedException \LogicException
* @expectedExceptionCode 333
* @expectedExceptionMessage Request schema definition has service class with wrong annotated methods
*/
public function testValidateRequestSchemaType()
{
/** @var Validator $validator */
$validator = new Validator($this->typeProcessor, $this->methodsMap);
$validator->validateRequestSchemaType('123', '123');
}
}