Skip to content

Commit 503fea6

Browse files
committed
chore: plugin generator tests
1 parent 6852a46 commit 503fea6

File tree

14 files changed

+290
-9
lines changed

14 files changed

+290
-9
lines changed

src/generator/plugin/PluginClassGenerator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export default class PluginClassGenerator extends FileGenerator {
7575

7676
const printer = new PsrPrinter();
7777
return new GeneratedFile(
78-
Uri.joinPath(moduleDirectory, 'Plugin', `${pluginName}.php`),
78+
Uri.joinPath(moduleDirectory, 'Plugin', ...nameParts, `${pluginName}.php`),
7979
printer.printFile(phpFile)
8080
);
8181
}

src/test/generator/observer/ObserverEventsGenerator.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,6 @@ describe('ObserverEventsGenerator Tests', () => {
110110
// Get the reference file content
111111
const referenceContent = getReferenceFile('generator/observer/events-with-comment.xml');
112112

113-
console.log(generatedFile.content, referenceContent);
114-
115113
// Compare the generated content with reference
116114
assert.strictEqual(generatedFile.content, referenceContent);
117115
});
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import { PluginContextWizardData } from 'wizard/PluginContextWizard';
2+
import * as assert from 'assert';
3+
import { Uri } from 'vscode';
4+
import PluginClassGenerator from 'generator/plugin/PluginClassGenerator';
5+
import { describe, it, before, afterEach } from 'mocha';
6+
import { setup } from 'test/setup';
7+
import { getReferenceFile, getReferenceFilePath, getTestWorkspaceUri } from 'test/util';
8+
import FileHeader from 'common/php/FileHeader';
9+
import sinon from 'sinon';
10+
import { PhpClass } from 'parser/php/PhpClass';
11+
import { PhpMethod } from 'parser/php/PhpMethod';
12+
import { MagentoScope } from 'types';
13+
import PhpParser from 'parser/php/Parser';
14+
15+
describe('PluginClassGenerator Tests', () => {
16+
const pluginWizardData: PluginContextWizardData = {
17+
module: 'Foo_Bar',
18+
className: 'TestPlugin',
19+
name: 'test_plugin',
20+
type: 'around',
21+
method: 'setData',
22+
sortOrder: 10,
23+
scope: MagentoScope.Global,
24+
};
25+
26+
let subjectClass: PhpClass;
27+
let subjectMethod: PhpMethod;
28+
29+
before(async () => {
30+
await setup();
31+
32+
const parser = new PhpParser();
33+
const referenceFilePath = getReferenceFilePath('generator/plugin/SubjectClass.php');
34+
const phpFile = await parser.parse(Uri.file(referenceFilePath));
35+
subjectClass = phpFile.classes[0];
36+
subjectMethod = subjectClass.methods[0];
37+
});
38+
39+
afterEach(() => {
40+
sinon.restore();
41+
});
42+
43+
it('should generate plugin class file', async () => {
44+
// Mock the FileHeader.getHeader method to return a consistent header
45+
sinon.stub(FileHeader, 'getHeader').returns('Foo_Bar');
46+
47+
// Create the generator with test data
48+
const generator = new PluginClassGenerator(pluginWizardData, subjectClass, subjectMethod);
49+
50+
// Use a test workspace URI
51+
const workspaceUri = getTestWorkspaceUri();
52+
53+
// Generate the file
54+
const generatedFile = await generator.generate(workspaceUri);
55+
56+
// Get the reference file content
57+
const referenceContent = getReferenceFile('generator/plugin/TestPlugin.php');
58+
59+
// Compare the generated content with reference
60+
assert.strictEqual(generatedFile.content, referenceContent);
61+
});
62+
63+
it('should generate file in correct location', async () => {
64+
// Create the generator with test data
65+
const generator = new PluginClassGenerator(pluginWizardData, subjectClass, subjectMethod);
66+
67+
// Use a test workspace URI
68+
const workspaceUri = getTestWorkspaceUri();
69+
70+
// Generate the file
71+
const generatedFile = await generator.generate(workspaceUri);
72+
73+
// Expected path
74+
const expectedPath = Uri.joinPath(
75+
workspaceUri,
76+
'app/code/Foo/Bar/Plugin/TestPlugin.php'
77+
).fsPath;
78+
79+
assert.strictEqual(generatedFile.uri.fsPath, expectedPath);
80+
});
81+
82+
it('should generate plugin in custom path', async () => {
83+
// Create test data with custom path
84+
const customPathData: PluginContextWizardData = {
85+
...pluginWizardData,
86+
className: 'Custom/Path/TestPlugin',
87+
};
88+
89+
// Mock the FileHeader.getHeader method to return a consistent header
90+
sinon.stub(FileHeader, 'getHeader').returns('Foo_Bar');
91+
92+
// Create the generator with custom path data
93+
const generator = new PluginClassGenerator(customPathData, subjectClass, subjectMethod);
94+
95+
// Use a test workspace URI
96+
const workspaceUri = getTestWorkspaceUri();
97+
98+
// Generate the file
99+
const generatedFile = await generator.generate(workspaceUri);
100+
101+
// Get the reference file content
102+
const referenceContent = getReferenceFile('generator/plugin/TestPluginCustomPath.php');
103+
104+
// Compare the generated content with reference
105+
assert.strictEqual(generatedFile.content, referenceContent);
106+
107+
// Verify file location
108+
const expectedPath = Uri.joinPath(
109+
workspaceUri,
110+
'app/code/Foo/Bar/Plugin/Custom/Path/TestPlugin.php'
111+
).fsPath;
112+
113+
assert.strictEqual(generatedFile.uri.fsPath, expectedPath);
114+
});
115+
});
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import { PluginContextWizardData } from 'wizard/PluginContextWizard';
2+
import * as assert from 'assert';
3+
import { Uri } from 'vscode';
4+
import PluginDiGenerator from 'generator/plugin/PluginDiGenerator';
5+
import { describe, it, before } from 'mocha';
6+
import { setup } from 'test/setup';
7+
import { getReferenceFile, getReferenceFilePath, getTestWorkspaceUri } from 'test/util';
8+
import { MagentoScope } from 'types';
9+
import { PhpClass } from 'parser/php/PhpClass';
10+
import PhpParser from 'parser/php/Parser';
11+
import { PhpMethod } from 'parser/php/PhpMethod';
12+
13+
describe('PluginDiGenerator Tests', () => {
14+
const pluginWizardData: PluginContextWizardData = {
15+
module: 'Foo_Bar',
16+
className: 'TestPlugin',
17+
name: 'test_plugin',
18+
type: 'around',
19+
method: 'setData',
20+
sortOrder: 10,
21+
scope: MagentoScope.Global,
22+
};
23+
24+
let subjectClass: PhpClass;
25+
let subjectMethod: PhpMethod;
26+
27+
before(async () => {
28+
await setup();
29+
30+
const parser = new PhpParser();
31+
const referenceFilePath = getReferenceFilePath('generator/plugin/SubjectClass.php');
32+
const phpFile = await parser.parse(Uri.file(referenceFilePath));
33+
subjectClass = phpFile.classes[0];
34+
subjectMethod = subjectClass.methods[0];
35+
});
36+
37+
it('should generate di.xml', async () => {
38+
// Create the generator with test data
39+
const generator = new PluginDiGenerator(pluginWizardData, subjectClass, subjectMethod);
40+
41+
// Use a test workspace URI
42+
const workspaceUri = getTestWorkspaceUri();
43+
44+
// Generate the file
45+
const generatedFile = await generator.generate(workspaceUri);
46+
47+
// Get the reference file content
48+
const referenceContent = getReferenceFile('generator/plugin/di.xml');
49+
50+
// Compare the generated content with reference
51+
assert.strictEqual(generatedFile.content, referenceContent);
52+
});
53+
54+
it('should generate file in correct location', async () => {
55+
// Create the generator with test data
56+
const generator = new PluginDiGenerator(pluginWizardData, subjectClass, subjectMethod);
57+
58+
// Use a test workspace URI
59+
const workspaceUri = getTestWorkspaceUri();
60+
61+
// Generate the file
62+
const generatedFile = await generator.generate(workspaceUri);
63+
64+
// Expected path
65+
const expectedPath = Uri.joinPath(workspaceUri, 'app/code/Foo/Bar/etc/di.xml').fsPath;
66+
67+
assert.strictEqual(generatedFile.uri.fsPath, expectedPath);
68+
});
69+
70+
it('should generate di.xml in adminhtml area', async () => {
71+
// Create test data with adminhtml area
72+
const adminhtmlData: PluginContextWizardData = {
73+
...pluginWizardData,
74+
scope: MagentoScope.Adminhtml,
75+
};
76+
77+
// Create the generator with adminhtml data
78+
const generator = new PluginDiGenerator(adminhtmlData, subjectClass, subjectMethod);
79+
80+
// Use a test workspace URI
81+
const workspaceUri = getTestWorkspaceUri();
82+
83+
// Generate the file
84+
const generatedFile = await generator.generate(workspaceUri);
85+
86+
// Get the reference file content
87+
const referenceContent = getReferenceFile('generator/plugin/di-adminhtml.xml');
88+
89+
// Compare the generated content with reference
90+
assert.strictEqual(generatedFile.content, referenceContent);
91+
92+
// Verify file location
93+
const expectedPath = Uri.joinPath(workspaceUri, 'app/code/Foo/Bar/etc/adminhtml/di.xml').fsPath;
94+
95+
assert.strictEqual(generatedFile.uri.fsPath, expectedPath);
96+
});
97+
});

src/test/util.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@ import fs from 'fs';
33
import path from 'path';
44
import { Uri } from 'vscode';
55

6-
export function getReferenceFile(filePath: string) {
6+
export function getReferenceFilePath(filePath: string) {
77
const resourcePath = FileSystem.getExtensionPath('test-resources');
8-
const refFilePath = path.resolve(resourcePath, 'reference', filePath);
8+
return path.resolve(resourcePath, 'reference', filePath);
9+
}
10+
11+
export function getReferenceFile(filePath: string) {
12+
const refFilePath = getReferenceFilePath(filePath);
913
return fs.readFileSync(refFilePath, 'utf8');
1014
}
1115

templates/xml/observer.ejs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<event name="<%= eventName %>">
2-
<observer name="<%= name %>" instance="<%= className %>"/>
2+
<observer name="<%= name %>" instance="<%= className %>" />
33
</event>

test-resources/reference/generator/observer/events-adminhtml.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
44

55
<event name="test_event">
6-
<observer name="test_observer" instance="Foo\Bar\Observer\TestObserver"/>
6+
<observer name="test_observer" instance="Foo\Bar\Observer\TestObserver" />
77
</event>
88
</config>

test-resources/reference/generator/observer/events-with-comment.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ Foo_Bar
66
xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
77

88
<event name="test_event">
9-
<observer name="test_observer" instance="Foo\Bar\Observer\TestObserver"/>
9+
<observer name="test_observer" instance="Foo\Bar\Observer\TestObserver" />
1010
</event>
1111
</config>

test-resources/reference/generator/observer/events.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
44

55
<event name="test_event">
6-
<observer name="test_observer" instance="Foo\Bar\Observer\TestObserver"/>
6+
<observer name="test_observer" instance="Foo\Bar\Observer\TestObserver" />
77
</event>
88
</config>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Foo\Bar\Model;
4+
5+
class SubjectClass
6+
{
7+
public function setData(array $data)
8+
{
9+
// Subject code
10+
}
11+
}

0 commit comments

Comments
 (0)