Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6006397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:32:00+00:00 2026-05-23T01:32:00+00:00

This is a follow-on from a previous question I had: How to decouple my

  • 0

This is a follow-on from a previous question I had: How to decouple my data layer better and restrict the scope of my unit tests?

I’ve read around on Zend and DI/IoC and came up with the following changes to my code:

Module Bootstrap

class Api_Bootstrap extends Zend_Application_Module_Bootstrap
{
    protected function _initAllowedMethods()
    {
        $front = Zend_Controller_Front::getInstance();
        $front->setParam('api_allowedMethods', array('POST'));
    }

    protected function _initResourceLoader()
    {
        $resourceLoader = $this->getResourceLoader();
        $resourceLoader->addResourceType('actionhelper', 'controllers/helpers', 'Controller_Action_Helper');
    }

    protected function _initActionHelpers()
    {
        Zend_Controller_Action_HelperBroker::addHelper(new Api_Controller_Action_Helper_Model());
    }
}

Action Helper

class Api_Controller_Action_Helper_Model extends Zend_Controller_Action_Helper_Abstract
{
    public function preDispatch()
    {
        if ($this->_actionController->getRequest()->getModuleName() != 'api') {
            return;
        }

        $this->_actionController->addMapper('account', new Application_Model_Mapper_Account());
        $this->_actionController->addMapper('product', new Application_Model_Mapper_Product());
        $this->_actionController->addMapper('subscription', new Application_Model_Mapper_Subscription());
    }
}

Controller

class Api_AuthController extends AMH_Controller
{
    protected $_mappers = array();

    public function addMapper($name, $mapper)
    {
        $this->_mappers[$name] = $mapper;
    }

    public function validateUserAction()
    {
        // stuff

        $accounts = $this->_mappers['account']->find(array('username' => $username, 'password' => $password));

        // stuff
    }
}

So, now, the controller doesn’t care what specific classes the mappers are – so long as there is a mapper…

But how do I now replace those classes with mocks for unit-testing without making the application/controller aware that it is being tested? All I can think of is putting something in the action helper to detect the current application enviroment and load the mocks directly:

class Api_Controller_Action_Helper_Model extends Zend_Controller_Action_Helper_Abstract
{
    public function preDispatch()
    {
        if ($this->_actionController->getRequest()->getModuleName() != 'api') {
            return;
        }

        if (APPLICATION_ENV != 'testing') {
            $this->_actionController->addMapper('account', new Application_Model_Mapper_Account());
            $this->_actionController->addMapper('product', new Application_Model_Mapper_Product());
            $this->_actionController->addMapper('subscription', new Application_Model_Mapper_Subscription());
        } else {
            $this->_actionController->addMapper('account', new Application_Model_Mapper_AccountMock());
            $this->_actionController->addMapper('product', new Application_Model_Mapper_ProductMock());
            $this->_actionController->addMapper('subscription', new Application_Model_Mapper_SubscriptionMock());
        }
    }
}

This just seems wrong…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T01:32:01+00:00Added an answer on May 23, 2026 at 1:32 am

    So, after a few misses, I settled on rewriting the action helper:

    class Api_Controller_Action_Helper_Model extends Zend_Controller_Action_Helper_Abstract
    {
        public function preDispatch()
        {
            if ($this->_actionController->getRequest()->getModuleName() != 'api') {
                return;
            }
    
            $registry = Zend_Registry::getInstance();
            $mappers = array();
            if ($registry->offsetExists('mappers')) {
                $mappers = $registry->get('mappers');
            }
    
            $this->_actionController->addMapper('account', (isset($mappers['account']) ? $mappers['account'] : new Application_Model_Mapper_Account()));
            $this->_actionController->addMapper('product', (isset($mappers['product']) ? $mappers['product'] : new Application_Model_Mapper_Product()));
            $this->_actionController->addMapper('subscription', (isset($mappers['subscription']) ? $mappers['subscription'] : new Application_Model_Mapper_Subscription()));
        }
    }
    

    This means that I can inject any class I like via the registry, but have a default/fallback to the actual mapper.

    My test case is:

    public function testPostValidateAccount($message)
    {
        $request = $this->getRequest();
        $request->setMethod('POST');
        $request->setRawBody(file_get_contents($message));
    
        $account = $this->getMock('Application_Model_Account');
    
        $accountMapper = $this->getMock('Application_Model_Mapper_Account');
        $accountMapper->expects($this->any())
            ->method('find')
            ->with($this->equalTo(array('username' => 'sjones', 'password' => 'test')))
            ->will($this->returnValue($accountMapper));
        $accountMapper->expects($this->any())
            ->method('count')
            ->will($this->returnValue(1));
        $accountMapper->expects($this->any())
            ->method('offsetGet')
            ->with($this->equalTo(0))
            ->will($this->returnValue($account));
    
        Zend_Registry::set('mappers', array(
            'account' => $accountMapper,
        ));
    
        $this->dispatch('/api/auth/validate-user');
    
        $this->assertModule('api');
        $this->assertController('auth');
        $this->assertAction('validate-user');
        $this->assertResponseCode(200);
    
        $expectedResponse = file_get_contents(dirname(__FILE__) . '/_testPostValidateAccount/response.xml');
    
        $this->assertEquals($expectedResponse, $this->getResponse()->outputBody());
    }
    

    And I make sure that I clear the default Zend_Registry instance in my tearDown()

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a follow up from my previous question I have this code basically
This is a follow on from my previous question although this is about something
This is a follow-on from a previous SO question Anchoring CSS Repeating Background Image
This is a follow-up on a previous question I had ( Complexity of STL
This is something of a follow up from a previous question . The requirements
I suppose this question is a follow up to a previous question I had
(This is a follow-up from this previous question ). I was able to successfully
This is a follow-on from a previous question, in the implementation, I have two
All, this is a follow up from a previous question here: C# formatting external
Note that this is a follow-up question from my previous one: How to memorize

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.