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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:15:39+00:00 2026-06-10T04:15:39+00:00

Let’s say I want to test a simple helper that takes a class name

  • 0

Let’s say I want to test a simple helper that takes a class name as an argument and makes a redirection.

How am I supposed to test this if the function is called in many places from inside a couple of controllers? Should I test every class name that was passed as a parameter in the whole code (write them in the provider function myself)? Or is there a magical function which does that for me?

  • 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-10T04:15:41+00:00Added an answer on June 10, 2026 at 4:15 am

    Your question is the exact reason why dependency injection — when done correctly (not how most popular frameworks “implement” it) — is touted as the ultimate in code testability.

    To understand why, lets look at how “helper functions” and class-oriented programming make your controllers difficult to test.

    class Helpers {
        public static function myHelper() {
            return 42;
        }
    }
    
    class MyController {
        public function doSomething() {
            return Helpers::myHelper() + 100;
        }
    }
    

    The entire point of unit testing is to verify that “units” of code work in isolation. If you can’t isolate functionality, your tests are meaningless because their results could be tainted by the behavior of the other code involved. This can result in what statisticians call Type I and Type II errors: basically, this means you can get test results that might be lying to you.

    In the code above, the helper cannot be easily mocked to determine that MyController::doSomething works in complete isolation from outside influences. Why not? Because we can’t “mock” the behavior of the helper method to guarantee our doSomething method actually adds 100 to the helper result. We’re stuck with the helper’s exact behavior (returning 42). This is a problem that correct object-orientation and inversion of control eliminate entirely. Let’s consider an example of how:

    If MyController asks for it’s dependencies instead of using the static helper function , it becomes trivial to mock the outside influences. Consider:

    interface AnswerMachine {
        public function getAnswer();
    }
    
    class UltimateAnswerer implements AnswerMachine {
        public function getAnswer() {
            return 42;
        }
    }
    
    class MyController {
        private $answerer;
        public function __construct(AnswerMachine $answerer) {
            $this->answerer = $answerer;
        }
        public function doSomething() {
            return $this->answerer->getAnswer() + 100;
        }
    
    }
    

    Now, it’s trivially simple to test that MyController::doSomething does in fact add 100 to whatever it gets back from the answer machine:

    // test file
    
    class StubAnswerer implements AnswerMachine {
        public function getAnswer() {
            return 50;
        }
    }
    
    $stubAnswer = new StubAnswerer();
    $testController = new MyController($stubAnswerer);
    assert($testController->doSomething() === 150);
    

    This example also demonstrates how the correct use of interfaces in your code can greatly simplify the testing process. Test frameworks like PHPUnit make it very easy to mock interface definitions to perform exactly what you want them to in order to test the isolated functionality of code units.

    So I hope these very simple examples demonstrate how powerful dependency injection is when it comes to testing your code. But more importantly, I hope they demonstrate why you should be wary if your framework of choice is using static (just another name for global), singletons, and helper functions.

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

Sidebar

Related Questions

Let me explain best with an example. Say you have node class that can
Let's say you have a method that expects a numerical value as an argument.
Let's say I'm writing a PHP (>= 5.0) class that's meant to be a
Let's say you have a class, with certain properties, and that you tried your
Let's say I don't have photoshop, but I want to make pattern files (.pat)
Let's say that I have a SQLite database that I create in a separate
Let's say I have thousands of users and I want to make the passwords
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's say I have window.open (without name parameter), scattered in my project and I
Let's say after I had login I will be prompt to enter the Name

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.