why is it impossible to test a static method with mockery or anything else












2















I have read in laravel's facade documentation the following sentence:




Typically, it would not be possible to mock or stub a truly static
class method.




1) question 1: I'm trying to understand facade in laravel. As I guess, it's implemented because if we have classes, and they have big namespaces and big names and every time we want to use this class and we don't want to use new keyword and use statements, we use the facade which is an easier code and readable. I also think that laravel implemented facades because they wanted to write non-static functions in their classes so that they could be tested. After all of this, we use facades like static classes (because of readability and not using new and use), but in reality, it makes new instances.



Am I right?



2) If the above is right, can you provide me an example why it's not possible to test a static method as laravel docs said?










share|improve this question





























    2















    I have read in laravel's facade documentation the following sentence:




    Typically, it would not be possible to mock or stub a truly static
    class method.




    1) question 1: I'm trying to understand facade in laravel. As I guess, it's implemented because if we have classes, and they have big namespaces and big names and every time we want to use this class and we don't want to use new keyword and use statements, we use the facade which is an easier code and readable. I also think that laravel implemented facades because they wanted to write non-static functions in their classes so that they could be tested. After all of this, we use facades like static classes (because of readability and not using new and use), but in reality, it makes new instances.



    Am I right?



    2) If the above is right, can you provide me an example why it's not possible to test a static method as laravel docs said?










    share|improve this question



























      2












      2








      2








      I have read in laravel's facade documentation the following sentence:




      Typically, it would not be possible to mock or stub a truly static
      class method.




      1) question 1: I'm trying to understand facade in laravel. As I guess, it's implemented because if we have classes, and they have big namespaces and big names and every time we want to use this class and we don't want to use new keyword and use statements, we use the facade which is an easier code and readable. I also think that laravel implemented facades because they wanted to write non-static functions in their classes so that they could be tested. After all of this, we use facades like static classes (because of readability and not using new and use), but in reality, it makes new instances.



      Am I right?



      2) If the above is right, can you provide me an example why it's not possible to test a static method as laravel docs said?










      share|improve this question
















      I have read in laravel's facade documentation the following sentence:




      Typically, it would not be possible to mock or stub a truly static
      class method.




      1) question 1: I'm trying to understand facade in laravel. As I guess, it's implemented because if we have classes, and they have big namespaces and big names and every time we want to use this class and we don't want to use new keyword and use statements, we use the facade which is an easier code and readable. I also think that laravel implemented facades because they wanted to write non-static functions in their classes so that they could be tested. After all of this, we use facades like static classes (because of readability and not using new and use), but in reality, it makes new instances.



      Am I right?



      2) If the above is right, can you provide me an example why it's not possible to test a static method as laravel docs said?







      php laravel






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 '18 at 12:00









      Eren ÇELİK

      10617




      10617










      asked Nov 21 '18 at 11:17









      Nika KhurashviliNika Khurashvili

      84013




      84013
























          1 Answer
          1






          active

          oldest

          votes


















          1














          A facade does not solve the big namespaces problem you mentioned. Big namespaces are solved using aliases. You can declare them in your config/app.php and internally Laravel will use class_alias when you call them. This is how e.g. Cache or DB work.



          A facade is basically a proxy class to a singleton object instance of another class (the facade itself ensures the instance is a singleton).



          Typically to register a singleton in Laravel you:




          1. Add app()->singleton(ABC::class) in your service provider

          2. Access it via app()->make(ABC::class)->...


          A facade basically takes care of that for you if you haven't already registered that class as a singleton.



          Basically a facade is a way to proxy that singleton instance of another class.



          Also it's generally not possible to mock or stub static methods however if you are using facades you can do ABCFacade::swap($mockObject) and therefore your facades can be mocked.



          It is also false that you cannot test a static method. You can absolutely test a static method. For example:



           public testStaticMethod() {
          $this->assertEquals(1, ABC::method()); // We tested a static method against a desired behaviour
          }


          What you usually can't do is mock a static method. Here's how you would typically mock something with PHPUnit:



          public testWithDependency() { 
          $dependency = $this->getMockBuilder(Dependency::class)->getMock();
          $dependency->expects($this->once())->method('dependantMethod')->willReturn(true);
          $objectToTest = new ABC($dependency); //We're passing a fake dependency which behaves in an ideal way
          $this->assertEquals(1, $objectToTest->methodToTest()); //Any calls to the dependency will call mock methods and not real ones
          }


          The problem arises when trying to mock a static method. As you can see mocking creates mock instances of a certain type. It can't mock the static members of that type because the mock object itself is not actually of that type.



          However as I just found out the statement that it's not possible to mock or stub a static method is not entirely true. There's the AspectMock you can mock static methods or helper methods. This seems to work by intercepting all function calls via a custom autoloader.



          This being said, just because you can doesn't mean it's good practice to use static methods, there's other issues to consider like e.g. you normally can't have static interfaces in most programming languages or you normally can't override static methods in most programming languages. Note the "in most programming languages" part here. In PHP it's entirely possible to override static methods with late static binding but that means you need to make a conscious decision about this when implementing the static method.



          Another disadvantage is that a class of statics can't implement an interface because interfaces apply to object behaviours and not the static behaviour. Therefore you can't swap out one interface for another if you are using statics which is a major disadvantage.



          In general the aversion to static methods is not because of testability but because if you are coding in OOP you are really limited if you are using statics.



          Hopefully this will help clear up some confusion.






          share|improve this answer
























          • Very true. In most languages you can mock an interface by providing a different implementation for that interface. PHP is very unstrict in that regard, and as long as you have an object instance that implements the right methods, it will usually work (unless there is some specific type checking going on). But in case of static methods, there is no object instance at all. You're directly referencing a certain class, and the only way to make sure you can mock that class, is to prevent completely that the actual class is loaded and load the mock version instead. Hacky to say the least.

            – GolezTrol
            Nov 21 '18 at 12:05













          • laravel itself says that They provide a terse, memorable syntax that allows you to use Laravel's features without remembering long class names that must be injected or configured manually. And I don't think it must be a singleton to make it work

            – Nika Khurashvili
            Nov 21 '18 at 12:20






          • 1





            @NikaKhurashvili the laravel Facades "cache" the last resolved instance within a request so in practice it behaves as a singleton. This doesn't mean you need to set it up as a singleton though.

            – apokryfos
            Nov 21 '18 at 12:38











          • @apokryfos Is there any reason why does it cache the last resolved instance and behaves as a singleton?

            – Nika Khurashvili
            Nov 21 '18 at 13:00











          • @NikaKhurashvili not exactly sure why but my guess is if you use it like a static then it makes sense if it has a static-like behaviour i.e. it maintains the same state between calls. You can always call Facade::clearResolvedInstance(<name>) to reset the state of the given facade

            – apokryfos
            Nov 21 '18 at 13:58













          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%2f53410966%2fwhy-is-it-impossible-to-test-a-static-method-with-mockery-or-anything-else%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














          A facade does not solve the big namespaces problem you mentioned. Big namespaces are solved using aliases. You can declare them in your config/app.php and internally Laravel will use class_alias when you call them. This is how e.g. Cache or DB work.



          A facade is basically a proxy class to a singleton object instance of another class (the facade itself ensures the instance is a singleton).



          Typically to register a singleton in Laravel you:




          1. Add app()->singleton(ABC::class) in your service provider

          2. Access it via app()->make(ABC::class)->...


          A facade basically takes care of that for you if you haven't already registered that class as a singleton.



          Basically a facade is a way to proxy that singleton instance of another class.



          Also it's generally not possible to mock or stub static methods however if you are using facades you can do ABCFacade::swap($mockObject) and therefore your facades can be mocked.



          It is also false that you cannot test a static method. You can absolutely test a static method. For example:



           public testStaticMethod() {
          $this->assertEquals(1, ABC::method()); // We tested a static method against a desired behaviour
          }


          What you usually can't do is mock a static method. Here's how you would typically mock something with PHPUnit:



          public testWithDependency() { 
          $dependency = $this->getMockBuilder(Dependency::class)->getMock();
          $dependency->expects($this->once())->method('dependantMethod')->willReturn(true);
          $objectToTest = new ABC($dependency); //We're passing a fake dependency which behaves in an ideal way
          $this->assertEquals(1, $objectToTest->methodToTest()); //Any calls to the dependency will call mock methods and not real ones
          }


          The problem arises when trying to mock a static method. As you can see mocking creates mock instances of a certain type. It can't mock the static members of that type because the mock object itself is not actually of that type.



          However as I just found out the statement that it's not possible to mock or stub a static method is not entirely true. There's the AspectMock you can mock static methods or helper methods. This seems to work by intercepting all function calls via a custom autoloader.



          This being said, just because you can doesn't mean it's good practice to use static methods, there's other issues to consider like e.g. you normally can't have static interfaces in most programming languages or you normally can't override static methods in most programming languages. Note the "in most programming languages" part here. In PHP it's entirely possible to override static methods with late static binding but that means you need to make a conscious decision about this when implementing the static method.



          Another disadvantage is that a class of statics can't implement an interface because interfaces apply to object behaviours and not the static behaviour. Therefore you can't swap out one interface for another if you are using statics which is a major disadvantage.



          In general the aversion to static methods is not because of testability but because if you are coding in OOP you are really limited if you are using statics.



          Hopefully this will help clear up some confusion.






          share|improve this answer
























          • Very true. In most languages you can mock an interface by providing a different implementation for that interface. PHP is very unstrict in that regard, and as long as you have an object instance that implements the right methods, it will usually work (unless there is some specific type checking going on). But in case of static methods, there is no object instance at all. You're directly referencing a certain class, and the only way to make sure you can mock that class, is to prevent completely that the actual class is loaded and load the mock version instead. Hacky to say the least.

            – GolezTrol
            Nov 21 '18 at 12:05













          • laravel itself says that They provide a terse, memorable syntax that allows you to use Laravel's features without remembering long class names that must be injected or configured manually. And I don't think it must be a singleton to make it work

            – Nika Khurashvili
            Nov 21 '18 at 12:20






          • 1





            @NikaKhurashvili the laravel Facades "cache" the last resolved instance within a request so in practice it behaves as a singleton. This doesn't mean you need to set it up as a singleton though.

            – apokryfos
            Nov 21 '18 at 12:38











          • @apokryfos Is there any reason why does it cache the last resolved instance and behaves as a singleton?

            – Nika Khurashvili
            Nov 21 '18 at 13:00











          • @NikaKhurashvili not exactly sure why but my guess is if you use it like a static then it makes sense if it has a static-like behaviour i.e. it maintains the same state between calls. You can always call Facade::clearResolvedInstance(<name>) to reset the state of the given facade

            – apokryfos
            Nov 21 '18 at 13:58


















          1














          A facade does not solve the big namespaces problem you mentioned. Big namespaces are solved using aliases. You can declare them in your config/app.php and internally Laravel will use class_alias when you call them. This is how e.g. Cache or DB work.



          A facade is basically a proxy class to a singleton object instance of another class (the facade itself ensures the instance is a singleton).



          Typically to register a singleton in Laravel you:




          1. Add app()->singleton(ABC::class) in your service provider

          2. Access it via app()->make(ABC::class)->...


          A facade basically takes care of that for you if you haven't already registered that class as a singleton.



          Basically a facade is a way to proxy that singleton instance of another class.



          Also it's generally not possible to mock or stub static methods however if you are using facades you can do ABCFacade::swap($mockObject) and therefore your facades can be mocked.



          It is also false that you cannot test a static method. You can absolutely test a static method. For example:



           public testStaticMethod() {
          $this->assertEquals(1, ABC::method()); // We tested a static method against a desired behaviour
          }


          What you usually can't do is mock a static method. Here's how you would typically mock something with PHPUnit:



          public testWithDependency() { 
          $dependency = $this->getMockBuilder(Dependency::class)->getMock();
          $dependency->expects($this->once())->method('dependantMethod')->willReturn(true);
          $objectToTest = new ABC($dependency); //We're passing a fake dependency which behaves in an ideal way
          $this->assertEquals(1, $objectToTest->methodToTest()); //Any calls to the dependency will call mock methods and not real ones
          }


          The problem arises when trying to mock a static method. As you can see mocking creates mock instances of a certain type. It can't mock the static members of that type because the mock object itself is not actually of that type.



          However as I just found out the statement that it's not possible to mock or stub a static method is not entirely true. There's the AspectMock you can mock static methods or helper methods. This seems to work by intercepting all function calls via a custom autoloader.



          This being said, just because you can doesn't mean it's good practice to use static methods, there's other issues to consider like e.g. you normally can't have static interfaces in most programming languages or you normally can't override static methods in most programming languages. Note the "in most programming languages" part here. In PHP it's entirely possible to override static methods with late static binding but that means you need to make a conscious decision about this when implementing the static method.



          Another disadvantage is that a class of statics can't implement an interface because interfaces apply to object behaviours and not the static behaviour. Therefore you can't swap out one interface for another if you are using statics which is a major disadvantage.



          In general the aversion to static methods is not because of testability but because if you are coding in OOP you are really limited if you are using statics.



          Hopefully this will help clear up some confusion.






          share|improve this answer
























          • Very true. In most languages you can mock an interface by providing a different implementation for that interface. PHP is very unstrict in that regard, and as long as you have an object instance that implements the right methods, it will usually work (unless there is some specific type checking going on). But in case of static methods, there is no object instance at all. You're directly referencing a certain class, and the only way to make sure you can mock that class, is to prevent completely that the actual class is loaded and load the mock version instead. Hacky to say the least.

            – GolezTrol
            Nov 21 '18 at 12:05













          • laravel itself says that They provide a terse, memorable syntax that allows you to use Laravel's features without remembering long class names that must be injected or configured manually. And I don't think it must be a singleton to make it work

            – Nika Khurashvili
            Nov 21 '18 at 12:20






          • 1





            @NikaKhurashvili the laravel Facades "cache" the last resolved instance within a request so in practice it behaves as a singleton. This doesn't mean you need to set it up as a singleton though.

            – apokryfos
            Nov 21 '18 at 12:38











          • @apokryfos Is there any reason why does it cache the last resolved instance and behaves as a singleton?

            – Nika Khurashvili
            Nov 21 '18 at 13:00











          • @NikaKhurashvili not exactly sure why but my guess is if you use it like a static then it makes sense if it has a static-like behaviour i.e. it maintains the same state between calls. You can always call Facade::clearResolvedInstance(<name>) to reset the state of the given facade

            – apokryfos
            Nov 21 '18 at 13:58
















          1












          1








          1







          A facade does not solve the big namespaces problem you mentioned. Big namespaces are solved using aliases. You can declare them in your config/app.php and internally Laravel will use class_alias when you call them. This is how e.g. Cache or DB work.



          A facade is basically a proxy class to a singleton object instance of another class (the facade itself ensures the instance is a singleton).



          Typically to register a singleton in Laravel you:




          1. Add app()->singleton(ABC::class) in your service provider

          2. Access it via app()->make(ABC::class)->...


          A facade basically takes care of that for you if you haven't already registered that class as a singleton.



          Basically a facade is a way to proxy that singleton instance of another class.



          Also it's generally not possible to mock or stub static methods however if you are using facades you can do ABCFacade::swap($mockObject) and therefore your facades can be mocked.



          It is also false that you cannot test a static method. You can absolutely test a static method. For example:



           public testStaticMethod() {
          $this->assertEquals(1, ABC::method()); // We tested a static method against a desired behaviour
          }


          What you usually can't do is mock a static method. Here's how you would typically mock something with PHPUnit:



          public testWithDependency() { 
          $dependency = $this->getMockBuilder(Dependency::class)->getMock();
          $dependency->expects($this->once())->method('dependantMethod')->willReturn(true);
          $objectToTest = new ABC($dependency); //We're passing a fake dependency which behaves in an ideal way
          $this->assertEquals(1, $objectToTest->methodToTest()); //Any calls to the dependency will call mock methods and not real ones
          }


          The problem arises when trying to mock a static method. As you can see mocking creates mock instances of a certain type. It can't mock the static members of that type because the mock object itself is not actually of that type.



          However as I just found out the statement that it's not possible to mock or stub a static method is not entirely true. There's the AspectMock you can mock static methods or helper methods. This seems to work by intercepting all function calls via a custom autoloader.



          This being said, just because you can doesn't mean it's good practice to use static methods, there's other issues to consider like e.g. you normally can't have static interfaces in most programming languages or you normally can't override static methods in most programming languages. Note the "in most programming languages" part here. In PHP it's entirely possible to override static methods with late static binding but that means you need to make a conscious decision about this when implementing the static method.



          Another disadvantage is that a class of statics can't implement an interface because interfaces apply to object behaviours and not the static behaviour. Therefore you can't swap out one interface for another if you are using statics which is a major disadvantage.



          In general the aversion to static methods is not because of testability but because if you are coding in OOP you are really limited if you are using statics.



          Hopefully this will help clear up some confusion.






          share|improve this answer













          A facade does not solve the big namespaces problem you mentioned. Big namespaces are solved using aliases. You can declare them in your config/app.php and internally Laravel will use class_alias when you call them. This is how e.g. Cache or DB work.



          A facade is basically a proxy class to a singleton object instance of another class (the facade itself ensures the instance is a singleton).



          Typically to register a singleton in Laravel you:




          1. Add app()->singleton(ABC::class) in your service provider

          2. Access it via app()->make(ABC::class)->...


          A facade basically takes care of that for you if you haven't already registered that class as a singleton.



          Basically a facade is a way to proxy that singleton instance of another class.



          Also it's generally not possible to mock or stub static methods however if you are using facades you can do ABCFacade::swap($mockObject) and therefore your facades can be mocked.



          It is also false that you cannot test a static method. You can absolutely test a static method. For example:



           public testStaticMethod() {
          $this->assertEquals(1, ABC::method()); // We tested a static method against a desired behaviour
          }


          What you usually can't do is mock a static method. Here's how you would typically mock something with PHPUnit:



          public testWithDependency() { 
          $dependency = $this->getMockBuilder(Dependency::class)->getMock();
          $dependency->expects($this->once())->method('dependantMethod')->willReturn(true);
          $objectToTest = new ABC($dependency); //We're passing a fake dependency which behaves in an ideal way
          $this->assertEquals(1, $objectToTest->methodToTest()); //Any calls to the dependency will call mock methods and not real ones
          }


          The problem arises when trying to mock a static method. As you can see mocking creates mock instances of a certain type. It can't mock the static members of that type because the mock object itself is not actually of that type.



          However as I just found out the statement that it's not possible to mock or stub a static method is not entirely true. There's the AspectMock you can mock static methods or helper methods. This seems to work by intercepting all function calls via a custom autoloader.



          This being said, just because you can doesn't mean it's good practice to use static methods, there's other issues to consider like e.g. you normally can't have static interfaces in most programming languages or you normally can't override static methods in most programming languages. Note the "in most programming languages" part here. In PHP it's entirely possible to override static methods with late static binding but that means you need to make a conscious decision about this when implementing the static method.



          Another disadvantage is that a class of statics can't implement an interface because interfaces apply to object behaviours and not the static behaviour. Therefore you can't swap out one interface for another if you are using statics which is a major disadvantage.



          In general the aversion to static methods is not because of testability but because if you are coding in OOP you are really limited if you are using statics.



          Hopefully this will help clear up some confusion.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '18 at 11:55









          apokryfosapokryfos

          19.4k53364




          19.4k53364













          • Very true. In most languages you can mock an interface by providing a different implementation for that interface. PHP is very unstrict in that regard, and as long as you have an object instance that implements the right methods, it will usually work (unless there is some specific type checking going on). But in case of static methods, there is no object instance at all. You're directly referencing a certain class, and the only way to make sure you can mock that class, is to prevent completely that the actual class is loaded and load the mock version instead. Hacky to say the least.

            – GolezTrol
            Nov 21 '18 at 12:05













          • laravel itself says that They provide a terse, memorable syntax that allows you to use Laravel's features without remembering long class names that must be injected or configured manually. And I don't think it must be a singleton to make it work

            – Nika Khurashvili
            Nov 21 '18 at 12:20






          • 1





            @NikaKhurashvili the laravel Facades "cache" the last resolved instance within a request so in practice it behaves as a singleton. This doesn't mean you need to set it up as a singleton though.

            – apokryfos
            Nov 21 '18 at 12:38











          • @apokryfos Is there any reason why does it cache the last resolved instance and behaves as a singleton?

            – Nika Khurashvili
            Nov 21 '18 at 13:00











          • @NikaKhurashvili not exactly sure why but my guess is if you use it like a static then it makes sense if it has a static-like behaviour i.e. it maintains the same state between calls. You can always call Facade::clearResolvedInstance(<name>) to reset the state of the given facade

            – apokryfos
            Nov 21 '18 at 13:58





















          • Very true. In most languages you can mock an interface by providing a different implementation for that interface. PHP is very unstrict in that regard, and as long as you have an object instance that implements the right methods, it will usually work (unless there is some specific type checking going on). But in case of static methods, there is no object instance at all. You're directly referencing a certain class, and the only way to make sure you can mock that class, is to prevent completely that the actual class is loaded and load the mock version instead. Hacky to say the least.

            – GolezTrol
            Nov 21 '18 at 12:05













          • laravel itself says that They provide a terse, memorable syntax that allows you to use Laravel's features without remembering long class names that must be injected or configured manually. And I don't think it must be a singleton to make it work

            – Nika Khurashvili
            Nov 21 '18 at 12:20






          • 1





            @NikaKhurashvili the laravel Facades "cache" the last resolved instance within a request so in practice it behaves as a singleton. This doesn't mean you need to set it up as a singleton though.

            – apokryfos
            Nov 21 '18 at 12:38











          • @apokryfos Is there any reason why does it cache the last resolved instance and behaves as a singleton?

            – Nika Khurashvili
            Nov 21 '18 at 13:00











          • @NikaKhurashvili not exactly sure why but my guess is if you use it like a static then it makes sense if it has a static-like behaviour i.e. it maintains the same state between calls. You can always call Facade::clearResolvedInstance(<name>) to reset the state of the given facade

            – apokryfos
            Nov 21 '18 at 13:58



















          Very true. In most languages you can mock an interface by providing a different implementation for that interface. PHP is very unstrict in that regard, and as long as you have an object instance that implements the right methods, it will usually work (unless there is some specific type checking going on). But in case of static methods, there is no object instance at all. You're directly referencing a certain class, and the only way to make sure you can mock that class, is to prevent completely that the actual class is loaded and load the mock version instead. Hacky to say the least.

          – GolezTrol
          Nov 21 '18 at 12:05







          Very true. In most languages you can mock an interface by providing a different implementation for that interface. PHP is very unstrict in that regard, and as long as you have an object instance that implements the right methods, it will usually work (unless there is some specific type checking going on). But in case of static methods, there is no object instance at all. You're directly referencing a certain class, and the only way to make sure you can mock that class, is to prevent completely that the actual class is loaded and load the mock version instead. Hacky to say the least.

          – GolezTrol
          Nov 21 '18 at 12:05















          laravel itself says that They provide a terse, memorable syntax that allows you to use Laravel's features without remembering long class names that must be injected or configured manually. And I don't think it must be a singleton to make it work

          – Nika Khurashvili
          Nov 21 '18 at 12:20





          laravel itself says that They provide a terse, memorable syntax that allows you to use Laravel's features without remembering long class names that must be injected or configured manually. And I don't think it must be a singleton to make it work

          – Nika Khurashvili
          Nov 21 '18 at 12:20




          1




          1





          @NikaKhurashvili the laravel Facades "cache" the last resolved instance within a request so in practice it behaves as a singleton. This doesn't mean you need to set it up as a singleton though.

          – apokryfos
          Nov 21 '18 at 12:38





          @NikaKhurashvili the laravel Facades "cache" the last resolved instance within a request so in practice it behaves as a singleton. This doesn't mean you need to set it up as a singleton though.

          – apokryfos
          Nov 21 '18 at 12:38













          @apokryfos Is there any reason why does it cache the last resolved instance and behaves as a singleton?

          – Nika Khurashvili
          Nov 21 '18 at 13:00





          @apokryfos Is there any reason why does it cache the last resolved instance and behaves as a singleton?

          – Nika Khurashvili
          Nov 21 '18 at 13:00













          @NikaKhurashvili not exactly sure why but my guess is if you use it like a static then it makes sense if it has a static-like behaviour i.e. it maintains the same state between calls. You can always call Facade::clearResolvedInstance(<name>) to reset the state of the given facade

          – apokryfos
          Nov 21 '18 at 13:58







          @NikaKhurashvili not exactly sure why but my guess is if you use it like a static then it makes sense if it has a static-like behaviour i.e. it maintains the same state between calls. You can always call Facade::clearResolvedInstance(<name>) to reset the state of the given facade

          – apokryfos
          Nov 21 '18 at 13:58






















          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%2f53410966%2fwhy-is-it-impossible-to-test-a-static-method-with-mockery-or-anything-else%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

          mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

          How to change which sound is reproduced for terminal bell?

          Can I use Tabulator js library in my java Spring + Thymeleaf project?