Welcome Guest, Not a member yet? Register   Sign In
Setting $_POST, Getting with input->post
#1

[eluser]jlester[/eluser]
I am writing a test suite for an application and have model functions that look like:

Code:
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:

Code:
function test_controller() {
    $this->load->model('my_model');
    $_POST['var1'] = 'value1';
    $_POST['var2'] = 'value2';
    $ret = $this->my_model->model_function();
    $this->_assert_true($ret);
}

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.
#2

[eluser]jlester[/eluser]
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.
#3

[eluser]MathBoon[/eluser]
I'd rather extend the Input-lib with a method to populate the $_POST var, just to keep it clean.
#4

[eluser]jlester[/eluser]
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.




Theme © iAndrew 2016 - Forum software by © MyBB