Skip to content

Commit be81fae

Browse files
committed
Merge pull request #17 from Spy-Seth/mock-service
Allow replacing a service by a mocked one.
2 parents b2b0325 + 5c82336 commit be81fae

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+840
-640
lines changed

.eslintignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
_builds/*
22
_steps/*
3-
test/*
3+
tests/*

.eslintrc

Lines changed: 129 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,131 @@
11
{
2-
"env": {
3-
"mocha": true,
4-
"node": true
5-
},
6-
"rules": {
7-
"no-shadow": 0,
8-
"no-unused-vars": 0,
9-
"valid-jsdoc": [
10-
2,
11-
{
12-
"requireParamDescription": false,
13-
"requireReturn": false,
14-
"requireReturnDescription": false,
15-
"prefer": {
16-
"returns": "return"
17-
}
18-
}
19-
],
20-
"no-native-reassign": [
21-
2,
22-
{
23-
"exceptions": ["Map"]
24-
}
25-
],
26-
"no-underscore-dangle": 0,
27-
"quotes": [
28-
2,
29-
"single"
30-
],
31-
"space-before-function-paren": [
32-
2,
33-
{
34-
"anonymous": "always",
35-
"named": "never"
36-
}
37-
],
38-
"strict": [
39-
2,
40-
"global"
41-
]
42-
}
2+
"ecmaFeatures": {
3+
"modules": false,
4+
"experimentalObjectRestSpread": false
5+
},
6+
7+
"env": {
8+
"browser": false,
9+
"es6": false,
10+
"node": true
11+
},
12+
13+
"globals": {
14+
"document": false,
15+
"navigator": false,
16+
"window": false
17+
},
18+
19+
"rules": {
20+
"accessor-pairs": 2,
21+
"arrow-spacing": [2, { "before": true, "after": true }],
22+
"brace-style": [2, "1tbs", { "allowSingleLine": true }],
23+
"comma-dangle": [2, "never"],
24+
"comma-spacing": [2, { "before": false, "after": true }],
25+
"comma-style": [2, "last"],
26+
"constructor-super": 2,
27+
"curly": [2, "multi-line"],
28+
"dot-location": [2, "property"],
29+
"eol-last": 2,
30+
"eqeqeq": [2, "allow-null"],
31+
"generator-star-spacing": [2, { "before": true, "after": true }],
32+
"handle-callback-err": [2, "^(err|error)$" ],
33+
"indent": [2, 4],
34+
"key-spacing": [2, { "beforeColon": false, "afterColon": true }],
35+
"new-cap": [2, { "newIsCap": true, "capIsNew": false }],
36+
"new-parens": 2,
37+
"no-array-constructor": 2,
38+
"no-caller": 2,
39+
"no-class-assign": 2,
40+
"no-cond-assign": 2,
41+
"no-const-assign": 2,
42+
"no-control-regex": 2,
43+
"no-debugger": 2,
44+
"no-delete-var": 2,
45+
"no-dupe-args": 2,
46+
"no-dupe-keys": 2,
47+
"no-duplicate-case": 2,
48+
"no-empty-character-class": 2,
49+
"no-empty-label": 2,
50+
"no-eval": 2,
51+
"no-ex-assign": 2,
52+
"no-extend-native": 2,
53+
"no-extra-bind": 2,
54+
"no-extra-boolean-cast": 2,
55+
"no-extra-parens": [2, "functions"],
56+
"no-fallthrough": 2,
57+
"no-floating-decimal": 2,
58+
"no-func-assign": 2,
59+
"no-implied-eval": 2,
60+
"no-inner-declarations": [2, "functions"],
61+
"no-invalid-regexp": 2,
62+
"no-irregular-whitespace": 2,
63+
"no-iterator": 2,
64+
"no-label-var": 2,
65+
"no-labels": 2,
66+
"no-lone-blocks": 2,
67+
"no-mixed-spaces-and-tabs": 2,
68+
"no-multi-spaces": 2,
69+
"no-multi-str": 2,
70+
"no-multiple-empty-lines": [2, { "max": 1 }],
71+
"no-native-reassign": 2,
72+
"no-negated-in-lhs": 2,
73+
"no-new": 2,
74+
"no-new-func": 2,
75+
"no-new-object": 2,
76+
"no-new-require": 2,
77+
"no-new-wrappers": 2,
78+
"no-obj-calls": 2,
79+
"no-octal": 2,
80+
"no-octal-escape": 2,
81+
"no-proto": 2,
82+
"no-redeclare": 2,
83+
"no-regex-spaces": 2,
84+
"no-return-assign": 2,
85+
"no-self-compare": 2,
86+
"no-sequences": 2,
87+
"no-shadow-restricted-names": 2,
88+
"no-spaced-func": 2,
89+
"no-sparse-arrays": 2,
90+
"no-this-before-super": 2,
91+
"no-throw-literal": 2,
92+
"no-trailing-spaces": 2,
93+
"no-undef": 2,
94+
"no-undef-init": 2,
95+
"no-unexpected-multiline": 2,
96+
"no-unneeded-ternary": 2,
97+
"no-unreachable": 2,
98+
"no-unused-vars": [2, { "vars": "all", "args": "none" }],
99+
"no-useless-call": 2,
100+
"no-with": 2,
101+
"one-var": [2, { "initialized": "never" }],
102+
"operator-linebreak": [2, "after"],
103+
"quotes": [2, "single", "avoid-escape"],
104+
"radix": 2,
105+
"semi": [2, "always"],
106+
"space-after-keywords": [2, "always"],
107+
"space-before-blocks": [2, "always"],
108+
"space-before-function-paren": [2, {
109+
"anonymous": "always",
110+
"named": "never"
111+
}],
112+
"space-in-parens": [2, "never"],
113+
"space-infix-ops": 2,
114+
"space-return-throw-case": 2,
115+
"space-unary-ops": [2, { "words": true, "nonwords": false }],
116+
"spaced-comment": [2, "always", { "markers": ["global", "globals", "eslint", "eslint-disable", "*package", "!", ","] }],
117+
"use-isnan": 2,
118+
"valid-typeof": 2,
119+
"wrap-iife": [2, "any"],
120+
"yoda": [2, "never"],
121+
122+
"valid-jsdoc": [2, {
123+
"requireParamDescription": false,
124+
"requireReturn": false,
125+
"requireReturnDescription": false,
126+
"prefer": {
127+
"returns": "return"
128+
}
129+
}]
130+
}
43131
}

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ sudo: false
44

55
node_js:
66
- "iojs-v2.4.0"
7+
- "iojs-v3.1.0"
78
- "0.12"
89

910
script:

package.json

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
],
1313
"main": "index.js",
1414
"scripts": {
15-
"bdd": "mocha --colors --reporter=dot --watch --recursive test",
16-
"coverage": "istanbul cover node_modules/.bin/_mocha --report lcovonly -- --reporter=dot --recursive test",
17-
"coverage-upload": "cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js",
15+
"bdd": "NODE_ENV=test mocha --colors --reporter=dot --watch --recursive tests",
16+
"coverage": "NODE_ENV=test istanbul cover node_modules/.bin/_mocha --report lcovonly -- --reporter=dot --recursive tests",
17+
"coverage-upload": "NODE_ENV=test cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js",
1818
"lint": "eslint --color .",
19-
"test": "mocha --colors --reporter=dot --recursive test"
19+
"test": "NODE_ENV=test mocha --colors --reporter=dot --recursive tests"
2020
},
2121
"repository": {
2222
"type": "git",
@@ -26,11 +26,12 @@
2626
"license": "MIT",
2727
"devDependencies": {
2828
"chai": "~3.2.0",
29-
"coveralls": "~2.11.3",
30-
"eslint": "~0.24.1",
31-
"istanbul": "~0.3.17",
29+
"coveralls": "~2.11.4",
30+
"eslint": "~1.2.1",
31+
"istanbul": "~0.3.18",
3232
"mocha": "~2.2.5",
33-
"sinon": "~1.15.4"
33+
"sinon": "~1.16.1",
34+
"sinon-chai": "~2.8.0"
3435
},
3536
"dependencies": {
3637
"immutable": "~3.7.4"

src/Container.js

Lines changed: 53 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
'use strict';
22

3-
var Parameter = require('./Parameter');
4-
var ParameterCollection = require('./ParameterCollection');
5-
var ServiceArgument = require('./ServiceArgument');
6-
var ServiceArgumentCollection = require('./ServiceArgumentCollection');
7-
var ServiceDefinition = require('./ServiceDefinition');
8-
var ServiceDefinitionCollection = require('./ServiceDefinitionCollection');
93
var ServiceStorage = require('./ServiceStorage');
104

115
/**
@@ -17,44 +11,70 @@ var ServiceStorage = require('./ServiceStorage');
1711
* @constructor
1812
*/
1913
var Container = function Container(serviceDefinitionCollection, parameterCollection) {
20-
var serviceStorage = new ServiceStorage();
14+
this.serviceDefinitionCollection = serviceDefinitionCollection;
15+
this.parameterCollection = parameterCollection;
2116

22-
/**
23-
* @param {String} name
24-
* @return {*|null}
25-
*/
26-
this.getParameter = function (name) {
27-
if (!parameterCollection.hasParameter(name)) {
28-
throw new Error('Unknown parameter "' + name + '".');
29-
}
17+
this.serviceStorage = new ServiceStorage();
18+
};
19+
20+
/**
21+
* Return a parameter value.
22+
*
23+
* @param {String} name
24+
* @return {*|null}
25+
*/
26+
Container.prototype.getParameter = function (name) {
27+
if (!this.parameterCollection.hasParameter(name)) {
28+
throw new Error('Unknown parameter "' + name + '".');
29+
}
3030

31-
var parameter = parameterCollection.getParameter(name);
31+
var parameter = this.parameterCollection.getParameter(name);
3232

33-
return parameter.getValue();
34-
};
33+
return parameter.getValue();
34+
};
35+
36+
/**
37+
* Return a sercice instance.
38+
*
39+
* @param {String} name
40+
* @return {*}
41+
*/
42+
Container.prototype.getService = function (name) {
43+
if (!this.serviceDefinitionCollection.hasServiceDefinition(name)) {
44+
throw new Error('Unknown service "' + name + '".');
45+
}
46+
47+
if (this.serviceStorage.hasInstance(name)) {
48+
return this.serviceStorage.getInstance(name);
49+
}
50+
51+
var serviceDefinition = this.serviceDefinitionCollection.getServiceDefinition(name);
52+
var serviceInstance = serviceDefinition.createInstance(this);
53+
if (serviceDefinition.isSingleton()) {
54+
this.serviceStorage.addInstance(name, serviceInstance);
55+
}
56+
57+
return serviceInstance;
58+
};
3559

60+
if (process.env.NODE_ENV === 'test') {
3661
/**
62+
* Replace a service instance by a mocked instance. Only avialable in test environement.
63+
*
3764
* @param {String} name
38-
* @return {*}
65+
* @param {Function} mock
3966
*/
40-
this.getService = function (name) {
41-
if (!serviceDefinitionCollection.hasServiceDefinition(name)) {
67+
Container.prototype.mockService = function (name, mock) {
68+
if (!this.serviceDefinitionCollection.hasServiceDefinition(name)) {
4269
throw new Error('Unknown service "' + name + '".');
4370
}
4471

45-
if (serviceStorage.hasInstance(name)) {
46-
return serviceStorage.getInstance(name);
47-
}
48-
49-
var serviceDefinition = serviceDefinitionCollection.getServiceDefinition(name);
50-
var serviceInstance = serviceDefinition.createInstance(this);
51-
52-
if (serviceDefinition.isSingleton()) {
53-
serviceStorage.addInstance(name, serviceInstance);
72+
if (!this.serviceStorage.hasInstance(name)) {
73+
this.serviceStorage.addInstance(name, mock);
74+
return;
5475
}
5576

56-
return serviceInstance;
77+
this.serviceStorage.replaceInstance(name, mock);
5778
};
58-
};
59-
79+
}
6080
module.exports = Container;

src/ContainerFactory.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ var ServiceArgument = require('./ServiceArgument');
1010
var ServiceArgumentCollection = require('./ServiceArgumentCollection');
1111
var ServiceDefinition = require('./ServiceDefinition');
1212
var ServiceDefinitionCollection = require('./ServiceDefinitionCollection');
13-
var ServiceStorage = require('./ServiceStorage');
1413

1514
/**
1615
* @param {Array} services
@@ -51,7 +50,7 @@ var buildParameterCollection = function buildParameterCollection(parameters) {
5150
return new ParameterCollection(parameterMap.toArray());
5251
};
5352

54-
var checkCyclicDependencies = function (serviceDefinition, serviceDefinitionCollection, parentDependentServiceNames) {
53+
var checkCyclicDependencies = function checkCyclicDependencies(serviceDefinition, serviceDefinitionCollection, parentDependentServiceNames) {
5554
parentDependentServiceNames = parentDependentServiceNames || [serviceDefinition.getName()];
5655

5756
var serviceArguments = new List(serviceDefinition.getArgumentCollection().getServiceArguments());
@@ -83,7 +82,7 @@ var ContainerFactory = {};
8382
* @return {Container}
8483
* @public
8584
*/
86-
ContainerFactory.create = function (services, parameters) {
85+
ContainerFactory.create = function create(services, parameters) {
8786
services = services || [];
8887
parameters = parameters || {};
8988

0 commit comments

Comments
 (0)