PageRenderTime 8ms CodeModel.GetById 2ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/wheels/tests/model/callbacks/beforeSave.cfc

http://cfwheels.googlecode.com/
ColdFusion CFScript | 75 lines | 67 code | 8 blank | 0 comment | 0 complexity | 6c85fc3f970ef644033a9ef8e9fa0c5e MD5 | raw file
 1<cfcomponent extends="wheelsMapping.Test">
 2
 3	<cffunction name="test_proceeding_on_true_and_nothing">
 4		<cfset model("tag").$registerCallback(type="beforeSave", methods="callbackThatReturnsTrue,callbackThatReturnsNothing")>
 5		<cfset loc.obj = model("tag").findOne(order="id")>
 6		<cfset loc.oldName = loc.obj.name>
 7		<cfset loc.obj.name = "somethingElse">
 8		<cfset loc.obj.save()>
 9		<cfset loc.obj.reload()>
10		<cfset loc.name = loc.obj.name>
11		<cfset loc.obj.name = loc.oldName>
12		<cfset loc.obj.save()>
13		<cfset model("tag").$clearCallbacks(type="beforeSave")>
14		<cfset assert("loc.name IS NOT loc.oldName")>
15	</cffunction>
16
17	<cffunction name="test_aborting_on_false">
18		<cfset model("tag").$registerCallback(type="beforeSave", methods="callbackThatReturnsFalse")>
19		<cfset loc.obj = model("tag").findOne(order="id")>
20		<cfset loc.oldName = loc.obj.name>
21		<cfset loc.obj.name = "somethingElse">
22		<cfset loc.obj.save()>
23		<cfset loc.obj.reload()>
24		<cfset model("tag").$clearCallbacks(type="beforeSave")>
25		<cfset assert("loc.obj.name IS loc.oldName")>
26	</cffunction>
27
28	<cffunction name="test_setting_property">
29		<cfset model("tag").$registerCallback(type="beforeSave", methods="callbackThatSetsProperty")>
30		<cfset loc.obj = model("tag").findOne(order="id")>
31		<cfset loc.existBefore = StructKeyExists(loc.obj, "setByCallback")>
32		<cfset loc.obj.save()>
33		<cfset loc.existAfter = StructKeyExists(loc.obj, "setByCallback")>
34		<cfset model("tag").$clearCallbacks(type="beforeSave")>
35		<cfset assert("NOT loc.existBefore AND loc.existAfter")>
36	</cffunction>
37
38	<cffunction name="test_setting_property_with_skipped_callback">
39		<cfset model("tag").$registerCallback(type="beforeSave", methods="callbackThatSetsProperty")>
40		<cfset loc.obj = model("tag").findOne(order="id")>
41		<cfset loc.existBefore = StructKeyExists(loc.obj, "setByCallback")>
42		<cfset loc.obj.save(callbacks=false, transaction="rollback")>
43		<cfset loc.existAfter = StructKeyExists(loc.obj, "setByCallback")>
44		<cfset model("tag").$clearCallbacks(type="beforeSave")>
45		<cfset assert("NOT loc.existBefore AND NOT loc.existAfter")>
46	</cffunction>
47
48	<cffunction name="test_execution_order">
49		<cfset model("tag").$registerCallback(type="beforeSave", methods="firstCallback,secondCallback")>
50		<cfset loc.obj = model("tag").findOne(order="id")>
51		<cfset loc.obj.name = "somethingElse">
52		<cfset loc.obj.save()>
53		<cfset model("tag").$clearCallbacks(type="beforeSave")>
54		<cfset assert("loc.obj.orderTest IS 'first,second'")>
55	</cffunction>
56
57	<cffunction name="test_aborting_chain">
58		<cfset model("tag").$registerCallback(type="beforeSave", methods="firstCallback,callbackThatReturnsFalse,secondCallback")>
59		<cfset loc.obj = model("tag").findOne(order="id")>
60		<cfset loc.obj.name = "somethingElse">
61		<cfset loc.obj.save()>
62		<cfset model("tag").$clearCallbacks(type="beforeSave")>
63		<cfset assert("loc.obj.orderTest IS 'first'")>
64	</cffunction>
65
66	<cffunction name="test_setting_in_init_and_clearing">
67		<cfset loc.callbacks = model("author").$callbacks()>
68		<cfset assert("loc.callbacks.beforeSave[1] IS 'callbackThatReturnsTrue'")>
69		<cfset model("author").$clearCallbacks(type="beforeSave")>
70		<cfset assert("ArrayLen(loc.callbacks.beforeSave) IS 0 AND loc.callbacks.beforeDelete[1] IS 'callbackThatReturnsTrue'")>
71		<cfset model("author").$clearCallbacks()>
72		<cfset assert("ArrayLen(loc.callbacks.beforeDelete) IS 0")>
73	</cffunction>
74
75</cfcomponent>