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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:13:36+00:00 2026-05-20T22:13:36+00:00

I’m unit testing and refactoring a large code base with many utility libraries in

  • 0

I’m unit testing and refactoring a large code base with many utility libraries in PHP.

There’s many libraries like this, filled with convenience methods used all over the site. Most of these static libraries interact with the configuration files (via another static class). Here’s a good example:

class core_lang {
    public static function set_timezone()
    {
        if(cfg::exists('time_zone')) {
            putenv("TZ=".cfg::get('time_zone'));
        }
    }
}

Then, of course, there’s another layer of more specific libraries elsewhere calling core_lang:: set_timezone() inside another function.

That’s making these classes VERY hard to write unit tests for, at least in PHPUnit, since you can only mock… basically one level in.

I ordered the book Working Effectively with Legacy Code, but what are some strategies for starting to refactor and manage this type of code for testability?

  • 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-05-20T22:13:37+00:00Added an answer on May 20, 2026 at 10:13 pm

    The most important principle to reduce coupling is Dependency Injection. There are many methods to actually implement it, but the base concept is the same:

    Don’t hardcode dependencies into your code, ask for them instead.

    In your particular example, one method to do this right is the following:

    You define an interface (let’s call it ExistenceChecker for now) which exposes a method called ‘exists()’. In production code, you create a class which actually implements the method (let’s call it ConcreteExistenceChecker), and you ask for an ExistenceChecker object in the constructor of core_lang. This way you can pass a stub object which implement this interface (but with a dead simple trivial implementation) while you unit test your code. From now on, you don’t have to depend on a concrete class, just an interface, which introduces far less coupling.

    Let me demonstrate it with a bit of code:

    interface ExistenceChecker {
        public function exists($timezone);
    }
    
    class ConcreteExistenceChecker implements ExistenceChecker {
        public function exists($timezone) {
            // do something and return a value
        }
    }
    
    class ExistenceCheckerStub implements ExistenceChecker {
        public function exists($timezone) {
            return true; // trivial implementation for testing purposes
        }
    }
    
    class core_lang {    
        public function set_timezone(ExistenceChecker $ec)
        {
            if($ec->exists('time_zone')) {
                putenv("TZ=".cfg::get('time_zone'));
            }
        }
    }
    

    Production code:

    // setting timezone
    $cl = new core_lang();
    $cl->set_timezone(new ConcreteExistenceChecker()); // this will do the real work
    

    Test code:

    // setting timezone
    $cl = new core_lang();
    $cl->set_timezone(new ExistenceCheckerStub()); // this will do the mocked stuff
    

    You can read more about this concept here.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I know there's a lot of other questions out there that deal with this
I have some data like this: 1 2 3 4 5 9 2 6

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.