13

I'm about to embark on writing a new extension for the Ecommerce module "Store" though this should relate to any extension hook. I am planning to "hook" into "store_order_complete_end" in order to add in some member/user data after an order is complete.

My core question, is how can I test this extension, without having to go through actually running through creating a cart, then registering the order, faking payment, etc on the front end which will become a rather tedious task each time I want to test my code.

Is there a simple way in either EE template code, or PHP / Plugin code to "fire" a hook, to then "fire" my code if that makes sense.

Thanks

Geoff

2

2 Answers 2

19

So, when I'm doing BR related add-ons - I work in isolation a lot of the time only hitting BR once and then using Action ID's to perform the development test work.

So, first thing you're going to need to do is create your hook as normal and install an empty add-on. You'll need to do this anyway, so it makes sense to do it.

You also want to register the same function in your exp_actions table so that you can execute the function via an ?ACT=X ID.

So, assume that $data is the array that's passed in to your extension. In the execution of the function, simply put the following in:

// =debug
header('Content-Type: text/plain; charset=iso-8859-1');
print_r($data);
exit;

N.B: The header line simply makes the output in a humanreadable with nice formatting - it's technically not required.

Now, you will need to make a trial purchase using the site - but then what will happen, is once it gets to that hook and executes your function and will display the $data array for you.

Now take that resulting $data and store it somewhere - at this point it doesn't matter where as long as you keep storing it.

This is where you now start the development. Normally, I'll simply set $data in the top of my function to the value that was output in my first test and then continue with development. You can keep testing the development with this same dataset, just by referencing the http://site.com?ACT=X URL which points to this function. Once you're then working, you simply substitute the $data value in the head of your function for the dynamically passed one that the hook passes and you're done.

Couple of Caveats:

  • When you've finished testing, make sure you uninstall your add-on, removing the Action from the exp_actions table and then reinstall it without that line of code in the install.

  • There will be no getting over the issue that you are going to have to test the add-on with real data, so once you've done your initial build/tests, you'll then have to make lots of test transactions to get it to work - you're going to need to try with variety of data etc.

2
  • 3
    An easy way to prevent having to enter test data during creating an extension is hard coding the values of the form elements to whatever you need to successfully create an order. Commented Nov 21, 2012 at 10:20
  • 2
    That is a good point Justin - however, I would say 99% of the time, you don't know the array structure thats being sent through at the time of the hook (and whether it adds additional data etc) - so rather than hard coding it, it makes a more useful test to get the value 'live' as it were to start, thus not making any assumptions which may come back and bite you. Commented Nov 21, 2012 at 10:22
0

Also try use:

echo "<pre>";
var_dump($order);
echo "</pre>";
exit;

To print out whatever variable you want. Perfect code for debugging values at specific points in your code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.