Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
RemoteServiceGeneratorTest.php
Go to the documentation of this file.
1 <?php
7 
8 use Composer\Autoload\ClassLoader;
11 use Magento\Framework\Communication\ConfigInterface as CommunicationConfigInterface;
17 use PHPUnit\Framework\MockObject_MockObject as MockObject;
18 
22 class RemoteServiceGeneratorTest extends \PHPUnit\Framework\TestCase
23 {
27  private $communicationConfig;
28 
32  private $generator;
33 
37  private $objectManager;
38 
42  protected function setUp()
43  {
44  $this->objectManager = new ObjectManager($this);
45 
46  $this->communicationConfig = $this->getMockBuilder(CommunicationConfigInterface::class)
47  ->disableOriginalConstructor()
48  ->getMock();
49 
50  $loader = new ClassLoader();
51  $loader->addPsr4(
52  'Magento\\Framework\\MessageQueue\\Code\\Generator\\',
53  __DIR__ . '/_files'
54  );
55  $loader->register();
56  }
57 
68  public function testGenerate($sourceClassName, $resultClassName, $topicName, $fileName)
69  {
70  $this->createGenerator($sourceClassName, $resultClassName);
71 
72  $this->communicationConfig->method('getTopic')
73  ->willReturnMap(
74  [
75  [$topicName . '.save', [CommunicationConfigInterface::TOPIC_IS_SYNCHRONOUS => true]],
76  [$topicName . '.get', [CommunicationConfigInterface::TOPIC_IS_SYNCHRONOUS => true]],
77  [$topicName . '.getById', [CommunicationConfigInterface::TOPIC_IS_SYNCHRONOUS => true]],
78  [$topicName . '.getList', [CommunicationConfigInterface::TOPIC_IS_SYNCHRONOUS => true]],
79  [$topicName . '.delete', [CommunicationConfigInterface::TOPIC_IS_SYNCHRONOUS => true]],
80  [$topicName . '.deleteById', [CommunicationConfigInterface::TOPIC_IS_SYNCHRONOUS => true]],
81  ]
82  );
83  $expectedResult = file_get_contents(__DIR__ . '/_files/' . $fileName);
84  $this->validateGeneratedCode($expectedResult);
85  }
86 
92  public function interfaceDataProvider()
93  {
94  return [
95  [
96  '\\' . \Magento\Customer\Api\CustomerRepositoryInterface::class,
97  '\\' . \Magento\Customer\Api\CustomerRepositoryInterfaceRemote::class,
98  'magento.customer.api.customerRepositoryInterface',
99  'RemoteService.txt'
100  ],
101  [
102  '\\' . \Magento\Framework\MessageQueue\Code\Generator\TRepositoryInterface::class,
103  '\\' . \Magento\Framework\MessageQueue\Code\Generator\TRepositoryInterfaceRemote::class,
104  'magento.framework.messageQueue.code.generator.tRepositoryInterface',
105  'TRemoteService.txt'
106  ]
107  ];
108  }
109 
116  private function validateGeneratedCode($expectedResult)
117  {
118  $reflectionObject = new \ReflectionObject($this->generator);
119  $reflectionMethod = $reflectionObject->getMethod('_generateCode');
120  $reflectionMethod->setAccessible(true);
121  $generatedCode = $reflectionMethod->invoke($this->generator);
122  self::assertEquals($expectedResult, $generatedCode);
123  }
124 
131  private function createGenerator($sourceClassName, $resultClassName)
132  {
133  $methodMap = $this->createMethodMap();
134  $this->generator = $this->objectManager->getObject(
135  RemoteServiceGenerator::class,
136  [
137  'communicationConfig' => $this->communicationConfig,
138  'serviceMethodsMap' => $methodMap,
139  'sourceClassName' => $sourceClassName,
140  'resultClassName' => $resultClassName,
141  'classGenerator' => null
142  ]
143  );
144 
145  $reflectionGenerator = $this->objectManager->getObject(ReflectionGenerator::class);
146  $this->objectManager->setBackwardCompatibleProperty(
147  $this->generator,
148  'reflectionGenerator',
149  $reflectionGenerator
150  );
151  }
152 
158  private function createMethodMap()
159  {
160  $cache = $this->getMockBuilder(FrontendInterface::class)
161  ->disableOriginalConstructor()
162  ->getMock();
163  $cache->method('load')
164  ->willReturn(false);
165 
166  $serializer = $this->getMockBuilder(SerializerInterface::class)
167  ->disableOriginalConstructor()
168  ->getMock();
169  $typeProcessor = $this->objectManager->getObject(TypeProcessor::class);
170 
172  $serviceMethodMap = $this->objectManager->getObject(MethodsMap::class, [
173  'cache' => $cache,
174  'typeProcessor' => $typeProcessor
175  ]);
176  $this->objectManager->setBackwardCompatibleProperty(
177  $serviceMethodMap,
178  'serializer',
180  );
181 
182  return $serviceMethodMap;
183  }
184 }
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60
$loader
Definition: autoload.php:8
$fileName
Definition: translate.phtml:15
$generatedCode
Definition: autoload.php:24