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

  • Home
  • SEARCH
  • 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 1018295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:50:12+00:00 2026-05-16T10:50:12+00:00

How I’m stuck with writing a test for the following code. I want to

  • 0

How I’m stuck with writing a test for the following code. I want to mock the $userModel but how can I add this to the test?

class PC_Validate_UserEmailDoesNotExist extends Zend_Validate_Abstract
{
    public function isValid($email, $context = NULL)
    {
        $userModel = new Application_Model_User();
        $user = $userModel->findByEmailReseller($email, $context['reseller']);

        if ($user == NULL) {
            return TRUE;
        } else {
            return FALSE;
        }
    }
}

Update: the Solution

I did change my class to the following to get it testable, it now uses dependency injection. More information about dependency injection you van find out here

I now call the class like this:

new PC_Validate_UserEmailDoesNotExist(new Application_Model_User()

The refactored class

class PC_Validate_UserEmailDoesNotExist extends Zend_Validate_Abstract
{
    protected $_userModel;

    public function  __construct($model)
    {
        $this->_userModel = $model;
    }

    public function isValid($email, $context = NULL)
    {
        if ($this->_userModel->findByEmailReseller($email, $context['reseller']) == NULL) {
            return TRUE;
        } else {
            return FALSE;
        }
    }
}

The unit test

class PC_Validate_UserEmailDoesNotExistTest extends BaseControllerTestCase
{
    protected $_userModelMock;

    public function setUp()
    {
        parent::setUp();
        $this->_userModelMock = $this->getMock('Application_Model_User', array('findByEmailReseller'));
    }

    public function testIsValid()
    {
        $this->_userModelMock->expects($this->once())
                        ->method('findByEmailReseller')
                        ->will($this->returnValue(NULL));

        $validate = new PC_Validate_UserEmailDoesNotExist($this->_userModelMock);
        $this->assertTrue(
                $validate->isValid('jef@test.com', NULL),
                'The email shouldn\'t exist'
        );
    }

    public function testIsNotValid()
    {
        $userStub = new \Entities\User();

        $this->_userModelMock->expects($this->once())
                        ->method('findByEmailReseller')
                        ->will($this->returnValue($userStub));

        $validate = new PC_Validate_UserEmailDoesNotExist($this->_userModelMock);
        $this->assertFalse(
                $validate->isValid('jef@test.com', NULL),
                'The email should exist'
        );
    }
}
  • 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-16T10:50:13+00:00Added an answer on May 16, 2026 at 10:50 am

    Short answer: you cant, because you hardcoded the dependency into the method.

    There is three workarounds for this:

    1) Make the used classname configurable, so you can do something like:

    $className = $this->userModelClassName;
    $userModel = new $className();
    

    or 2) Add a third param to the method signature that allows passing in the dependency

    public function isValid($email, $context = NULL, $userModel = NULL)
    {
        if($userModel === NULL)
        {
            $userModel = new Application_Model_User();
        }
        // ...
    }
    

    or 3) use set_new_overload() as described in

    • http://sebastian-bergmann.de/archives/885-Stubbing-Hard-Coded-Dependencies.html
    • http://github.com/sebastianbergmann/php-test-helpers

    Note: the Test-Helper extension is superseded by https://github.com/krakjoe/uopz

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

Sidebar

Related Questions

No related questions found

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.