Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(autowire): 🐛 Autowiring override arguments do not work when c… #220

Merged
merged 2 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions lib/Autowire.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import json5 from 'json5'
import { parse } from '@typescript-eslint/typescript-estree'
import Definition from './Definition'
import Reference from './Reference'
import ServiceFile from './ServiceFile'
import AutowireIdentifier from './AutowireIdentifier'
import ContainerDefaultDirMustBeSet from './Exception/ContainerDefaultDirMustBeSet'
import PassConfig from './PassConfig'
Expand All @@ -29,6 +28,7 @@ export default class Autowire {
} catch (e) {
this._tsConfigPaths = null
}
this._container.autowire = this
}

_ensureContainerIsValidForAutowire (container) {
Expand Down Expand Up @@ -120,9 +120,6 @@ export default class Autowire {
promises.push(this._executeFilePath(filePath))
}
await Promise.all(promises)
if (this._serviceFile instanceof ServiceFile) {
await this._serviceFile.generateFromContainer(this._container)
}
this._container.addCompilerPass(
new AutowireOverridePass(),
PassConfig.TYPE_BEFORE_OPTIMIZATION
Expand Down
16 changes: 16 additions & 0 deletions lib/ContainerBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import WrongDefinitionException from './Exception/WrongDefinitionException'
import FrozenContainerException from './Exception/FrozenContainerException'
import RootDirectoryNotFound from './Exception/RootDirectoryNotFound'
import RootDirectoryMustBeAbsolute from './Exception/RootDirectoryMustBeAbsolute'
import ServiceFile from './ServiceFile'

class ContainerBuilder {
/**
Expand All @@ -32,6 +33,18 @@ class ContainerBuilder {
this._instanceManager = undefined
this._containerReferenceAsService = containerReferenceAsService
this._defaultDir = defaultDir
this._autowire = null
}

/**
* @param {Autowire} autowire
*/
set autowire (autowire) {
this._autowire = autowire
}

get autowire () {
return this._autowire
}

ensureDirectoryExists (directory) {
Expand Down Expand Up @@ -163,6 +176,9 @@ class ContainerBuilder {

async compile () {
await new Compiler(this).run()
if (this._autowire?.serviceFile instanceof ServiceFile) {
await this._autowire.serviceFile.generateFromContainer(this)
}
}

/**
Expand Down
14 changes: 14 additions & 0 deletions test/Resources-ts/Autowire-Override/config/services-noimports.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
services:
App.FooBarAutowireOverride:
class: './FooBarAutowireOverride'
override_arguments:
- '@CiAdapter'
- '@SomeService'
- '@AnotherService'

App.AnotherFooBarAutowireOverride:
class: './AnotherFooBarAutowireOverride'
override_arguments:
- '@FooAdapter'
- '@SomeService'
- '@AnotherService'
155 changes: 114 additions & 41 deletions test/node-dependency-injection/lib-ts/Autowire.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,51 +38,121 @@ describe('AutowireTS', () => {
const excludedServiceMessage = 'The service ExcludedService is not registered'
const inFolderExcludedMessage = 'The service InFolderExcludedService is not registered'

it("should not override single class with autowiring if not exists", async () => {
const configFile = path.join(
__dirname,
'..',
'..',
resourcesTsFolder,
'Autowire-Override',
'config',
'services-not-exists.yaml'
)
const cb = new ContainerBuilder()
const loader = new YamlFileLoader(cb)
await loader.load(configFile)
await cb.compile()
describe("Autowiring override arguments", () => {
const folder = 'Autowire-Override';

it("should override arguments be visible after compile container and dump file", async () => {
// Arrange.
const configFile = path.join(
__dirname,
'..',
'..',
resourcesTsFolder,
folder,
'config',
'services-noimports.yaml'
)
const dir = path.join(__dirname, '..', '..', resourcesTsFolder, folder, 'src')
const container = new ContainerBuilder(false, dir)
const loader = new YamlFileLoader(container)
const autowire = new Autowire(container)
const dumpPath = `${dumpServicesPath}-override.yaml`
autowire.serviceFile = new ServiceFile(dumpPath)
await loader.load(configFile)
await autowire.process()
await container.compile()

const containerFromDump = new ContainerBuilder(false, dir)
const loaderFromDump = new YamlFileLoader(containerFromDump)

// Act.
await loaderFromDump.load(dumpPath)

// Act.
const actual = containerFromDump.get(FooBarAutowireOverride)
const actualAnother = containerFromDump.get(AnotherFooBarAutowireOverride)

// Assert.
assert.equal(actual.getString(), "ci")
assert.equal(actualAnother.getString(), "foo")
});

it("should override arguments be visible after compile container", async () => {
// Arrange.
const dir = path.join(__dirname, '..', '..', resourcesTsFolder, folder, 'src')
const container = new ContainerBuilder(false, dir)
const autowire = new Autowire(container)
const loader = new YamlFileLoader(container);
const configFile = path.join(
__dirname,
'..',
'..',
resourcesTsFolder,
folder,
'config',
'services-noimports.yaml'
)
await loader.load(configFile)
await autowire.process()
await container.compile()

// Act.
const actual = container.get(FooBarAutowireOverride)
const actualAnother = container.get(AnotherFooBarAutowireOverride)

// Assert.
assert.equal(actual.getString(), "ci")
assert.equal(actualAnother.getString(), "foo")
});

// Act.
const actual = cb.get(FooBarAutowireOverride)
it("should not override single class with autowiring if not exists", async () => {
// Arrange.
const configFile = path.join(
__dirname,
'..',
'..',
resourcesTsFolder,
folder,
'config',
'services-not-exists.yaml'
)
const cb = new ContainerBuilder()
const loader = new YamlFileLoader(cb)
await loader.load(configFile)
await cb.compile()

// Assert.
assert.isUndefined(actual.adapter)
});

it("should override single class with autowiring", async () => {
const configFile = path.join(
__dirname,
'..',
'..',
resourcesTsFolder,
'Autowire-Override',
'config',
'services.yaml'
)
const cb = new ContainerBuilder()
const loader = new YamlFileLoader(cb)
await loader.load(configFile)
await cb.compile()
// Act.
const actual = cb.get(FooBarAutowireOverride)

// Assert.
assert.isUndefined(actual.adapter)
});

// Act.
const actual = cb.get(FooBarAutowireOverride)
const actualAnother = cb.get(AnotherFooBarAutowireOverride)
it("should override single class with autowiring", async () => {
// Arrange.
const configFile = path.join(
__dirname,
'..',
'..',
resourcesTsFolder,
folder,
'config',
'services.yaml'
)
const cb = new ContainerBuilder()
const loader = new YamlFileLoader(cb)
await loader.load(configFile)
await cb.compile()

// Assert.
assert.equal(actual.getString(), "ci")
assert.equal(actualAnother.getString(), "bar")
});
// Act.
const actual = cb.get(FooBarAutowireOverride)
const actualAnother = cb.get(AnotherFooBarAutowireOverride)

// Assert.
assert.equal(actual.getString(), "ci")
assert.equal(actualAnother.getString(), "bar")
});
})

it('should get service file when was properly set', () => {
// Arrange.
Expand Down Expand Up @@ -124,6 +194,7 @@ describe('AutowireTS', () => {
const dumpPath = `${dumpServicesPath}.yaml`
autowire.serviceFile = new ServiceFile(dumpPath, true)
await autowire.process()
await container.compile()
const containerDump = new ContainerBuilder(false)
const loader = new YamlFileLoader(containerDump)

Expand Down Expand Up @@ -188,6 +259,7 @@ describe('AutowireTS', () => {
await autowire.process()
const containerDump = new ContainerBuilder(false, dir)
const loader = new JsonFileLoader(containerDump)
await container.compile()

// Act.
await loader.load(dumpPath)
Expand Down Expand Up @@ -216,6 +288,7 @@ describe('AutowireTS', () => {
await autowire.process()
const containerDump = new ContainerBuilder(false, dir)
const loader = new JsFileLoader(containerDump)
await container.compile()

// Act.
await loader.load(dumpPath)
Expand Down
Loading