php_unit_mock.rst 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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
  7. -------
  8. Using this rule is risky
  9. ~~~~~~~~~~~~~~~~~~~~~~~~
  10. Risky when PHPUnit classes are overridden or not accessible, or when project has
  11. PHPUnit incompatibilities.
  12. Configuration
  13. -------------
  14. ``target``
  15. ~~~~~~~~~~
  16. Target version of PHPUnit.
  17. Allowed values: ``'5.4'``, ``'5.5'`` and ``'newest'``
  18. Default value: ``'newest'``
  19. Examples
  20. --------
  21. Example #1
  22. ~~~~~~~~~~
  23. *Default* configuration.
  24. .. code-block:: diff
  25. --- Original
  26. +++ New
  27. <?php
  28. final class MyTest extends \PHPUnit_Framework_TestCase
  29. {
  30. public function testFoo()
  31. {
  32. - $mock = $this->getMockWithoutInvokingTheOriginalConstructor("Foo");
  33. - $mock1 = $this->getMock("Foo");
  34. - $mock1 = $this->getMock("Bar", ["aaa"]);
  35. + $mock = $this->createMock("Foo");
  36. + $mock1 = $this->createMock("Foo");
  37. + $mock1 = $this->createPartialMock("Bar", ["aaa"]);
  38. $mock1 = $this->getMock("Baz", ["aaa"], ["argument"]); // version with more than 2 params is not supported
  39. }
  40. }
  41. Example #2
  42. ~~~~~~~~~~
  43. With configuration: ``['target' => '5.4']``.
  44. .. code-block:: diff
  45. --- Original
  46. +++ New
  47. <?php
  48. final class MyTest extends \PHPUnit_Framework_TestCase
  49. {
  50. public function testFoo()
  51. {
  52. - $mock1 = $this->getMock("Foo");
  53. + $mock1 = $this->createMock("Foo");
  54. $mock1 = $this->getMock("Bar", ["aaa"]); // version with multiple params is not supported
  55. }
  56. }
  57. Rule sets
  58. ---------
  59. The rule is part of the following rule sets:
  60. - `@PHPUnit54Migration:risky <./../../ruleSets/PHPUnit54MigrationRisky.rst>`_ with config:
  61. ``['target' => '5.4']``
  62. - `@PHPUnit55Migration:risky <./../../ruleSets/PHPUnit55MigrationRisky.rst>`_ with config:
  63. ``['target' => '5.5']``
  64. - `@PHPUnit56Migration:risky <./../../ruleSets/PHPUnit56MigrationRisky.rst>`_ with config:
  65. ``['target' => '5.5']``
  66. - `@PHPUnit57Migration:risky <./../../ruleSets/PHPUnit57MigrationRisky.rst>`_ with config:
  67. ``['target' => '5.5']``
  68. - `@PHPUnit60Migration:risky <./../../ruleSets/PHPUnit60MigrationRisky.rst>`_ with config:
  69. ``['target' => '5.5']``
  70. - `@PHPUnit75Migration:risky <./../../ruleSets/PHPUnit75MigrationRisky.rst>`_ with config:
  71. ``['target' => '5.5']``
  72. - `@PHPUnit84Migration:risky <./../../ruleSets/PHPUnit84MigrationRisky.rst>`_ with config:
  73. ``['target' => '5.5']``
  74. - `@PHPUnit91Migration:risky <./../../ruleSets/PHPUnit91MigrationRisky.rst>`_ with config:
  75. ``['target' => '5.5']``
  76. - `@PHPUnit100Migration:risky <./../../ruleSets/PHPUnit100MigrationRisky.rst>`_ with config:
  77. ``['target' => '5.5']``
  78. References
  79. ----------
  80. - Fixer class: `PhpCsFixer\\Fixer\\PhpUnit\\PhpUnitMockFixer <./../../../src/Fixer/PhpUnit/PhpUnitMockFixer.php>`_
  81. - Test class: `PhpCsFixer\\Tests\\Fixer\\PhpUnit\\PhpUnitMockFixerTest <./../../../tests/Fixer/PhpUnit/PhpUnitMockFixerTest.php>`_
  82. The test class defines officially supported behaviour. Each test case is a part of our backward compatibility promise.