php_unit_mock.rst 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. ======================
  2. Rule ``php_unit_mock``
  3. ======================
  4. Usages of ``->getMock`` and ``->getMockWithoutInvokingTheOriginalConstructor``
  5. methods MUST be replaced by ``->createMock`` or ``->createPartialMock`` methods.
  6. .. warning:: Using this rule is risky.
  7. Risky when PHPUnit classes are overridden or not accessible, or when project
  8. has PHPUnit incompatibilities.
  9. Configuration
  10. -------------
  11. ``target``
  12. ~~~~~~~~~~
  13. Target version of PHPUnit.
  14. Allowed values: ``'5.4'``, ``'5.5'``, ``'newest'``
  15. Default value: ``'newest'``
  16. Examples
  17. --------
  18. Example #1
  19. ~~~~~~~~~~
  20. *Default* configuration.
  21. .. code-block:: diff
  22. --- Original
  23. +++ New
  24. @@ -3,9 +3,9 @@
  25. {
  26. public function testFoo()
  27. {
  28. - $mock = $this->getMockWithoutInvokingTheOriginalConstructor("Foo");
  29. - $mock1 = $this->getMock("Foo");
  30. - $mock1 = $this->getMock("Bar", ["aaa"]);
  31. + $mock = $this->createMock("Foo");
  32. + $mock1 = $this->createMock("Foo");
  33. + $mock1 = $this->createPartialMock("Bar", ["aaa"]);
  34. $mock1 = $this->getMock("Baz", ["aaa"], ["argument"]); // version with more than 2 params is not supported
  35. }
  36. }
  37. Example #2
  38. ~~~~~~~~~~
  39. With configuration: ``['target' => '5.4']``.
  40. .. code-block:: diff
  41. --- Original
  42. +++ New
  43. @@ -3,7 +3,7 @@
  44. {
  45. public function testFoo()
  46. {
  47. - $mock1 = $this->getMock("Foo");
  48. + $mock1 = $this->createMock("Foo");
  49. $mock1 = $this->getMock("Bar", ["aaa"]); // version with multiple params is not supported
  50. }
  51. }
  52. Rule sets
  53. ---------
  54. The rule is part of the following rule sets:
  55. @PHPUnit54Migration:risky
  56. Using the `@PHPUnit54Migration:risky <./../../ruleSets/PHPUnit54MigrationRisky.rst>`_ rule set will enable the ``php_unit_mock`` rule with the config below:
  57. ``['target' => '5.4']``
  58. @PHPUnit55Migration:risky
  59. Using the `@PHPUnit55Migration:risky <./../../ruleSets/PHPUnit55MigrationRisky.rst>`_ rule set will enable the ``php_unit_mock`` rule with the config below:
  60. ``['target' => '5.5']``
  61. @PHPUnit56Migration:risky
  62. Using the `@PHPUnit56Migration:risky <./../../ruleSets/PHPUnit56MigrationRisky.rst>`_ rule set will enable the ``php_unit_mock`` rule with the config below:
  63. ``['target' => '5.5']``
  64. @PHPUnit57Migration:risky
  65. Using the `@PHPUnit57Migration:risky <./../../ruleSets/PHPUnit57MigrationRisky.rst>`_ rule set will enable the ``php_unit_mock`` rule with the config below:
  66. ``['target' => '5.5']``
  67. @PHPUnit60Migration:risky
  68. Using the `@PHPUnit60Migration:risky <./../../ruleSets/PHPUnit60MigrationRisky.rst>`_ rule set will enable the ``php_unit_mock`` rule with the config below:
  69. ``['target' => '5.5']``
  70. @PHPUnit75Migration:risky
  71. Using the `@PHPUnit75Migration:risky <./../../ruleSets/PHPUnit75MigrationRisky.rst>`_ rule set will enable the ``php_unit_mock`` rule with the config below:
  72. ``['target' => '5.5']``