Issues with mocking EE->TMPL->fetch_param

Douglas's Avatar

Douglas

25 Jan, 2012 08:54 PM via web

Hello there,

I'm writing some unit tests for a plugin that uses the fetch_param() method on the TMPL class, and having some troubles. Not entirely sure if it's a SimpleTest thing, but since the TMPL class is being mocked by Testee, I figured I'd ask here first.

At any rate, here's some code:

class TwitterTweetTemplateParams extends Testee_unit_test_case {
    private $_params;

    public function setUp(){
        parent::setUp();
        $this->_params = new TemplateParams($this->EE->TMPL);
    }
    public function testGetOptionTypeWithTagParam(){
        $this->EE->TMPL->returns('fetch_param', 'option1', array('type'));
        $this->assertIdentical('option1', $this->_params->getType());
    }
}

and the class I'm testing contains a method like this:

function getType() {
    return $this->_eeTemplate->fetch_param('type', 'option2');
}

but when my tests run, the Mocked TMPL class returns NULL for any fetch_param() call that includes the default parameter, causing my tests to fail.

Any advice here on how to get this working?

  1. Support Staff 2 Posted by Stephen Lewis on 25 Jan, 2012 09:01 PM

    Stephen Lewis's Avatar

    Hi Douglas,

    Your test syntax doesn't look like standard SimpleTest syntax to me, and I'm really not sure what this line means:

    return $this->_eeTemplate->fetch_param('type', 'option2');
    

    If you can give me a few more details on what _eeTemplate is, and what you're trying to achieve, I should be able to help.

    Cheers,
    Stephen

  2. 3 Posted by Douglas on 25 Jan, 2012 09:16 PM

    Douglas's Avatar

    Ah, apologies. The function in the second code snippet is the one I am trying to test against, not the actual SimpleTest assertion.

    The getType() method below returns the value for the given plugin tag parameter from the EE Template class that's passed to the TemplateParams constructor.

    Here's a complete version.

    class TemplateParams {
    
        private $_eeTemplate;
    
        public function __construct(EE_Template $eeTemplate) {
            $this->_eeTemplate = $eeTemplate;
        }
        function getType() {
            return $this->_eeTemplate->fetch_param('type', 'html5');
        }
    }
    
  3. Support Staff 4 Posted by Stephen Lewis on 25 Jan, 2012 10:19 PM

    Stephen Lewis's Avatar

    Hi Douglas,

    Is there a reason you're creating a new instance of the EE_Template class, and passing it to your constructor?

    That's not to say it's impossible to test that, just that I don't understand why you're doing it, and thought it best to check that this isn't simply a misunderstanding regarding the typical usage of the EE_Template class.

    Assuming this is what you're trying to achieve, I'd do something like this.

    require_once APPPATH .'libraries/Template.php';
    
    class Test_TemplateParams extends Testee_unit_test_case {
    
        private $_subject;
        private $_tmpl;
    
        public function setUp()
        {
            parent::setUp();
    
            /**
             * Generates the mock EE_Template class, with a class name of
             * Test_TemplateParams_mock_template.
             *
             * Including the class name in the generated mock class guards
             * against a redeclared class error.
             */
            Mock::generate('EE_Template', get_class($this) .'_mock_template');
    
            // Get an instance of our newly-created mock.
            $this->_tmpl = $this->_get_mock('template');
    
            // Create the test subject, and pass in the mock template.
            $this->_subject = new TemplateParams($this->_tmpl);
        }
    
    
        function test__getType__retrieves_the_requested_parameter_from_the_template_object()
        {
            $param_key  = 'my_key';
            $param_val  = 'my_val';
            $fallback   = 'default_val';
    
            $this->_tmpl->expectOnce('fetch_param', array($param_key, $fallback));
            $this->_tmpl->setReturnValue('fetch_param', $param_val, array($param_key, $fallback));
    
            $this->assertIdentical($param_val, $this->_subject->getType($param_key));
        }
    
    }
    

    Cheers,
    Stephen

Reply to this discussion

Internal reply

Formatting help or Preview

Attached Files

You can attach files up to 10MB

What is five times five?

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.