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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:56:09+00:00 2026-06-12T06:56:09+00:00

public function thisMethod { $example = $this->methodReturnsObject()->this1->that2->there3->id; return $example; } How would you test

  • 0
public function thisMethod {
    $example = $this->methodReturnsObject()->this1->that2->there3->id;
    return $example;
}

How would you test thisMethod in PHPUnit?

Obviously I could write an expectation that methodReturnsObject() will return something… but what? That object has properties associated with it, but how would you even mock that value?

  • 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-12T06:56:10+00:00Added an answer on June 12, 2026 at 6:56 am

    The answer is “You don’t”. Unit testing should test each class in isolation, what you are trying to do there is not a unit test. As I said in my comment, you are breaking the Law of Demeter, which simply stated says

    • Each unit should have only limited knowledge about other units: only units “closely” related to the current unit.
    • Each unit should only talk to its friends; don’t talk to strangers.
    • Only talk to your immediate friends.

    You have tightly coupled classes there that need re-factoring. I have written the classes first here to illustrate the point, but I usually write the tests first.

    Lets start with the end of the chain:-

    class there3
    {
        private $id
    
        public function setId($id)
        {
            $this->id = $id;
        }
    
        public function getId()
        {
            return $this->id;
        }
    
    }
    

    Now let’s set up a unit test for it:-

    class there3Test extends PHPUnit_Framework_TestCase
    {
        public function testCanGetId()
        {
            $there3 = new there3();
            $there3->setId(3);
            $this->assertTrue($there3->getId() === 3);
        }
    }
    

    That class is now tested, so we don’t need to test it again. Now let’s look at the next one:-

    class this2
    {
        public $there3;
    
        //To facilitate unit testing we inject the dependency so we can mock it
        public function __construct(there3 $there3)
        {
            $this->there3 = $there3;
        }
    
        public function getId()
        {
            return $this->there3->getId();
        }
    
    }
    

    And now the unit test:-

    class this2Test extends PHPUnit_Framework_TestCase
    {
        public function testCanGetId()
        {
            $mockThere3 = $this->getMock('there3');
            $mockThere3->method('getId')
                       ->will($this->returnValue(3);
    
            $this2 = new this2($mockThere3);//We pass in the mock object instead of the real one
            $this->assertTrue($this2->getId() === 3);
        }
    }
    

    We’ll do one last example to further illustrate my point:-

    class this1
    {
        private $this2;
    
        public function __construct(this2 $this2)//injecting again
        {
             $this->$this2 = $this2;
        }
    
        public function getId()
        {
            return $this->$this2->getId();
        }
    }
    

    And, again, the unit test:-

    class this1Test extends PHPUnit_Framework_TestCase
    {
        public function testCanGetId()
        {
            $mockThis2 = $this->getMock('this2');
            $mockThis2->method('getId')
                      ->will($this->returnValue(3);
    
            $this1 = new this1($mockThis2);//We pass in the mock object instead of the real one
            $this->assertTrue($this1->getId() === 3);
        }
    }
    

    Hopefully, you get the idea without me having to go through all the objects in your example.

    What I have done is to de-couple the classes from each other. They only have knowledge of the object they depend on, they don’t care how that object gets the information requested.

    Now the call for id would look something like:-

    public function getId()
    {
        return $this->this1->getId();
    }
    

    Which will go up the chain until the id returned is there2::id. You never have to write something like $this->$this1->$this2->there3->id and you can unit test your classes properly.

    For more information on unit testing see the PHPUnit manual.

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

Sidebar

Related Questions

public function create($tableName, $userInput) { $this->db->insert($tableName, $userInput); return $this->db->affected_rows(); } As you see, $this->db->affected_rows();
Consider this trivial function: public static bool IsPositive(IComparable<int> value) { return value.CompareTo(0) > 0;
I have this method : public function getInstance() { $classname = $this->getFullyQualifiedClassName(); return new
I have the following function: public void Test(IDataContext context) { return (From c In
public function getItemsCollection($useCache = true) { if (is_null($this->_items)) { $this->_items = Mage::getModel('sales/quote_item')->getCollection(); $this->_items->setQuote($this); }
public function __construct($input = null) { if (empty($input)){ return false; } and then there's
public function test(){ $data = ORM::factory('testdata')->find_all(); Table::factory() ->set_body_data($data) ->set_row_titles('id') ->set_column_titles(Table::AUTO) ->set_callback('format_row', 'row') ->render(true); $this->template->title
public function configure() { $this->widgetSchema['start_date'] = new sfWidgetFormInput(); $this->widgetSchema['end_date'] = new sfWidgetFormInput(); $this->validatorSchema->setPostValidator( new
Public Function CastToT(Of T)(ByVal GenericType(Of Object) data) As GenericType(Of T) Return DirectCast(data, GenericType(Of T))
public function index() { $this->redirect('/'); } I will just go insane soon... How can

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.