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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:50:33+00:00 2026-06-01T23:50:33+00:00

For a PHP application I’m developing, I need to read the current git revision

  • 0

For a PHP application I’m developing, I need to read the current git revision SHA which of course I can get easily by using shell_exec or backticks to execute the git command line client.

I have obviously put this call into a method of its very own, so that I can easily isolate and mock this for the rest of my unit tests. So my class looks a bit like this:

class Task_Bundle
{

    public function execute()
    {
        // Do things
        $revision = $this->git_sha();
        // Do more things
    }

    protected function git_sha()
    {
        return `git rev-parse --short HEAD`;
    }
}

Of course, although I can test most of the class by mocking git_sha, I’m struggling to see how to test the actual git_sha() method because I don’t see a way to create a known state for it. I don’t think there’s any real value in a unit test that also calls git rev-parse to compare the results? I was wondering about at least asserting that the command had been run, but I can’t see any way to get a history of shell commands executed by PHP – even if I specify that PHP should use BASH rather than SH the history list comes up empty, I presume because the separate backticks executions are separate terminal sessions.

I’d love to hear any suggestions for how I might test this, or is it OK to just leave that method untested and be careful with it when the app is being maintained in future?

  • 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-01T23:50:34+00:00Added an answer on June 1, 2026 at 11:50 pm

    Although you have accepted an answer, let me post something about what the way unittests should work because even if the solution posted does work and is indeed nice, it has some backdraws.

    First of all you don’t want to actually execute your command, as this would test an external software. From this moment you are depending on external stuff you cannot control (what happens if I want to test your software on my pc? Do I have to instal git or any git-mock?). What you really want to do is see if the command line command is correct and is executed well.

    As I struggled with this before, here is what I did: I created a class which executes shell_exec. Everywhere I want to interact with the shell, I inject the class and use it. So for testing I can mock the class and see if the correct method is executed and if the correct parameters are set.

    This has many benefits:

    • I have one point of interacting with the shell. If anything changes in the future or if I have the need to do anything before executing the command, I can add the behaviour to the class. The PHP function is encapsulated nicely.
    • It works on every PC, even if the shell_exec command does not work for any reason. The tests get executed on every pc (development machines, testserver, integration server) without the need to worry about external dependencies.

    Of course to validate that your software works as a whole, you have to do integration tests which do not mock anything. But this is a totally different situation. Unittests should make sure your class (or method) does what it needs to do. Sometimes this is not clear cut, as you may decide to use a real class somewhere because mocking it would lead to much overhead. But, as far as I’m concerned, it’s no longer a unit test by any means if it relys on external libraries/software which may not be installed or may not work for some reason.

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

Sidebar

Related Questions

I am building a php application. I can easily upload an image or any
I'm developing a php application and need to test the functionality of sending emails.
i am developing a php application which run on offline mode mean not connect
I'm developing a PHP application. I just moved a bunch of files over to
When a PHP application makes a database connection it of course generally needs to
I've got a PHP application which needs to grab the contents from another web
I have a Php application using stream_socket_client(), to get data through tcp from a
I'm writing a PHP application which uses AJAX to submit forms via POST when
I'm developing a PHP application to retrieve the list of contacts from a GMail
I am building a PHP application with CakePHP 2 . I need to have

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.