123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Webapi\Test\Unit\Model;
- use Magento\Framework\Serialize\SerializerInterface;
- use Magento\Webapi\Model\Config;
- use Magento\Webapi\Model\Cache\Type\Webapi;
- use Magento\Webapi\Model\Config\ClassReflector;
- use Magento\Framework\Reflection\TypeProcessor;
- use Magento\Webapi\Model\ServiceMetadata;
- use Magento\Customer\Api\CustomerRepositoryInterface;
- class ServiceMetadataTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var ServiceMetadata
- */
- private $serviceMetadata;
- /**
- * @var Webapi|\PHPUnit_Framework_MockObject_MockObject
- */
- private $cacheMock;
- /**
- * @var Config|\PHPUnit_Framework_MockObject_MockObject
- */
- private $configMock;
- /**
- * @var ClassReflector|\PHPUnit_Framework_MockObject_MockObject
- */
- private $classReflectorMock;
- /**
- * @var TypeProcessor|\PHPUnit_Framework_MockObject_MockObject
- */
- private $typeProcessorMock;
- /**
- * @var SerializerInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $serializerMock;
- protected function setUp()
- {
- $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
- $this->configMock = $this->createMock(Config::class);
- $this->cacheMock = $this->createMock(Webapi::class);
- $this->classReflectorMock = $this->createMock(ClassReflector::class);
- $this->typeProcessorMock = $this->createMock(TypeProcessor::class);
- $this->serializerMock = $this->createMock(SerializerInterface::class);
- $this->serviceMetadata = $objectManager->getObject(
- ServiceMetadata::class,
- [
- 'config' => $this->configMock,
- 'cache' => $this->cacheMock,
- 'classReflector' => $this->classReflectorMock,
- 'typeProcessor' => $this->typeProcessorMock,
- 'serializer' => $this->serializerMock
- ]
- );
- }
- public function testGetServicesConfig()
- {
- $servicesConfig = ['foo' => 'bar'];
- $typeData = ['bar' => 'foo'];
- $serializedServicesConfig = 'serialized services config';
- $serializedTypeData = 'serialized type data';
- $this->cacheMock->expects($this->at(0))
- ->method('load')
- ->with(ServiceMetadata::SERVICES_CONFIG_CACHE_ID)
- ->willReturn($serializedServicesConfig);
- $this->cacheMock->expects($this->at(1))
- ->method('load')
- ->with(ServiceMetadata::REFLECTED_TYPES_CACHE_ID)
- ->willReturn($serializedTypeData);
- $this->serializerMock->expects($this->at(0))
- ->method('unserialize')
- ->with($serializedServicesConfig)
- ->willReturn($servicesConfig);
- $this->serializerMock->expects($this->at(1))
- ->method('unserialize')
- ->with($serializedTypeData)
- ->willReturn($typeData);
- $this->typeProcessorMock->expects($this->once())
- ->method('setTypesData')
- ->with($typeData);
- $this->serviceMetadata->getServicesConfig();
- $this->assertEquals($servicesConfig, $this->serviceMetadata->getServicesConfig());
- }
- /**
- * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
- */
- public function testGetServicesConfigNoCache()
- {
- $servicesConfig = [
- 'services' => [
- CustomerRepositoryInterface::class => [
- 'V1' => [
- 'methods' => [
- 'getById' => [
- 'resources' => [
- [
- 'Magento_Customer::customer',
- ]
- ],
- 'secure' => false
- ]
- ]
- ]
- ]
- ]
- ];
- $methodsReflectionData = [
- 'getById' => [
- 'documentation' => 'Get customer by customer ID.',
- 'interface' => [
- 'in' => [
- 'parameters' => [
- 'customerId' => [
- 'type' => 'int',
- 'required' => true,
- 'documentation' => null
- ]
- ]
- ],
- 'out' => [
- 'parameters' => [
- 'result' => [
- 'type' => 'CustomerDataCustomerInterface',
- 'required' => true,
- 'documentation' => null
- ]
- ]
- ]
- ]
- ]
- ];
- $servicesMetadata = [
- 'customerCustomerRepositoryV1' => [
- 'methods' => array_merge_recursive(
- [
- 'getById' => [
- 'resources' => [
- [
- 'Magento_Customer::customer',
- ],
- ],
- 'method' => 'getById',
- 'inputRequired' => false,
- 'isSecure' => false,
- ]
- ],
- $methodsReflectionData
- ),
- 'class' => CustomerRepositoryInterface::class,
- 'description' => 'Customer CRUD interface.'
- ]
- ];
- $typeData = [
- 'CustomerDataCustomerInterface' => [
- 'documentation' => 'Customer interface.',
- 'parameters' => [
- 'id' => [
- 'type' => 'int',
- 'required' => false,
- 'documentation' => 'Customer id'
- ]
- ]
- ]
- ];
- $serializedServicesConfig = 'serialized services config';
- $serializedTypeData = 'serialized type data';
- $this->cacheMock->expects($this->at(0))
- ->method('load')
- ->with(ServiceMetadata::SERVICES_CONFIG_CACHE_ID)
- ->willReturn(false);
- $this->cacheMock->expects($this->at(1))
- ->method('load')
- ->with(ServiceMetadata::REFLECTED_TYPES_CACHE_ID)
- ->willReturn(false);
- $this->serializerMock->expects($this->never())
- ->method('unserialize');
- $this->configMock->expects($this->once())
- ->method('getServices')
- ->willReturn($servicesConfig);
- $this->classReflectorMock->expects($this->once())
- ->method('reflectClassMethods')
- ->willReturn($methodsReflectionData);
- $this->classReflectorMock->expects($this->once())
- ->method('extractClassDescription')
- ->with(CustomerRepositoryInterface::class)
- ->willReturn('Customer CRUD interface.');
- $this->typeProcessorMock->expects($this->once())
- ->method('getTypesData')
- ->willReturn($typeData);
- $this->serializerMock->expects($this->at(0))
- ->method('serialize')
- ->with($servicesMetadata)
- ->willReturn($serializedServicesConfig);
- $this->serializerMock->expects($this->at(1))
- ->method('serialize')
- ->with($typeData)
- ->willReturn($serializedTypeData);
- $this->cacheMock->expects($this->at(2))
- ->method('save')
- ->with(
- $serializedServicesConfig,
- ServiceMetadata::SERVICES_CONFIG_CACHE_ID
- );
- $this->cacheMock->expects($this->at(3))
- ->method('save')
- ->with(
- $serializedTypeData,
- ServiceMetadata::REFLECTED_TYPES_CACHE_ID
- );
- $this->serviceMetadata->getServicesConfig();
- $this->assertEquals($servicesMetadata, $this->serviceMetadata->getServicesConfig());
- }
- public function testGetRoutesConfig()
- {
- $routesConfig = ['foo' => 'bar'];
- $typeData = ['bar' => 'foo'];
- $serializedRoutesConfig = 'serialized routes config';
- $serializedTypeData = 'serialized type data';
- $this->cacheMock->expects($this->at(0))
- ->method('load')
- ->with(ServiceMetadata::ROUTES_CONFIG_CACHE_ID)
- ->willReturn($serializedRoutesConfig);
- $this->cacheMock->expects($this->at(1))
- ->method('load')
- ->with(ServiceMetadata::REFLECTED_TYPES_CACHE_ID)
- ->willReturn($serializedTypeData);
- $this->serializerMock->expects($this->at(0))
- ->method('unserialize')
- ->with($serializedRoutesConfig)
- ->willReturn($routesConfig);
- $this->serializerMock->expects($this->at(1))
- ->method('unserialize')
- ->with($serializedTypeData)
- ->willReturn($typeData);
- $this->typeProcessorMock->expects($this->once())
- ->method('setTypesData')
- ->with($typeData);
- $this->serviceMetadata->getRoutesConfig();
- $this->assertEquals($routesConfig, $this->serviceMetadata->getRoutesConfig());
- }
- /**
- * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
- */
- public function testGetRoutesConfigNoCache()
- {
- $servicesConfig = [
- 'services' => [
- CustomerRepositoryInterface::class => [
- 'V1' => [
- 'methods' => [
- 'getById' => [
- 'resources' => [
- [
- 'Magento_Customer::customer',
- ]
- ],
- 'secure' => false
- ]
- ]
- ]
- ]
- ],
- 'routes' => [
- '/V1/customers/:customerId' => [
- 'GET' => [
- 'secure' => false,
- 'service' => [
- 'class' => CustomerRepositoryInterface::class,
- 'method' => 'getById'
- ],
- 'resources' => [
- 'Magento_Customer::customer' => true
- ],
- 'parameters' => []
- ]
- ]
- ],
- 'class' => CustomerRepositoryInterface::class,
- 'description' => 'Customer CRUD interface.',
- ];
- $methodsReflectionData = [
- 'getById' => [
- 'documentation' => 'Get customer by customer ID.',
- 'interface' => [
- 'in' => [
- 'parameters' => [
- 'customerId' => [
- 'type' => 'int',
- 'required' => true,
- 'documentation' => null
- ]
- ]
- ],
- 'out' => [
- 'parameters' => [
- 'result' => [
- 'type' => 'CustomerDataCustomerInterface',
- 'required' => true,
- 'documentation' => null
- ]
- ]
- ]
- ]
- ]
- ];
- $routesMetadata = [
- 'customerCustomerRepositoryV1' => [
- 'methods' => array_merge_recursive(
- [
- 'getById' => [
- 'resources' => [
- [
- 'Magento_Customer::customer',
- ]
- ],
- 'method' => 'getById',
- 'inputRequired' => false,
- 'isSecure' => false,
- ]
- ],
- $methodsReflectionData
- ),
- 'routes' => [
- '/V1/customers/:customerId' => [
- 'GET' => [
- 'method' => 'getById',
- 'parameters' => []
- ]
- ]
- ],
- 'class' => CustomerRepositoryInterface::class,
- 'description' => 'Customer CRUD interface.'
- ]
- ];
- $typeData = [
- 'CustomerDataCustomerInterface' => [
- 'documentation' => 'Customer interface.',
- 'parameters' => [
- 'id' => [
- 'type' => 'int',
- 'required' => false,
- 'documentation' => 'Customer id'
- ]
- ]
- ]
- ];
- $serializedRoutesConfig = 'serialized routes config';
- $serializedTypeData = 'serialized type data';
- $this->cacheMock->expects($this->at(0))
- ->method('load')
- ->with(ServiceMetadata::ROUTES_CONFIG_CACHE_ID)
- ->willReturn(false);
- $this->cacheMock->expects($this->at(1))
- ->method('load')
- ->with(ServiceMetadata::REFLECTED_TYPES_CACHE_ID)
- ->willReturn(false);
- $this->serializerMock->expects($this->never())
- ->method('unserialize');
- $this->configMock->expects($this->exactly(2))
- ->method('getServices')
- ->willReturn($servicesConfig);
- $this->classReflectorMock->expects($this->once())
- ->method('reflectClassMethods')
- ->willReturn($methodsReflectionData);
- $this->classReflectorMock->expects($this->once())
- ->method('extractClassDescription')
- ->with(CustomerRepositoryInterface::class)
- ->willReturn('Customer CRUD interface.');
- $this->typeProcessorMock->expects($this->exactly(2))
- ->method('getTypesData')
- ->willReturn($typeData);
- $this->serializerMock->expects($this->at(2))
- ->method('serialize')
- ->with($routesMetadata)
- ->willReturn($serializedRoutesConfig);
- $this->serializerMock->expects($this->at(3))
- ->method('serialize')
- ->with($typeData)
- ->willReturn($serializedTypeData);
- $this->cacheMock->expects($this->at(6))
- ->method('save')
- ->with(
- $serializedRoutesConfig,
- ServiceMetadata::ROUTES_CONFIG_CACHE_ID
- );
- $this->cacheMock->expects($this->at(7))
- ->method('save')
- ->with(
- $serializedTypeData,
- ServiceMetadata::REFLECTED_TYPES_CACHE_ID
- );
- $this->serviceMetadata->getRoutesConfig();
- $this->assertEquals($routesMetadata, $this->serviceMetadata->getRoutesConfig());
- }
- /**
- * @dataProvider getServiceNameDataProvider
- */
- public function testGetServiceName($className, $version, $preserveVersion, $expected)
- {
- $this->assertEquals(
- $expected,
- $this->serviceMetadata->getServiceName($className, $version, $preserveVersion)
- );
- }
- /**
- * @return string
- */
- public function getServiceNameDataProvider()
- {
- return [
- [
- \Magento\Customer\Api\AccountManagementInterface::class,
- 'V1',
- false,
- 'customerAccountManagement'
- ],
- [
- \Magento\Customer\Api\AddressRepositoryInterface::class,
- 'V1',
- true,
- 'customerAddressRepositoryV1'
- ],
- ];
- }
- /**
- * @expectedException \InvalidArgumentException
- * @dataProvider getServiceNameInvalidNameDataProvider
- */
- public function testGetServiceNameInvalidName($interfaceClassName, $version)
- {
- $this->serviceMetadata->getServiceName($interfaceClassName, $version);
- }
- /**
- * @return string
- */
- public function getServiceNameInvalidNameDataProvider()
- {
- return [
- ['BarV1Interface', 'V1'], // Missed vendor, module and Service
- ['Service\\V1Interface', 'V1'], // Missed vendor and module
- ['Magento\\Foo\\Service\\BarVxInterface', 'V1'], // Version number should be a number
- ['Magento\\Foo\\Service\\BarInterface', 'V1'], // Missed version
- ['Magento\\Foo\\Service\\BarV1', 'V1'], // Missed Interface
- ['Foo\\Service\\BarV1Interface', 'V1'], // Missed module
- ['Foo\\BarV1Interface', 'V1'] // Missed module and Service
- ];
- }
- }
|