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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:28:09+00:00 2026-06-17T20:28:09+00:00

I am implementing a module that will provide an API to work with and

  • 0

I am implementing a module that will provide an API to work with and manage PHP sessions. I am testing the Session\Manager implementation that will allow users to start sessions, set IDs, get IDs, destroy sessions, etc. I am testing the methods in this class in a separate process, by using PHPUnit’s @runInSeparateProcess annotation. When I use this annotation I get an exception thrown by PHPUnit due to an unserialization error. When I do not use the annotation the test runs as expected and fails on null not being equal to false.

Here’s the test causing the error. So far no implementation details have been made, all methods for the interface exist but perform no operations.

class ManagerTest extends PHPUnitTestCase {

    /**
     * Ensures that if the session has not been started yet the sessionExists
     * method returns false.
     *
     * @runInSeparateProcess
     */
    public function testSessionExistsWhenSessionHasNotBeenStarted() {
        $Manager = new \Session\Manager();
        $this->assertFalse($Manager->sessionExists());
    }

}

I was able to trace the problem down to the following PHPUnit_Util_PHP::runJob() method. I am running PHPUnit 3.7.5 and the runJob method being invoked is:

/**
 * Runs a single job (PHP code) using a separate PHP process.
 *
 * @param  string                       $job
 * @param  PHPUnit_Framework_TestCase   $test
 * @param  PHPUnit_Framework_TestResult $result
 * @return array|null
 * @throws PHPUnit_Framework_Exception
 */
public function runJob($job, PHPUnit_Framework_Test $test = NULL, PHPUnit_Framework_TestResult $result = NULL)
{
    $process = proc_open(
      $this->getPhpBinary(),
      array(
        0 => array('pipe', 'r'),
        1 => array('pipe', 'w'),
        2 => array('pipe', 'w')
      ),
      $pipes
    );

    if (!is_resource($process)) {
        throw new PHPUnit_Framework_Exception(
          'Unable to create process for process isolation.'
        );
    }

    if ($result !== NULL) {
        $result->startTest($test);
    }

    $this->process($pipes[0], $job);
    fclose($pipes[0]);

    $stdout = stream_get_contents($pipes[1]);
    fclose($pipes[1]);

    $stderr = stream_get_contents($pipes[2]);
    fclose($pipes[2]);

    proc_close($process);
    $this->cleanup();

    if ($result !== NULL) {
        $this->processChildResult($test, $result, $stdout, $stderr);
    } else {
        return array('stdout' => $stdout, 'stderr' => $stderr);
    }
}

The line $stdout = stream_get_contents($pipes[1]); results in $stdout being equal to a long string of ?. In $this->processChildResult the value in $stdout is unserialized and the invalid value passed to this function triggers a warning, resulting in an exception being thrown. I’ve also been able to determine that the return value of $this->getPhpBinary() is /usr/bin/php.

Exception message thrown:

PHPUnit_Framework_Exception: ???...???"*???...??


Caused by
ErrorException: unserialize(): Error at offset 0 of 10081 bytes

Thanks to hek2mgl the PHP code in $job can be reviewed at this gist holding the output of a var_dump on $job. I created a link as it is a fair amount of code and this question is already quite long.

I’ve reached the end of my knowledge for this particular domain and not sure how go about debugging this problem further. I’m not sure why the @runInSeparateProcess is failing and why the $stdout from running a separate process results in a long string of ? marks. What might be causing this issue and how could I go about resolving it? I’m at a standstill for this module as future tests will require running in a separate process to ensure sessions being started and destroyed don’t impact tests.

  • PHP: 5.3.15
  • PHPUnit: 3.7.5
  • Error caused in IDE and command line test runners
  • 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-17T20:28:11+00:00Added an answer on June 17, 2026 at 8:28 pm

    Ok, I figured out what is going on here and boy is it a doozy.

    During testing the framework attempts to preserve global state. In the PHPUnit_Framework_TestCase::run() function the current globals are converted into a string of PHP code by PHPUnit_Util_GlobalState::getGlobalsAsString(). I am currently using an autoloading library to take care of requiring appropriate class files. This class was declared in a variable that was in the global scope, causing a serialization of that object to be put into the $GLOBALS array created by getGlobalsAsString(). This serialization, for whatever reason, includes the null byte character \u0000. When you attempt to unserialize this you get an error and is ultimately causing the output to be corrupted as well.

    I fixed this issue by create a function in my test bootstrap that creates the autoloading object outside of the global scope. Now that the script is no longer attempting to unserialize a null byte the script produces no errors and the test case works as expected. This is a really funky issue, not sure if its an error in my code, PHPUnit’s code or an internal error with the serialize function.


    Alternatively you can override the run() function in your test case and explicitly set the test case to not preserve global state. This is probably a cleaner solution as you could easily run into other issues with requiring all included files again as is the default behavior.

    class ManagerTest extends PHPUnit_Framework_TestCase {
    
        public function run(PHPUnit_Framework_TestResult $result = null) {
            $this->setPreserveGlobalState(false);
            parent::run($result);
        }
    
    }
    

    Global state must be set before the PHPUnit_Framework_TestCase::run() function is invoked. This is likely the only way to reliably set the global state preservation properly.

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

Sidebar

Related Questions

I'm implementing an API that must accept fairly large file uploads. The request will
I've searched around and found that when implementing an authentication module in MVC architecture
implementing publishActivity in PHP using the REST API using this code: $activity = array(
Documentation for logging module says that If you are implementing asynchronous signal handlers using
I am implementing a search module with result page support paging. The example provided
I am implementing this hoemwork functionality using Ocaml: Not allowed to use List module
Implementing the ScriptControlClass was extremely easy, unfortunately the side effects with the language implementation
Implementing a custom membership provider, there are certain properties such as MinRequiredPasswordLength that only
when implementing a recursive function during development, i will use a counter and exit
I am interfacing an embedded device with a camera module that returns a single

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.