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 132637

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:17:38+00:00 2026-05-11T06:17:38+00:00

For objects which compose another object as part of their implementation, what’s the best

  • 0

For objects which compose another object as part of their implementation, what’s the best way to write the unit test so only the principle object gets tested? Trivial example:

class myObj {      public function doSomethingWhichIsLogged()     {         // ...         $logger = new logger('/tmp/log.txt');         $logger->info('some message');         // ...     } } 

I know that the object could be designed so that the logger object dependency could be injected and hence mocked in a unit test, but that’s not always the case – in more complicated scenarios, you do need to compose other objects or make calls to static methods.

As we don’t want to test the logger object, only the myObj, how do we proceed? Do we create a stubbed ‘double’ with the test script? Something like:

class logger {     public function __construct($filepath) {}     public function info($message) {} }  class TestMyObj extends PHPUnit_Framework_TestCase  {     // ... } 

This seems feasible for small objects but would be a pain for more complicated APIs where the SUT depended on the return values. Also, what if you want to test the calls to the dependency object in the same was you can with mock objects? Is there a way of mocking objects which are instantiated by the SUT rather than being passed in?

I’ve read the man page on mocks but it doesn’t seem to cover this situation where the dependency is composed rather than aggregated. How do you do it?

  • 0 0 Answers
  • 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. 2026-05-11T06:17:39+00:00Added an answer on May 11, 2026 at 6:17 am

    As you seem to be aware already, Concrete Class Dependencies makes testing hard (or outright impossible). You need to decouple that dependency. A simple change, that doesn’t break the existing API, is to default to the current behaviour, but provide a hook to override it. There are a number of ways that this could be implemented.

    Some languages have tools that can inject mock classes into code, but I don’t know of anything like this for PHP. In most cases, you would probably be better off refactoring your code anyway.

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

Sidebar

Ask A Question

Stats

  • Questions 189k
  • Answers 189k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer std::exception is the class whose only purpose is to serve… May 12, 2026 at 5:49 pm
  • Editorial Team
    Editorial Team added an answer Probably your fastest way would be to store your movable… May 12, 2026 at 5:49 pm
  • Editorial Team
    Editorial Team added an answer Fixed by putting DataKeyNames="CategoryUID" in the gridview parameters. May 12, 2026 at 5:49 pm

Related Questions

For objects which compose another object as part of their implementation, what's the best
I want to return two values, one of which is a new object. I
I have a view which is composed of top, left and bottom headers and
I had a previous question that touched this topic ( Rails: how to get
Let's say you want to diagram a Map class (the mathematical kind, not the

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.