Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
InterfaceGeneratorTest.php
Go to the documentation of this file.
1 <?php
7 
8 class InterfaceGeneratorTest extends \PHPUnit\Framework\TestCase
9 {
14 
20  protected $interfaceDocBlock = [
21  'shortDescription' => 'Interface short description.',
22  'longDescription' => "Interface long\ndescription.",
23  'tags' => [
24  'tag1' => ['name' => 'tag1', 'description' => 'data1'],
25  'tag2' => ['name' => 'tag2', 'description' => 'data2'],
26  ],
27  ];
28 
34  protected $methodsData = [
35  'testMethod1' => [
36  'name' => 'testMethod1',
37  'static' => true,
38  'parameters' => [
39  ['name' => 'data', 'type' => 'array', 'defaultValue' => [], 'passedByReference' => true],
40  ],
41  'docblock' => [
42  'shortDescription' => 'Method short description',
43  'tags' => [
44  ['name' => 'param', 'description' => 'array $data'],
45  ['name' => 'return', 'description' => 'TestThree'],
46  ],
47  ],
48  ],
49  'testMethod2' => [
50  'name' => 'testMethod2',
51  'parameters' => [
52  ['name' => 'data', 'defaultValue' => 'test_default'],
53  ['name' => 'flag', 'defaultValue' => true],
54  ],
55  'docblock' => [
56  'shortDescription' => 'Method short description',
57  'longDescription' => "Method long\ndescription",
58  'tags' => [
59  ['name' => 'param', 'description' => 'string $data'],
60  ['name' => 'param', 'description' => 'bool $flag'],
61  ],
62  ],
63  ],
64  'testMethod3' => ['name' => 'testMethod3'],
65  ];
66 
67  protected function setUp()
68  {
69  $this->interfaceGenerator = new \Magento\Framework\Code\Generator\InterfaceGenerator();
70  }
71 
75  public function testGenerate($additionalMethodsData, $expectedException, $expectedExceptionMessage)
76  {
77  if ($expectedException) {
78  $this->expectException($expectedException);
79  $this->expectExceptionMessage($expectedExceptionMessage);
80  }
81  $methodsData = array_merge_recursive($this->methodsData, $additionalMethodsData);
82  $this->interfaceGenerator->setClassDocBlock($this->interfaceDocBlock)
83  ->addMethods($methodsData)
84  ->setName('SevenInterface')
85  ->setNamespaceName(\Magento\SomeModule\Model::class)
86  ->addUse(\Magento\SomeModule\Model\Two\Test::class, 'TestTwo')
87  ->addUse(\Magento\SomeModule\Model\Three\Test::class, 'TestThree')
88  ->setExtendedClass(\Magento\Framework\Code\Generator\CodeGeneratorInterface::class);
89  $generatedInterface = $this->interfaceGenerator->generate();
90  $expectedInterface = file_get_contents(
91  __DIR__ . '/../_files/app/code/Magento/SomeModule/Model/SevenInterface.php'
92  );
93 
94  $this->assertStringEndsWith(
95  $generatedInterface,
96  $expectedInterface,
97  "Interface was generated incorrectly."
98  );
99  }
100 
102  {
103  $expectedContent = 'Expected generated content.';
104  $this->interfaceGenerator->setSourceDirty(false)->setSourceContent($expectedContent);
105  $generatedContent = $this->interfaceGenerator->generate();
106  $this->assertEquals($expectedContent, $generatedContent, "Generated content is invalid.");
107  }
108 
110  {
111  $expectedContent = '';
112  $this->interfaceGenerator->setSourceDirty(false);
113  $generatedContent = $this->interfaceGenerator->generate();
114  $this->assertEquals($expectedContent, $generatedContent, "Generated content is invalid.");
115  }
116 
120  public function generateDataProvider()
121  {
122  return [
123  'Valid data' => [
124  'additionalMethodsData' => [],
125  'expectedException' => '',
126  'expectedExceptionMessage' => ''
127  ],
128  '"final" usage exception' => [
129  'additionalMethodsData' => ['testMethod1' => ['final' => true]],
130  'expectedException' => '\LogicException',
131  'expectedExceptionMessage' => "Interface method cannot be marked as 'final'. Method name: 'testMethod1'"
132  ],
133  'Non public interface method exception' => [
134  'additionalMethodsData' => ['testMethod2' => ['visibility' => 'protected']],
135  'expectedException' => '\LogicException',
136  'expectedExceptionMessage' =>
137  "Interface method visibility can only be 'public'. Method name: 'testMethod2'"
138  ],
139  '"abstract" usage exception' => [
140  'additionalMethodsData' => ['testMethod1' => ['abstract' => true]],
141  'expectedException' => '\LogicException',
142  'expectedExceptionMessage' =>
143  "'abstract' modifier cannot be used for interface method. Method name: 'testMethod1'"
144  ],
145  ];
146  }
147 }
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60
testGenerate($additionalMethodsData, $expectedException, $expectedExceptionMessage)