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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:05:06+00:00 2026-06-02T04:05:06+00:00

PHP 5.3.10, PHPUnit 3.6.10 I am calling code in a test that fails to

  • 0

PHP 5.3.10, PHPUnit 3.6.10

I am calling code in a test that fails to initialize an object so that a method may be used from it. The problem is this is legacy code, and this function is constantly used in our application. It works under PHP natively, but not when run from within PHPUnit. The calls access a PHP Extension we have for our application.
A Very simple example to demonstrate the issue is below.

Class file to wrap calls to our PHP Extension (FOO):

class FOO
{
    $private $Bar;

    pubilc function __construct()
    {
        $this->Bar = new OurPHPExtensionObject();
    }

    public function Option1($Variable)
    {
        return $this->Bar->OurPHPExtensionMethod($Variable);
    }

    public function _Option1($Variable)
    {
        return $Variable; // Dummy to test that code is loaded
    }
}
$Bar = new FOO();

This creates the object and loads initial data so it is only done once. Other calls to use this include a file which then includes this class. As the application continues to run, the only initialization is done the first time, then the global keyword is used to reference this object to access it’s methods.

Function.php example:

require_once('FOO.class');
function CausesError($Parameter)
{
    global $Bar;    // Using $Bar defined from FOO.class
    $ErrorCreated = $Bar->Option1($Parameter);  // Call Option from Class
}

Then, individual programs simply inclde the Function.php file with require_once() to ensure there is only 1 instance of the file loaded from anywhere.

Sample File:

require_once('Function.php');
echo CausesError('Variable Text') . "\n";

This causes the error:

Fatal error: Call to a member function Option1() on a non-object in ...

Being new to PHPUnit, and trying to minimize changes to the existing code until I can get the tests in place, I need some suggestions on how to handle this. A large amount of the existing code is calling this function by simply including the Function.php, then calling the function (CausesError). I am not sure how to get this to return the data.

My test file then has a setUp() method before each test.

class TEST_Function extends PHPUnit_Framework_TestCase
{
    protected function setUp()
    {
        require_once('Function.php);
        echo _Option1('Sample') . "\n"; // Works and ECHOs to show code loaded
        echo Option1('Sample2') . "\n"; // Fails with the error
    }

    public function testFunctionOption1()
    {
        $this->assertEquals(Option1('English'), 'English'); // Error
    }
}

Should I mock the function in my test? I am not sure which tests may include this since the function is used through out the code.

Hope this describes the environment and code layout. Thanks in advance for any assistance.

  • 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-02T04:05:09+00:00Added an answer on June 2, 2026 at 4:05 am

    After reading through your question I’d assume that you’re running into some other problem:

    require_once('Function.php');
    

    will ensure the file is loaded only once. So the assignment of new FOO to $Bar is done only once. As that variable therefore only exists within the scope of a single test function, it’s only available with in the setUp() function.

    But this might not be the case, more specifics are missing to properly say what goes on behind the scenes as you have not posted the definition of _Option1() and Option1().

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

Sidebar

Related Questions

With PHPUnit it's quite easy to test raw PHP code, but what about code
i have this simple rule: .PHONY: test test: src/*Test.php $(PHPUNIT) $?;\ it will look
Is there a simple Web interface to running PHPUnit test suites? i.e. a PHP
Talk about falling at the first hurdle. My test script: <?php require_once('PHPUnit/Framework.php'); class TransferResponseTest
PHP_CodeCoverage 1.1 removed the singleton accessor for PHP_CodeCoverage_Filter that allowed our PHPUnit bootstrap.php files
I'm getting started unit testing my PHP application with PHPUnit. I understand that it's
I am trying to create a mock object in PHP and PHPUnit. So far,
Situation I'm implementing a Listener for PHPUnit that will record test results to a
How should be test with phpunit php web application for xss + sql injection?
My quest of starting to use namespaces in PHP keeps continuing. This time PHPUnit

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.