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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:13:08+00:00 2026-05-11T00:13:08+00:00

I am trying to test a class that manages data access in the database

  • 0

I am trying to test a class that manages data access in the database (you know, CRUD, essentially). The DB library we’re using happens to have an API wherein you first get the table object by a static call:

function getFoo($id) {   $MyTableRepresentation = DB_DataObject::factory('mytable');   $MyTableRepresentation->get($id);   ... do some stuff   return $somedata } 

…you get the idea.

We’re trying to test this method, but mocking the DataObject stuff so that (a) we don’t need an actual db connection for the test, and (b) we don’t even need to include the DB_DataObject lib for the test.

However, in PHPUnit I can’t seem to get $this->getMock() to appropriately set up a static call. I have…

        $DB_DataObject = $this->getMock('DB_DataObject', array('factory')); 

…but the test still says unknown method ‘factory’. I know it’s creating the object, because before it said it couldn’t find DB_DataObject. Now it can. But, no method?

What I really want to do is to have two mock objects, one for the table object returned as well. So, not only do I need to specify that factory is a static call, but also that it returns some specified other mock object that I’ve already set up.

I should mention as a caveat that I did this in SimpleTest a while ago (can’t find the code) and it worked fine.

What gives?

[UPDATE]

I am starting to grasp that it has something to do with expects()

  • 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-11T00:13:08+00:00Added an answer on May 11, 2026 at 12:13 am

    I agree with both of you that it would be better not to use a static call. However, I guess I forgot to mention that DB_DataObject is a third party library, and the static call is their best practice for their code usage, not ours. There are other ways to use their objects that involve constructing the returned object directly. It just leaves those darned include/require statements in whatever class file is using that DB_DO class. That sucks because the tests will break (or just not be isolated) if you’re meanwhile trying to mock a class of the same name in your test–at least I think.

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

Sidebar

Related Questions

I'm trying to Unit Test a class that has many internal functions. These obviously
I am trying to test the likelihood that a particular clustering of data has
I'm trying to use PHPUnit to unit test some class methods that return SQL.
I am trying to test that a particular method throws an expected exception from
I am trying to test a proof of concept that I can run a
I'm trying to test a method I have in my application, but I don't
I'm trying to test a proposition that one of our vendors presented to us
I am trying to create a generic callback object that will hold arbitrary data
I'm trying to write a unit test with phpunit for a model that uses
I'm trying to figure out how to unit test my object that persists itself

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.