Skip to content

Commit

Permalink
Refactor to Wirebox injection.
Browse files Browse the repository at this point in the history
  • Loading branch information
elpete committed May 7, 2016
1 parent 3f53bd5 commit 54bc5e0
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 22 deletions.
14 changes: 9 additions & 5 deletions models/Query/Builder.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ component displayname='Builder' {

property name='grammar' inject='Grammar@Quick';
property name='utils' inject='QueryUtils@Quick';
property name='wirebox' inject='wirebox';

property name='distinct' type='boolean' default='false';
property name='columns' type='array';
Expand Down Expand Up @@ -77,26 +78,29 @@ component displayname='Builder' {
string type = 'inner',
any conditions
) {
var join = new JoinClause(arguments.type, arguments.table, utils);
var joinClause = wirebox.getInstance(name = 'JoinClause@Quick', initArguments = {
type = arguments.type,
table = arguments.table
});

if (structKeyExists(arguments, 'first') && isClosure(arguments.first)) {
arguments.conditions = arguments.first;
}

if (structKeyExists(arguments, 'conditions') && isClosure(arguments.conditions)) {
conditions(join);
conditions(joinClause);
}
else {
join.on(
joinClause.on(
first = arguments.first,
operator = arguments.operator,
second = arguments.second,
combinator = 'and'
);
}

arrayAppend(variables.joins, join);
arrayAppend(bindings.join, join.getBindings(), true);
arrayAppend(variables.joins, joinClause);
arrayAppend(bindings.join, joinClause.getBindings(), true);

return this;
}
Expand Down
11 changes: 4 additions & 7 deletions models/Query/JoinClause.cfc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
component displayname='JoinClause' {

property name='utils';
property name='utils' inject='QueryUtils@Quick';

property name='type' type='string';
property name='table' type='string';
Expand All @@ -23,8 +23,7 @@ component displayname='JoinClause' {

public JoinClause function init(
required string type,
required string table,
QueryUtils utils = wirebox.getInstance('QueryUtils@Quick')
required string table
) {
if (! arrayContainsNoCase(types, arguments.type)) {
throw('[#type#] is not a valid sql join type');
Expand All @@ -33,10 +32,8 @@ component displayname='JoinClause' {
variables.type = arguments.type;
variables.table = arguments.table;

clauses = [];
bindings = [];

variables.utils = arguments.utils;
variables.clauses = [];
variables.bindings = [];

return this;
}
Expand Down
14 changes: 14 additions & 0 deletions tests/specs/Query/Builder/BuilderGetSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,22 @@ component extends='testbox.system.BaseSpec' {
beforeEach(function() {
variables.query = new Quick.models.Query.Builder();
getMockBox().prepareMock(query);

var utils = new Quick.models.Query.QueryUtils()
query.$property(propertyName = 'utils', mock = utils);

var mockWirebox = getMockBox().createStub();
var mockJoinClause = getMockBox()
.prepareMock(new Quick.models.Query.JoinClause('inner', 'second'));
mockJoinClause.$property(propertyName = 'utils', mock = utils);
mockWirebox
.$('getInstance')
.$args(name = 'JoinClause@Quick', initArguments = {
type = 'inner',
table = 'second'
})
.$results(mockJoinClause);
query.$property(propertyName = 'wirebox', mock = mockWirebox);
});

it('retreives bindings in a flat array', function() {
Expand Down
48 changes: 47 additions & 1 deletion tests/specs/Query/Builder/BuilderJoinSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,34 @@ component extends='testbox.system.BaseSpec' {
beforeEach(function() {
variables.query = new Quick.models.Query.Builder();
getMockBox().prepareMock(query);
query.$property(propertyName = 'utils', mock = new Quick.models.Query.QueryUtils());
variables.utils = new Quick.models.Query.QueryUtils();
query.$property(propertyName = 'utils', mock = utils);
variables.mockWirebox = getMockBox().createStub();
var mockJoinClause = getMockBox()
.prepareMock(new Quick.models.Query.JoinClause('inner', 'second'));
mockJoinClause.$property(propertyName = 'utils', mock = utils);
mockWirebox
.$('getInstance')
.$args(name = 'JoinClause@Quick', initArguments = {
type = 'inner',
table = 'second'
})
.$results(mockJoinClause);
query.$property(propertyName = 'wirebox', mock = mockWirebox);
});

it('does a simple inner join', function() {
var mockJoinClause = getMockBox()
.prepareMock(new Quick.models.Query.JoinClause('inner', 'second'));
mockJoinClause.$property(propertyName = 'utils', mock = utils);
mockWirebox
.$('getInstance')
.$args(name = 'JoinClause@Quick', initArguments = {
type = 'inner',
table = 'second'
})
.$results(mockJoinClause);

query.join('second', 'first.id', '=', 'second.first_id');

var joins = query.getJoins();
Expand All @@ -31,6 +55,17 @@ component extends='testbox.system.BaseSpec' {
});

it('does a left join', function() {
var mockJoinClause = getMockBox()
.prepareMock(new Quick.models.Query.JoinClause('left', 'second'));
mockJoinClause.$property(propertyName = 'utils', mock = utils);
mockWirebox
.$('getInstance')
.$args(name = 'JoinClause@Quick', initArguments = {
type = 'left',
table = 'second'
})
.$results(mockJoinClause);

query.leftJoin('second', 'first.id', '=', 'second.first_id');

var joins = query.getJoins();
Expand All @@ -54,6 +89,17 @@ component extends='testbox.system.BaseSpec' {
});

it('does a right join', function() {
var mockJoinClause = getMockBox()
.prepareMock(new Quick.models.Query.JoinClause('right', 'second'));
mockJoinClause.$property(propertyName = 'utils', mock = utils);
mockWirebox
.$('getInstance')
.$args(name = 'JoinClause@Quick', initArguments = {
type = 'right',
table = 'second'
})
.$results(mockJoinClause);

query.rightJoin('second', 'first.id', '=', 'second.first_id');

var joins = query.getJoins();
Expand Down
17 changes: 8 additions & 9 deletions tests/specs/Query/Builder/JoinClauseSpec.cfc
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
component extends='testbox.system.BaseSpec' {
function beforeAll() {
variables.utils = new Quick.models.Query.QueryUtils();
}
function run() {
describe('initialization', function() {
it('requires a type and a table', function() {
expect(function() { new Quick.models.Query.JoinClause(); }).toThrow();
expect(function() { new Quick.models.Query.JoinClause('inner'); }).toThrow();
expect(function() { new Quick.models.Query.JoinClause('inner', 'sometable', variables.utils); }).notToThrow();
expect(function() { new Quick.models.Query.JoinClause('inner', 'sometable'); }).notToThrow();
});

it('validates the type is a valid sql join type', function() {
expect(function() { new Quick.models.Query.JoinClause('gibberish', 'sometable', variables.utils) }).toThrow();
expect(function() { new Quick.models.Query.JoinClause('left typo', 'sometable', variables.utils) }).toThrow();
expect(function() { new Quick.models.Query.JoinClause('left', 'sometable', variables.utils) }).notToThrow();
expect(function() { new Quick.models.Query.JoinClause('left outer', 'sometable', variables.utils) }).notToThrow();
expect(function() { new Quick.models.Query.JoinClause('gibberish', 'sometable') }).toThrow();
expect(function() { new Quick.models.Query.JoinClause('left typo', 'sometable') }).toThrow();
expect(function() { new Quick.models.Query.JoinClause('left', 'sometable') }).notToThrow();
expect(function() { new Quick.models.Query.JoinClause('left outer', 'sometable') }).notToThrow();
});
});

describe('adding join conditions', function() {
beforeEach(function() {
variables.join = new Quick.models.Query.JoinClause('inner', 'second', variables.utils);
variables.join = new Quick.models.Query.JoinClause('inner', 'second');
getMockBox().prepareMock(join);
join.$property(propertyName = 'utils', mock = new Quick.models.Query.QueryUtils());
});

afterEach(function() {
Expand Down

0 comments on commit 54bc5e0

Please sign in to comment.