php_unit_mock.rst 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. <?php
  25. final class MyTest extends \PHPUnit_Framework_TestCase
  26. {
  27. public function testFoo()
  28. {
  29. - $mock = $this->getMockWithoutInvokingTheOriginalConstructor("Foo");
  30. - $mock1 = $this->getMock("Foo");
  31. - $mock1 = $this->getMock("Bar", ["aaa"]);
  32. + $mock = $this->createMock("Foo");
  33. + $mock1 = $this->createMock("Foo");
  34. + $mock1 = $this->createPartialMock("Bar", ["aaa"]);
  35. $mock1 = $this->getMock("Baz", ["aaa"], ["argument"]); // version with more than 2 params is not supported
  36. }
  37. }
  38. Example #2
  39. ~~~~~~~~~~
  40. With configuration: ``['target' => '5.4']``.
  41. .. code-block:: diff
  42. --- Original
  43. +++ New
  44. <?php
  45. final class MyTest extends \PHPUnit_Framework_TestCase
  46. {
  47. public function testFoo()
  48. {
  49. - $mock1 = $this->getMock("Foo");
  50. + $mock1 = $this->createMock("Foo");
  51. $mock1 = $this->getMock("Bar", ["aaa"]); // version with multiple params is not supported
  52. }
  53. }
  54. Rule sets
  55. ---------
  56. The rule is part of the following rule sets:
  57. @PHPUnit54Migration:risky
  58. Using the `@PHPUnit54Migration:risky <./../../ruleSets/PHPUnit54MigrationRisky.rst>`_ rule set will enable the ``php_unit_mock`` rule with the config below:
  59. ``['target' => '5.4']``
  60. @PHPUnit55Migration:risky
  61. Using the `@PHPUnit55Migration:risky <./../../ruleSets/PHPUnit55MigrationRisky.rst>`_ rule set will enable the ``php_unit_mock`` rule with the config below:
  62. ``['target' => '5.5']``
  63. @PHPUnit56Migration:risky
  64. Using the `@PHPUnit56Migration:risky <./../../ruleSets/PHPUnit56MigrationRisky.rst>`_ rule set will enable the ``php_unit_mock`` rule with the config below:
  65. ``['target' => '5.5']``
  66. @PHPUnit57Migration:risky
  67. Using the `@PHPUnit57Migration:risky <./../../ruleSets/PHPUnit57MigrationRisky.rst>`_ rule set will enable the ``php_unit_mock`` rule with the config below:
  68. ``['target' => '5.5']``
  69. @PHPUnit60Migration:risky
  70. Using the `@PHPUnit60Migration:risky <./../../ruleSets/PHPUnit60MigrationRisky.rst>`_ rule set will enable the ``php_unit_mock`` rule with the config below:
  71. ``['target' => '5.5']``
  72. @PHPUnit75Migration:risky
  73. Using the `@PHPUnit75Migration:risky <./../../ruleSets/PHPUnit75MigrationRisky.rst>`_ rule set will enable the ``php_unit_mock`` rule with the config below:
  74. ``['target' => '5.5']``
  75. @PHPUnit84Migration:risky
  76. Using the `@PHPUnit84Migration:risky <./../../ruleSets/PHPUnit84MigrationRisky.rst>`_ rule set will enable the ``php_unit_mock`` rule with the config below:
  77. ``['target' => '5.5']``