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

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?

  • 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. 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

Related Questions

I have some objects which I draw onto a Canvas as part of a
Given an object that has a variable which it doesn't own; that is, the
I have a list of objects which I built with a class, and one
I have a Array of objects which is something like this : SomeObject (Array)
Is it better to put a default implementation of a method in a superclass,
If I have an object that is composed of nested complex types, e.g. a
I have a MEF based solution that has several exported implementations of an interface.
I come from heavy web application dev background, and having some problems representing my
I am confused about the strange syntax provided by C++ function templates and class
I want to bind to a resource (DynamicResource) and access properties on that resource,

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.