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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:24:06+00:00 2026-06-17T21:24:06+00:00

I am trying to implement some unit tests into a legacy PHP application. There

  • 0

I am trying to implement some unit tests into a legacy PHP application.

There have been a number of challenges with this, but in particular for this question, I am currently looking at a small class that manages the app config.

The class interface is pretty simple; it does the following:

  • The constructor calls a Populate method, that uses our Recordset class to load the config for the requested module from the database.
  • Get method, which returns a specified config value.
  • Set method, which sets the config value in memory.
  • Save method, which writes the config update(s) back to the DB.

Testing the Get/Set methods is straightforward; they map directly to private array, and work pretty much as you’d expect.

The problem I have is with testing the database handling. The class uses a number of fields on the config table (module name, language, etc) to determine which config items to load and in what priority. In order to do so, it constructs a series of elaborate SQL strings, and then makes direct calls to the DB to get the correct config data.

I have no idea how to go about writing a unit test for this. Aside from the Get/Set methods, the class consists pretty much entirely of building SQL strings and running them.

I can’t see a way to test it sensibly without actually running it against a real DB, and all the issues that go with that — if nothing else, the complexity of the config loader would mean I’d need at least seven or eight test databases populated with slightly different config. It seems like it would be unmanageable and fragile, which would defeat the point somewhat.

Can anyone suggest how I should proceed from here? Is it even possible to unit test this kind of class?

Many thanks.

  • 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-17T21:24:08+00:00Added an answer on June 17, 2026 at 9:24 pm

    I must say I’m not sure I agree that unit tests would be somewhat pointless without hitting the database here. My goal would be to get the business logic that produces the SQL under test, without involving your database. Here is an example of what I’m talking about:

    class Foo {
    
        // ... Getters and setters for your config ...
    
        public function doSomeBusinessLogicThenHitDb()
        {
            $sql = 'SELECT * FROM mytable WHERE ';
            $sql .= $this->_doSomethingComplicatedThatInvolvesParsingTheConfig();
            $this->_queryDb($sql);
        }
    
        protected function _queryDb($sql)
        {
            // Do something with a PDO or whatever
        }
    }
    

    Having abstracted the _queryDb() bit to a separate function, you can then write this test:

        public function testMyClassUnderSomeCircumstances()
        {
            // Set up config
            $exampleConfig = // whatever
    
            // Set up expected result
            $whatTheSqlShouldLookLikeForThisConfig = 'SELECT ... WHERE ...';
    
            // Set up a partial mock that doesn't actually hit the DB
            $myPartialMockObject = $this->getMock('Foo', array('_queryDb'), array(), '');
            $myPartialMockObject->expects($this->once())
                                ->method('_queryDb')
                                ->with($whatTheSqlShouldLookLikeForThisConfig);
    
            // Exercise the class under test
            $myPartialMockObject->setConfig($exampleConfig);
            $myPartialMockObject->doSomeBusinessLogicThenHitTheDb();
        }
    

    The point of this test is to test the business logic that produces your SQL – not to test your database itself. By putting the expectation in place that the resulting SQL must look like whatever it must look like, you are ensuring that your tests will fail if innocent refactoring of _doSomethingComplicatedThatInvolvesParsingTheConfig() accidentally breaks your code, by making it produce different SQL from what it used to.

    If testing the whole application, including its database, is your goal, try a proper integration testing suite like Selenium. Unit tests monitor individual classes and tell you when they’ve stopped behaving as they’re supposed to. You will face problems with speed of execution, and with error localisation (i.e. is the bug even in the code, let alone the class under test, or is it a DB thing?), if you let them overreach.

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

Sidebar

Related Questions

I'm trying to implement some unit tests in my rails3 application. The following is
I've been trying to implement unit testing and currently have some code that does
I'm trying to implement some unit tests for a couple of classes that rely
I have created an Android application that includes some unit conversion tools built into
I am trying to implement some integration tests for my application to test a
I am trying to implement some unit testing for an old framework. I am
I'm trying to implement unit tests in my current project. After that I'll start
I have an older VB6 project that I'm trying to add unit tests for.
I am currently trying to test some code I have in a web application
I'm writing some unit tests for my View Model class. The constructor of this

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.