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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:26:51+00:00 2026-06-13T11:26:51+00:00

Possible Duplicate: PHP – override existing function I want to use mocking to unit

  • 0

Possible Duplicate:
PHP – override existing function

I want to use mocking to unit test some of my functions that have external dependencies.

So here goes… Below is a simplified model of what I’m working with. A lot of the code I have inherited is written in this way, so I would like to be able to write some simple tests without having to rewrite if possible:

function display_tasks($id)
{
    $tasks = call_some_function_that_runs_a_database_query($id);

    $html = "<some html>";

    foreach ($tasks as $task)
    {
        // Some operations here
    }

    $html .= "</some html>";

    return $html;
}

The code that my function is calling is not wrapped in an object, so I am having trouble putting the various examples into practice, as 99% of the examples on the internet are based on object-oriented code. Is it even possible to mock procedural functions directly from PHPunit?

I would like to be able to mock out whatever call_some_function_that_runs_a_database_query is returning. I have had a go at using the built-in PHPunit mocking methods, but doing this doesn’t override the result from the original call in my function.

Any help or examples would be really appreciated.

  • 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-13T11:26:52+00:00Added an answer on June 13, 2026 at 11:26 am

    You can use the runkit PHP extension to replace a function with one that will return what you want during the test. It’s more cumbersome than mocking objects because you have to take care to swap the original back in after the test under all cases (pass, fail, error).

    Function Under Test

    function getTasksFromDatabase($id) {
        // call to database...
    }
    

    Test Case

    function stub_getTasksFromDatabase($id) {
        return array('one', 'two', 'three');
    }
    
    class getUserTest extends PHPUnit_Framework_TestCase {
        public static function setUpBeforeClass() {
            runkit_function_rename('getTasksFromDatabase', 'orig_getTasksFromDatabase');
            runkit_function_rename('stub_getTasksFromDatabase', 'getTasksFromDatabase');
        }
    
        public function tearDownAfterClass() {
            runkit_function_rename('getTasksFromDatabase', 'stub_getTasksFromDatabase');
            runkit_function_rename('orig_getTasksFromDatabase', 'getTasksFromDatabase');
        }
    
        public function setUp() {
            $html = getTasks(1);
            self::assertEquals('<some html>...one...two...three...</some html>', $html);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: PHP method chaining? I occasionally see some php applications use classes like
Possible Duplicate: PHP and Enums I want to create a function with several parameters
Possible Duplicate: PHP create a file without fopen I want to create a file
Possible Duplicate: php false place in condition I have noticed that a lot of
Possible Duplicate: PHP function to strip tags, except a list of whitelisted tags and
Possible Duplicate: PHP remove special character from string I want to find a regex
Possible Duplicate: php mail() function on localhost I am using php mail(). I have
Possible Duplicate: PHP class instantiation. To use or not to use the parenthesis? Omission
Possible Duplicate: PHP - send file to user I have written a script that
Possible Duplicate: PHP date time Trying to add one second to a datetime that

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.