php_unit_construct.rst 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. ===========================
  2. Rule ``php_unit_construct``
  3. ===========================
  4. PHPUnit assertion method calls like ``->assertSame(true, $foo)`` should be
  5. written with dedicated method like ``->assertTrue($foo)``.
  6. Warning
  7. -------
  8. Using this rule is risky
  9. ~~~~~~~~~~~~~~~~~~~~~~~~
  10. Fixer could be risky if one is overriding PHPUnit's native methods.
  11. Configuration
  12. -------------
  13. ``assertions``
  14. ~~~~~~~~~~~~~~
  15. List of assertion methods to fix.
  16. Allowed values: a subset of ``['assertEquals', 'assertNotEquals', 'assertNotSame', 'assertSame']``
  17. Default value: ``['assertEquals', 'assertSame', 'assertNotEquals', 'assertNotSame']``
  18. Examples
  19. --------
  20. Example #1
  21. ~~~~~~~~~~
  22. *Default* configuration.
  23. .. code-block:: diff
  24. --- Original
  25. +++ New
  26. <?php
  27. final class FooTest extends \PHPUnit_Framework_TestCase {
  28. public function testSomething() {
  29. - $this->assertEquals(false, $b);
  30. - $this->assertSame(true, $a);
  31. - $this->assertNotEquals(null, $c);
  32. - $this->assertNotSame(null, $d);
  33. + $this->assertFalse($b);
  34. + $this->assertTrue($a);
  35. + $this->assertNotNull($c);
  36. + $this->assertNotNull($d);
  37. }
  38. }
  39. Example #2
  40. ~~~~~~~~~~
  41. With configuration: ``['assertions' => ['assertSame', 'assertNotSame']]``.
  42. .. code-block:: diff
  43. --- Original
  44. +++ New
  45. <?php
  46. final class FooTest extends \PHPUnit_Framework_TestCase {
  47. public function testSomething() {
  48. $this->assertEquals(false, $b);
  49. - $this->assertSame(true, $a);
  50. + $this->assertTrue($a);
  51. $this->assertNotEquals(null, $c);
  52. - $this->assertNotSame(null, $d);
  53. + $this->assertNotNull($d);
  54. }
  55. }
  56. Rule sets
  57. ---------
  58. The rule is part of the following rule sets:
  59. - `@PhpCsFixer:risky <./../../ruleSets/PhpCsFixerRisky.rst>`_
  60. - `@Symfony:risky <./../../ruleSets/SymfonyRisky.rst>`_
  61. References
  62. ----------
  63. - Fixer class: `PhpCsFixer\\Fixer\\PhpUnit\\PhpUnitConstructFixer <./../../../src/Fixer/PhpUnit/PhpUnitConstructFixer.php>`_
  64. - Test class: `PhpCsFixer\\Tests\\Fixer\\PhpUnit\\PhpUnitConstructFixerTest <./../../../tests/Fixer/PhpUnit/PhpUnitConstructFixerTest.php>`_
  65. The test class defines officially supported behaviour. Each test case is a part of our backward compatibility promise.