Skip to content

Commit 726601f

Browse files
committed
chore: preference generator test & namespace fixes
1 parent 503fea6 commit 726601f

File tree

7 files changed

+238
-2
lines changed

7 files changed

+238
-2
lines changed

src/generator/preference/PreferenceClassGenerator.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ export default class PreferenceClassGenerator extends FileGenerator {
1313

1414
public async generate(workspaceUri: Uri): Promise<GeneratedFile> {
1515
const [vendor, module] = this.data.module.split('_');
16-
const namespaceParts = [vendor, module, this.data.directory];
16+
const directoryParts = this.data.directory.split('/');
17+
const namespaceParts = [vendor, module, ...directoryParts];
1718
const moduleDirectory = Magento.getModuleDirectory(vendor, module, workspaceUri);
1819

1920
const header = FileHeader.getHeader(this.data.module);

src/generator/preference/PreferenceDiGenerator.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,18 @@ export default class PreferenceDiGenerator extends FileGenerator {
1414

1515
public async generate(workspaceUri: Uri): Promise<GeneratedFile> {
1616
const [vendor, module] = this.data.module.split('_');
17+
const directoryParts = this.data.directory.split('/');
18+
const namespaceParts = [vendor, module, ...directoryParts, this.data.className];
19+
const typeNamespace = namespaceParts.join('\\');
20+
1721
const etcDirectory = Magento.getModuleDirectory(vendor, module, workspaceUri, 'etc');
1822
const diFile = Magento.getUriWithArea(etcDirectory, 'di.xml', this.data.area);
1923
const diXml = await FindOrCreateDiXml.execute(workspaceUri, vendor, module, this.data.area);
2024
const insertPosition = this.getInsertPosition(diXml);
2125

2226
const pluginXml = await GenerateFromTemplate.generate('xml/preference', {
2327
forClass: this.data.parentClass,
24-
typeClass: this.data.className,
28+
typeClass: typeNamespace,
2529
});
2630

2731
const newDiXml =
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import { PreferenceWizardData } from 'wizard/PreferenceWizard';
2+
import * as assert from 'assert';
3+
import { Uri } from 'vscode';
4+
import PreferenceClassGenerator from 'generator/preference/PreferenceClassGenerator';
5+
import { describe, it, before, afterEach } from 'mocha';
6+
import { setup } from 'test/setup';
7+
import { getReferenceFile, getTestWorkspaceUri } from 'test/util';
8+
import FileHeader from 'common/php/FileHeader';
9+
import sinon from 'sinon';
10+
import { MagentoScope } from 'types';
11+
12+
describe('PreferenceClassGenerator Tests', () => {
13+
const preferenceWizardData: PreferenceWizardData = {
14+
module: 'Foo_Bar',
15+
className: 'TestPreference',
16+
directory: 'Model',
17+
parentClass: 'Magento\\Catalog\\Model\\Product',
18+
inheritClass: true,
19+
area: MagentoScope.Global,
20+
};
21+
22+
before(async () => {
23+
await setup();
24+
});
25+
26+
afterEach(() => {
27+
sinon.restore();
28+
});
29+
30+
it('should generate preference class file', async () => {
31+
// Mock the FileHeader.getHeader method to return a consistent header
32+
sinon.stub(FileHeader, 'getHeader').returns('Foo_Bar');
33+
34+
// Create the generator with test data
35+
const generator = new PreferenceClassGenerator(preferenceWizardData);
36+
37+
// Use a test workspace URI
38+
const workspaceUri = getTestWorkspaceUri();
39+
40+
// Generate the file
41+
const generatedFile = await generator.generate(workspaceUri);
42+
43+
// Get the reference file content
44+
const referenceContent = getReferenceFile('generator/preference/TestPreference.php');
45+
46+
// Compare the generated content with reference
47+
assert.strictEqual(generatedFile.content, referenceContent);
48+
});
49+
50+
it('should generate file in correct location', async () => {
51+
// Create the generator with test data
52+
const generator = new PreferenceClassGenerator(preferenceWizardData);
53+
54+
// Use a test workspace URI
55+
const workspaceUri = getTestWorkspaceUri();
56+
57+
// Generate the file
58+
const generatedFile = await generator.generate(workspaceUri);
59+
60+
// Expected path
61+
const expectedPath = Uri.joinPath(
62+
workspaceUri,
63+
'app/code/Foo/Bar/Model/TestPreference.php'
64+
).fsPath;
65+
66+
assert.strictEqual(generatedFile.uri.fsPath, expectedPath);
67+
});
68+
69+
it('should generate preference without inheritance', async () => {
70+
// Create test data without inheritance
71+
const noInheritanceData: PreferenceWizardData = {
72+
...preferenceWizardData,
73+
inheritClass: false,
74+
};
75+
76+
// Mock the FileHeader.getHeader method to return a consistent header
77+
sinon.stub(FileHeader, 'getHeader').returns('Foo_Bar');
78+
79+
// Create the generator with modified data
80+
const generator = new PreferenceClassGenerator(noInheritanceData);
81+
82+
// Use a test workspace URI
83+
const workspaceUri = getTestWorkspaceUri();
84+
85+
// Generate the file
86+
const generatedFile = await generator.generate(workspaceUri);
87+
88+
// Get the reference file content
89+
const referenceContent = getReferenceFile('generator/preference/TestPreferenceNoInherit.php');
90+
91+
// Compare the generated content with reference
92+
assert.strictEqual(generatedFile.content, referenceContent);
93+
});
94+
});
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import { PreferenceWizardData } from 'wizard/PreferenceWizard';
2+
import * as assert from 'assert';
3+
import { Uri } from 'vscode';
4+
import PreferenceDiGenerator from 'generator/preference/PreferenceDiGenerator';
5+
import { describe, it, before, afterEach } from 'mocha';
6+
import { setup } from 'test/setup';
7+
import { getReferenceFile, getTestWorkspaceUri } from 'test/util';
8+
import sinon from 'sinon';
9+
import { MagentoScope } from 'types';
10+
11+
describe('PreferenceDiGenerator Tests', () => {
12+
const preferenceWizardData: PreferenceWizardData = {
13+
module: 'Foo_Bar',
14+
className: 'TestPreference',
15+
directory: 'Model',
16+
parentClass: 'Magento\\Catalog\\Model\\Product',
17+
inheritClass: true,
18+
area: MagentoScope.Global,
19+
};
20+
21+
before(async () => {
22+
await setup();
23+
});
24+
25+
afterEach(() => {
26+
sinon.restore();
27+
});
28+
29+
it('should generate di.xml file', async () => {
30+
// Create the generator with test data
31+
const generator = new PreferenceDiGenerator(preferenceWizardData);
32+
33+
// Use a test workspace URI
34+
const workspaceUri = getTestWorkspaceUri();
35+
36+
// Generate the file
37+
const generatedFile = await generator.generate(workspaceUri);
38+
39+
// Get the reference file content
40+
const referenceContent = getReferenceFile('generator/preference/di.xml');
41+
42+
// Compare the generated content with reference
43+
assert.strictEqual(generatedFile.content, referenceContent);
44+
});
45+
46+
it('should generate file in correct location for global scope', async () => {
47+
// Create the generator with test data
48+
const generator = new PreferenceDiGenerator(preferenceWizardData);
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+
// Expected path for global scope
57+
const expectedPath = Uri.joinPath(workspaceUri, 'app/code/Foo/Bar/etc/di.xml').fsPath;
58+
59+
assert.strictEqual(generatedFile.uri.fsPath, expectedPath);
60+
});
61+
62+
it('should generate file in correct location for frontend scope', async () => {
63+
// Create test data with frontend scope
64+
const frontendData: PreferenceWizardData = {
65+
...preferenceWizardData,
66+
area: MagentoScope.Frontend,
67+
};
68+
69+
// Create the generator with frontend data
70+
const generator = new PreferenceDiGenerator(frontendData);
71+
72+
// Use a test workspace URI
73+
const workspaceUri = getTestWorkspaceUri();
74+
75+
// Generate the file
76+
const generatedFile = await generator.generate(workspaceUri);
77+
78+
// Expected path for frontend scope
79+
const expectedPath = Uri.joinPath(workspaceUri, 'app/code/Foo/Bar/etc/frontend/di.xml').fsPath;
80+
81+
assert.strictEqual(generatedFile.uri.fsPath, expectedPath);
82+
});
83+
84+
it('should generate file in correct location for adminhtml scope', async () => {
85+
// Create test data with adminhtml scope
86+
const adminhtmlData: PreferenceWizardData = {
87+
...preferenceWizardData,
88+
area: MagentoScope.Adminhtml,
89+
};
90+
91+
// Create the generator with adminhtml data
92+
const generator = new PreferenceDiGenerator(adminhtmlData);
93+
94+
// Use a test workspace URI
95+
const workspaceUri = getTestWorkspaceUri();
96+
97+
// Generate the file
98+
const generatedFile = await generator.generate(workspaceUri);
99+
100+
// Expected path for adminhtml scope
101+
const expectedPath = Uri.joinPath(workspaceUri, 'app/code/Foo/Bar/etc/adminhtml/di.xml').fsPath;
102+
103+
assert.strictEqual(generatedFile.uri.fsPath, expectedPath);
104+
});
105+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
/**
4+
* Foo_Bar
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Foo\Bar\Model;
10+
11+
class TestPreference extends \Magento\Catalog\Model\Product
12+
{
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
/**
4+
* Foo_Bar
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Foo\Bar\Model;
10+
11+
class TestPreference
12+
{
13+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0"?>
2+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
4+
5+
<preference for="Magento\Catalog\Model\Product" type="Foo\Bar\Model\TestPreference" />
6+
</config>

0 commit comments

Comments
 (0)