|
| 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 | +}); |
0 commit comments