php_unit_construct.rst 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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>`_