phpunit - Mock external static methods












1















I am desperately trying to unit test a module for a shopsystem. That shop system uses static methods which I have to call in my functions I want to test.



public function toTest() {
$value = Context::getData();
return $value;
}


Now how can I unit test that function while mocking this static call? I tried using AspectMock but that does not work because it apparently needs access to the original Context class which is not available since it's an external system. I also tried using class_alias to create my own Context class but that does not work either because I need different Context output depending on which function I am testing. And setting class_alias multiple times for different tests does not work because the same class can't be declared multiple times and @runTestsInSeparateProcesses did not have the expected effect.



Edit: None of the duplicates provided a viable solution to my situation, so I don't think this is a duplicate. With no access to the shopsystem code and especially with hard to maintain code like that, PHP does not make it easy to unit test this. Also the solution I found should help others with similar settings.










share|improve this question

























  • Believe this is a duplicate of Mocking static methods in PHP which already exists in stackoverflow

    – Diogo Santo
    Nov 26 '18 at 13:09











  • Possible duplicate of phpunit static called method in method

    – Dirk Scholten
    Nov 26 '18 at 13:09











  • I don't know if this is a realistic example of your code, but if it is, there is not much to test here. Instead you should test the getData() method.

    – jeroen
    Nov 26 '18 at 13:11













  • @DiogoSanto This duplicate is for mocking a method that would be in my own code not in an external system like here. At least as far I understand.

    – alobeejay
    Nov 26 '18 at 13:18











  • @jeroen The getData method is not my own so I can't test it. Also this is a very simplified example. I do stuff with $value in between.

    – alobeejay
    Nov 26 '18 at 13:19
















1















I am desperately trying to unit test a module for a shopsystem. That shop system uses static methods which I have to call in my functions I want to test.



public function toTest() {
$value = Context::getData();
return $value;
}


Now how can I unit test that function while mocking this static call? I tried using AspectMock but that does not work because it apparently needs access to the original Context class which is not available since it's an external system. I also tried using class_alias to create my own Context class but that does not work either because I need different Context output depending on which function I am testing. And setting class_alias multiple times for different tests does not work because the same class can't be declared multiple times and @runTestsInSeparateProcesses did not have the expected effect.



Edit: None of the duplicates provided a viable solution to my situation, so I don't think this is a duplicate. With no access to the shopsystem code and especially with hard to maintain code like that, PHP does not make it easy to unit test this. Also the solution I found should help others with similar settings.










share|improve this question

























  • Believe this is a duplicate of Mocking static methods in PHP which already exists in stackoverflow

    – Diogo Santo
    Nov 26 '18 at 13:09











  • Possible duplicate of phpunit static called method in method

    – Dirk Scholten
    Nov 26 '18 at 13:09











  • I don't know if this is a realistic example of your code, but if it is, there is not much to test here. Instead you should test the getData() method.

    – jeroen
    Nov 26 '18 at 13:11













  • @DiogoSanto This duplicate is for mocking a method that would be in my own code not in an external system like here. At least as far I understand.

    – alobeejay
    Nov 26 '18 at 13:18











  • @jeroen The getData method is not my own so I can't test it. Also this is a very simplified example. I do stuff with $value in between.

    – alobeejay
    Nov 26 '18 at 13:19














1












1








1








I am desperately trying to unit test a module for a shopsystem. That shop system uses static methods which I have to call in my functions I want to test.



public function toTest() {
$value = Context::getData();
return $value;
}


Now how can I unit test that function while mocking this static call? I tried using AspectMock but that does not work because it apparently needs access to the original Context class which is not available since it's an external system. I also tried using class_alias to create my own Context class but that does not work either because I need different Context output depending on which function I am testing. And setting class_alias multiple times for different tests does not work because the same class can't be declared multiple times and @runTestsInSeparateProcesses did not have the expected effect.



Edit: None of the duplicates provided a viable solution to my situation, so I don't think this is a duplicate. With no access to the shopsystem code and especially with hard to maintain code like that, PHP does not make it easy to unit test this. Also the solution I found should help others with similar settings.










share|improve this question
















I am desperately trying to unit test a module for a shopsystem. That shop system uses static methods which I have to call in my functions I want to test.



public function toTest() {
$value = Context::getData();
return $value;
}


Now how can I unit test that function while mocking this static call? I tried using AspectMock but that does not work because it apparently needs access to the original Context class which is not available since it's an external system. I also tried using class_alias to create my own Context class but that does not work either because I need different Context output depending on which function I am testing. And setting class_alias multiple times for different tests does not work because the same class can't be declared multiple times and @runTestsInSeparateProcesses did not have the expected effect.



Edit: None of the duplicates provided a viable solution to my situation, so I don't think this is a duplicate. With no access to the shopsystem code and especially with hard to maintain code like that, PHP does not make it easy to unit test this. Also the solution I found should help others with similar settings.







php unit-testing phpunit






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 27 '18 at 10:11







alobeejay

















asked Nov 26 '18 at 13:05









alobeejayalobeejay

3118




3118













  • Believe this is a duplicate of Mocking static methods in PHP which already exists in stackoverflow

    – Diogo Santo
    Nov 26 '18 at 13:09











  • Possible duplicate of phpunit static called method in method

    – Dirk Scholten
    Nov 26 '18 at 13:09











  • I don't know if this is a realistic example of your code, but if it is, there is not much to test here. Instead you should test the getData() method.

    – jeroen
    Nov 26 '18 at 13:11













  • @DiogoSanto This duplicate is for mocking a method that would be in my own code not in an external system like here. At least as far I understand.

    – alobeejay
    Nov 26 '18 at 13:18











  • @jeroen The getData method is not my own so I can't test it. Also this is a very simplified example. I do stuff with $value in between.

    – alobeejay
    Nov 26 '18 at 13:19



















  • Believe this is a duplicate of Mocking static methods in PHP which already exists in stackoverflow

    – Diogo Santo
    Nov 26 '18 at 13:09











  • Possible duplicate of phpunit static called method in method

    – Dirk Scholten
    Nov 26 '18 at 13:09











  • I don't know if this is a realistic example of your code, but if it is, there is not much to test here. Instead you should test the getData() method.

    – jeroen
    Nov 26 '18 at 13:11













  • @DiogoSanto This duplicate is for mocking a method that would be in my own code not in an external system like here. At least as far I understand.

    – alobeejay
    Nov 26 '18 at 13:18











  • @jeroen The getData method is not my own so I can't test it. Also this is a very simplified example. I do stuff with $value in between.

    – alobeejay
    Nov 26 '18 at 13:19

















Believe this is a duplicate of Mocking static methods in PHP which already exists in stackoverflow

– Diogo Santo
Nov 26 '18 at 13:09





Believe this is a duplicate of Mocking static methods in PHP which already exists in stackoverflow

– Diogo Santo
Nov 26 '18 at 13:09













Possible duplicate of phpunit static called method in method

– Dirk Scholten
Nov 26 '18 at 13:09





Possible duplicate of phpunit static called method in method

– Dirk Scholten
Nov 26 '18 at 13:09













I don't know if this is a realistic example of your code, but if it is, there is not much to test here. Instead you should test the getData() method.

– jeroen
Nov 26 '18 at 13:11







I don't know if this is a realistic example of your code, but if it is, there is not much to test here. Instead you should test the getData() method.

– jeroen
Nov 26 '18 at 13:11















@DiogoSanto This duplicate is for mocking a method that would be in my own code not in an external system like here. At least as far I understand.

– alobeejay
Nov 26 '18 at 13:18





@DiogoSanto This duplicate is for mocking a method that would be in my own code not in an external system like here. At least as far I understand.

– alobeejay
Nov 26 '18 at 13:18













@jeroen The getData method is not my own so I can't test it. Also this is a very simplified example. I do stuff with $value in between.

– alobeejay
Nov 26 '18 at 13:19





@jeroen The getData method is not my own so I can't test it. Also this is a very simplified example. I do stuff with $value in between.

– alobeejay
Nov 26 '18 at 13:19












1 Answer
1






active

oldest

votes


















1














I could solve my issue with the Mockery library. I tried out a few but nothing worked. With Mockery everything seems possible now. This link really helped: https://robertbasic.com/blog/mocking-hard-dependencies-with-mockery/



You can easily mock static calls to classes that don't belong to you:



public function methodToTest() {
return Context::getData();
}

public function testMethodToTest() {
m::mock('alias:Context')
->shouldReceive('getData')
->andReturn('foo');
}


And even instantiations for classes you don't have access to:



public function methodToTest() {
$obj = new Category(5);
return $obj->id;
}

public function testMethodToTest() {
m::mock('overload:Category')
->shouldReceive('__construct')
->with(5)
->andSet('id', 5);
}


But you have to keep in mind that you need the two phpunit annotations at the beginning of the class:



* @runTestsInSeparateProcesses
* @preserveGlobalState disabled





share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53481778%2fphpunit-mock-external-static-methods%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    I could solve my issue with the Mockery library. I tried out a few but nothing worked. With Mockery everything seems possible now. This link really helped: https://robertbasic.com/blog/mocking-hard-dependencies-with-mockery/



    You can easily mock static calls to classes that don't belong to you:



    public function methodToTest() {
    return Context::getData();
    }

    public function testMethodToTest() {
    m::mock('alias:Context')
    ->shouldReceive('getData')
    ->andReturn('foo');
    }


    And even instantiations for classes you don't have access to:



    public function methodToTest() {
    $obj = new Category(5);
    return $obj->id;
    }

    public function testMethodToTest() {
    m::mock('overload:Category')
    ->shouldReceive('__construct')
    ->with(5)
    ->andSet('id', 5);
    }


    But you have to keep in mind that you need the two phpunit annotations at the beginning of the class:



    * @runTestsInSeparateProcesses
    * @preserveGlobalState disabled





    share|improve this answer




























      1














      I could solve my issue with the Mockery library. I tried out a few but nothing worked. With Mockery everything seems possible now. This link really helped: https://robertbasic.com/blog/mocking-hard-dependencies-with-mockery/



      You can easily mock static calls to classes that don't belong to you:



      public function methodToTest() {
      return Context::getData();
      }

      public function testMethodToTest() {
      m::mock('alias:Context')
      ->shouldReceive('getData')
      ->andReturn('foo');
      }


      And even instantiations for classes you don't have access to:



      public function methodToTest() {
      $obj = new Category(5);
      return $obj->id;
      }

      public function testMethodToTest() {
      m::mock('overload:Category')
      ->shouldReceive('__construct')
      ->with(5)
      ->andSet('id', 5);
      }


      But you have to keep in mind that you need the two phpunit annotations at the beginning of the class:



      * @runTestsInSeparateProcesses
      * @preserveGlobalState disabled





      share|improve this answer


























        1












        1








        1







        I could solve my issue with the Mockery library. I tried out a few but nothing worked. With Mockery everything seems possible now. This link really helped: https://robertbasic.com/blog/mocking-hard-dependencies-with-mockery/



        You can easily mock static calls to classes that don't belong to you:



        public function methodToTest() {
        return Context::getData();
        }

        public function testMethodToTest() {
        m::mock('alias:Context')
        ->shouldReceive('getData')
        ->andReturn('foo');
        }


        And even instantiations for classes you don't have access to:



        public function methodToTest() {
        $obj = new Category(5);
        return $obj->id;
        }

        public function testMethodToTest() {
        m::mock('overload:Category')
        ->shouldReceive('__construct')
        ->with(5)
        ->andSet('id', 5);
        }


        But you have to keep in mind that you need the two phpunit annotations at the beginning of the class:



        * @runTestsInSeparateProcesses
        * @preserveGlobalState disabled





        share|improve this answer













        I could solve my issue with the Mockery library. I tried out a few but nothing worked. With Mockery everything seems possible now. This link really helped: https://robertbasic.com/blog/mocking-hard-dependencies-with-mockery/



        You can easily mock static calls to classes that don't belong to you:



        public function methodToTest() {
        return Context::getData();
        }

        public function testMethodToTest() {
        m::mock('alias:Context')
        ->shouldReceive('getData')
        ->andReturn('foo');
        }


        And even instantiations for classes you don't have access to:



        public function methodToTest() {
        $obj = new Category(5);
        return $obj->id;
        }

        public function testMethodToTest() {
        m::mock('overload:Category')
        ->shouldReceive('__construct')
        ->with(5)
        ->andSet('id', 5);
        }


        But you have to keep in mind that you need the two phpunit annotations at the beginning of the class:



        * @runTestsInSeparateProcesses
        * @preserveGlobalState disabled






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 27 '18 at 10:08









        alobeejayalobeejay

        3118




        3118
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53481778%2fphpunit-mock-external-static-methods%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            To store a contact into the json file from server.js file using a class in NodeJS

            Redirect URL with Chrome Remote Debugging Android Devices

            Dieringhausen