php_unit_strict.rst 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. ========================
  2. Rule ``php_unit_strict``
  3. ========================
  4. PHPUnit methods like ``assertSame`` should be used instead of ``assertEquals``.
  5. Warning
  6. -------
  7. Using this rule is risky
  8. ~~~~~~~~~~~~~~~~~~~~~~~~
  9. Risky when any of the functions are overridden or when testing object equality.
  10. Configuration
  11. -------------
  12. ``assertions``
  13. ~~~~~~~~~~~~~~
  14. List of assertion methods to fix.
  15. Allowed values: a subset of ``['assertAttributeEquals', 'assertAttributeNotEquals', 'assertEquals', 'assertNotEquals']``
  16. Default value: ``['assertAttributeEquals', 'assertAttributeNotEquals', 'assertEquals', 'assertNotEquals']``
  17. Examples
  18. --------
  19. Example #1
  20. ~~~~~~~~~~
  21. *Default* configuration.
  22. .. code-block:: diff
  23. --- Original
  24. +++ New
  25. <?php
  26. final class MyTest extends \PHPUnit_Framework_TestCase
  27. {
  28. public function testSomeTest()
  29. {
  30. - $this->assertAttributeEquals(a(), b());
  31. - $this->assertAttributeNotEquals(a(), b());
  32. - $this->assertEquals(a(), b());
  33. - $this->assertNotEquals(a(), b());
  34. + $this->assertAttributeSame(a(), b());
  35. + $this->assertAttributeNotSame(a(), b());
  36. + $this->assertSame(a(), b());
  37. + $this->assertNotSame(a(), b());
  38. }
  39. }
  40. Example #2
  41. ~~~~~~~~~~
  42. With configuration: ``['assertions' => ['assertEquals']]``.
  43. .. code-block:: diff
  44. --- Original
  45. +++ New
  46. <?php
  47. final class MyTest extends \PHPUnit_Framework_TestCase
  48. {
  49. public function testSomeTest()
  50. {
  51. $this->assertAttributeEquals(a(), b());
  52. $this->assertAttributeNotEquals(a(), b());
  53. - $this->assertEquals(a(), b());
  54. + $this->assertSame(a(), b());
  55. $this->assertNotEquals(a(), b());
  56. }
  57. }
  58. Rule sets
  59. ---------
  60. The rule is part of the following rule set:
  61. - `@PhpCsFixer:risky <./../../ruleSets/PhpCsFixerRisky.rst>`_
  62. References
  63. ----------
  64. - Fixer class: `PhpCsFixer\\Fixer\\PhpUnit\\PhpUnitStrictFixer <./../../../src/Fixer/PhpUnit/PhpUnitStrictFixer.php>`_
  65. - Test class: `PhpCsFixer\\Tests\\Fixer\\PhpUnit\\PhpUnitStrictFixerTest <./../../../tests/Fixer/PhpUnit/PhpUnitStrictFixerTest.php>`_
  66. The test class defines officially supported behaviour. Each test case is a part of our backward compatibility promise.