CodeIgniter Forums
Setting $_POST, Getting with input->post - Printable Version

+- CodeIgniter Forums (
+-- Forum: Archived Discussions (
+--- Forum: Archived Development & Programming (
+--- Thread: Setting $_POST, Getting with input->post (/thread-42278.html)

Setting $_POST, Getting with input->post - El Forum - 06-01-2011

I am writing a test suite for an application and have model functions that look like:

function model_function() {
    $var1 = $this->input->post('var1');
    $var2 = $this->input->post('var2');

What I would like to do (and what I have tried) is to write a test function that looks like this:

function test_controller() {
    $_POST['var1'] = 'value1';
    $_POST['var2'] = 'value2';
    $ret = $this->my_model->model_function();

So, in words, I'd like to populate the $_POST variable then call a model function that will read the values in $_POST using $this->input->post(). This however does not seem to work. input->post() returns FALSE for everything.

Is this a case where CI is setting an internal variable when it is initialized and used by input->post() such that setting $_POST values after CI has initialized is pointless? If this is not the case, what am I missing? It seems like my approach ought to work.

Thank you.

Setting $_POST, Getting with input->post - El Forum - 06-01-2011

Okay, nm, it works exactly as I thought it should. I had another error that was masquerading as that described above. So, yeah, we're all fine here.

Setting $_POST, Getting with input->post - El Forum - 06-01-2011

I'd rather extend the Input-lib with a method to populate the $_POST var, just to keep it clean.

Setting $_POST, Getting with input->post - El Forum - 06-01-2011

That's a sound idea. If I were going make a habit of populating the $_POST var in production code (which I won't) I most certainly would, but as I'm only doing this as a surrogate form submit within the integration server test suite it doesn't seem necessary.

My initial confusion stemmed from my changing a variable name in one of the methods that I was testing but not changing it everywhere. That mistake caused the method to silently fail (not sure why it didn't raise an error). We are using Toast as the test framework and the developer who originally built it had a call to clear the database before every test that I didn't catch until later. It looked like the tests were running but not populating the database with anything. To me it looked like the $_POST variable wasn't being read by input->post() in the tested methods. Once the code was reorganized to not clear the database before every test the actual issue (and how the test framework behaves) became very clear.