| 1 | <!----------------------------------------------------------------------- |
|---|
| 2 | Template : baseTest.cfc |
|---|
| 3 | Author : Luis Majano |
|---|
| 4 | Date : 5/25/2007 |
|---|
| 5 | Description : |
|---|
| 6 | Base Unit Test Component based on CFCUnit. |
|---|
| 7 | |
|---|
| 8 | If you would like to change this to CFUnit, then change the extends |
|---|
| 9 | portion to net.sourceforge.cfunit.framework.TestCase |
|---|
| 10 | |
|---|
| 11 | This is a base test component for testing coldbox handlers. All you need |
|---|
| 12 | to do is add the extends portions of your test cases to this base test |
|---|
| 13 | and you will have a coldbox handler test. The setup method will need |
|---|
| 14 | to be changed in order to match your application path. |
|---|
| 15 | |
|---|
| 16 | MODIFY: |
|---|
| 17 | 1) instance.AppMapping : To point to your application relative from the root |
|---|
| 18 | or via CF Mappings. |
|---|
| 19 | 2) instance.ConfigMapping : The expanded path location of your coldbox configuration file. |
|---|
| 20 | |
|---|
| 21 | OPTIONAL: |
|---|
| 22 | 3) Execute the on App start handler. You will need to fill out the name |
|---|
| 23 | of the Application Start Handler to be executed. |
|---|
| 24 | |
|---|
| 25 | ----------------------------------------------------------------------> |
|---|
| 26 | <cfcomponent name="baseTest" extends="org.cfcunit.framework.TestCase" output="false"> |
|---|
| 27 | |
|---|
| 28 | <!------------------------------------------- CONSTRUCTOR -------------------------------------------> |
|---|
| 29 | |
|---|
| 30 | <cfscript> |
|---|
| 31 | variables.instance = structnew(); |
|---|
| 32 | instance.AppMapping = ""; |
|---|
| 33 | instance.ConfigMapping = ""; |
|---|
| 34 | instance.controller = ""; |
|---|
| 35 | </cfscript> |
|---|
| 36 | |
|---|
| 37 | <cffunction name="setUp" returntype="void" access="private"> |
|---|
| 38 | <cfscript> |
|---|
| 39 | //Initialize ColdBox |
|---|
| 40 | instance.controller = CreateObject("component", "coldbox.system.testcontroller").init(); |
|---|
| 41 | instance.controller.getService("loader").configLoader(instance.ConfigMapping,instance.AppMapping); |
|---|
| 42 | instance.controller.getService("loader").registerHandlers(); |
|---|
| 43 | |
|---|
| 44 | //Create Initial Event Context |
|---|
| 45 | setupRequest(); |
|---|
| 46 | //Clean up Initial Event Context due to MACH-II vars of the unit test framework. |
|---|
| 47 | getRequestContext().clearCollection(); |
|---|
| 48 | </cfscript> |
|---|
| 49 | </cffunction> |
|---|
| 50 | |
|---|
| 51 | <!------------------------------------------- HELPERS -------------------------------------------> |
|---|
| 52 | |
|---|
| 53 | <!--- getter for AppMapping ---> |
|---|
| 54 | <cffunction name="getAppMapping" access="public" returntype="string" output="false"> |
|---|
| 55 | <cfreturn instance.AppMapping> |
|---|
| 56 | </cffunction> |
|---|
| 57 | |
|---|
| 58 | <cffunction name="setAppMapping" access="public" output="false" returntype="void" hint="Set AppMapping"> |
|---|
| 59 | <cfargument name="AppMapping" type="string" required="true"/> |
|---|
| 60 | <cfset instance.AppMapping = arguments.AppMapping/> |
|---|
| 61 | </cffunction> |
|---|
| 62 | |
|---|
| 63 | <!--- getter for ConfigMapping ---> |
|---|
| 64 | <cffunction name="getConfigMapping" access="public" returntype="string" output="false"> |
|---|
| 65 | <cfreturn instance.ConfigMapping> |
|---|
| 66 | </cffunction> |
|---|
| 67 | |
|---|
| 68 | <cffunction name="setConfigMapping" access="public" output="false" returntype="void" hint="Set ConfigMapping"> |
|---|
| 69 | <cfargument name="ConfigMapping" type="string" required="true"/> |
|---|
| 70 | <cfset instance.ConfigMapping = arguments.ConfigMapping/> |
|---|
| 71 | </cffunction> |
|---|
| 72 | |
|---|
| 73 | <!--- getter for controller ---> |
|---|
| 74 | <cffunction name="getcontroller" access="public" returntype="any" output="false"> |
|---|
| 75 | <cfreturn instance.controller> |
|---|
| 76 | </cffunction> |
|---|
| 77 | |
|---|
| 78 | <!--- Get current request context ---> |
|---|
| 79 | <cffunction name="getRequestContext" access="public" output="false" returntype="any" hint="Get the event object"> |
|---|
| 80 | <cfreturn getController().getRequestService().getContext() > |
|---|
| 81 | </cffunction> |
|---|
| 82 | |
|---|
| 83 | <!--- Setup a request context ---> |
|---|
| 84 | <cffunction name="setupRequest" access="public" output="false" returntype="void" hint="Setup a request with FORM/URL data"> |
|---|
| 85 | <cfset getController().getRequestService().requestCapture() > |
|---|
| 86 | </cffunction> |
|---|
| 87 | |
|---|
| 88 | <!--- prepare request, execute request and retrieve request ---> |
|---|
| 89 | <cffunction name="execute" access="public" output="false" returntype="any" hint="Executes a framework lifecycle"> |
|---|
| 90 | <cfargument name="eventhandler" required="true" type="string" hint=""> |
|---|
| 91 | <cfscript> |
|---|
| 92 | //Setup the request Context with setup FORM/URL variables set in the unit test. |
|---|
| 93 | setupRequest(); |
|---|
| 94 | //TEST EVENT EXECUTION |
|---|
| 95 | getController().runEvent(eventhandler); |
|---|
| 96 | //Return the correct event context. |
|---|
| 97 | return getRequestContext(); |
|---|
| 98 | </cfscript> |
|---|
| 99 | </cffunction> |
|---|
| 100 | |
|---|
| 101 | <!------------------------------------------- PRIVATE -------------------------------------------> |
|---|
| 102 | |
|---|
| 103 | <cffunction name="dump" access="private" hint="Facade for cfmx dump" returntype="void"> |
|---|
| 104 | <cfargument name="var" required="yes" type="any"> |
|---|
| 105 | <cfdump var="#var#"> |
|---|
| 106 | </cffunction> |
|---|
| 107 | <cffunction name="abort" access="private" hint="Facade for cfabort" returntype="void" output="false"> |
|---|
| 108 | <cfabort> |
|---|
| 109 | </cffunction> |
|---|
| 110 | |
|---|
| 111 | </cfcomponent> |
|---|