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 8795997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:31:07+00:00 2026-06-13T23:31:07+00:00

Given the following implementation: class Foo { public function helper() { // Does something

  • 0

Given the following implementation:

class Foo {
    public function helper() {
        // Does something with external side effects, like updating
        // a database or altering the file system.
    }

    public function bar() {
        if ($this->helper() === FALSE) {
            throw new Exception(/* ... */);
        }
    }
}

How would I unit test Foo::bar() without incurring the side effects of Foo::helper() during testing?

I know I can mock Foo and stub Foo::helper():

public function testGoodBar() {
    $mock = $this->getMock('Foo', array('helper'));
    $this->expects($this->once())
        ->method('helper')
        ->will($this->returnValue(TRUE));

    $this->assertTrue($mock->bar());
}

…but this leaves the test wide open for code changes that introduce other methods which may have side effects. Then if the test is run again without being updated, the test itself will have permanent side effects.

I could also mock Foo, such that all its methods get mocked and won’t produce side effects:

public function testGoodBar() {
    $mock = $this->getMock('Foo');
    $this->expects($this->once())
        ->method('helper')
        ->will($this->returnValue(TRUE));

    $this->assertTrue($mock->bar());
}

…but then even Foo::bar() gets mocked, which is bad, since that’s the method we want to test.

The only solution I could come up with is explicitly mocking all methods except the one under test:

public function testGoodBar() {
    $mock = $this->getMock('Foo', array_diff(
        get_class_methods('Foo'),
        'bar'
    ));

    $this->expects($this->once())
        ->method('helper')
        ->will($this->returnValue(TRUE));

    $this->assertTrue($mock->bar());
}

…but this seems kludgy, and I feel like I’m missing something obvious.

  • 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-06-13T23:31:09+00:00Added an answer on June 13, 2026 at 11:31 pm

    (Take the comments under the question into consideration for this answer.)

    If you’re extending a class whose sole purpose is to have side effects, I’d expect all of the extension code to also produce side effects. Hence you’ll have to account for that in your tests and set up an environment in which you can test code with side effects (i.e.: get a memcached instance up and running for this test).

    If you do not want this (understandably), it would be better to write your code as a wrapper around the side-effect class in a way that it is mockable. So, your Foo::__construct accepts an instance of or factory for the class which produces side effects, so you can mock it in your tests to test only your side-effect-free code.

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

Sidebar

Related Questions

Given the following interface and implementation class: public interface MyInterface { public String getSomething
Given I have two File objects I can think of the following implementation: public
Given the following two interfaces (these are small examples, not my actual implementation): public
I have the following: class Parent { public: virtual bool foo(vector<string> arg1, vector<string> arg2)
Given the following: class Base<T> {/*...*/} class Der<T>: Base<T> {/*...*/} interface Sth<T>{ IEnumerable<Base<T>> Foo
Given the following: class BC { public void Display() { System.Console.WriteLine(BC::Display); } } class
given the following class definition: public class Order { public IProduct Product {get;set;} }
Given the following interface: public interface IFoo { bool Foo(Person a, Person b); }
Given the following classes: public class ContentItem : IEquatable<ContentItem> { ... } and public
Given the class below does anyone know why EclipseLink implementation of JPA fails to

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.