php_unit_dedicate_assert.rst 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. =================================
  2. Rule ``php_unit_dedicate_assert``
  3. =================================
  4. PHPUnit assertions like ``assertInternalType``, ``assertFileExists``, should be
  5. used over ``assertTrue``.
  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. ``target``
  14. ~~~~~~~~~~
  15. Target version of PHPUnit.
  16. Allowed values: ``'3.0'``, ``'3.5'``, ``'5.0'``, ``'5.6'`` and ``'newest'``
  17. Default value: ``'newest'``
  18. Examples
  19. --------
  20. Example #1
  21. ~~~~~~~~~~
  22. *Default* configuration.
  23. .. code-block:: diff
  24. --- Original
  25. +++ New
  26. <?php
  27. final class MyTest extends \PHPUnit_Framework_TestCase
  28. {
  29. public function testSomeTest()
  30. {
  31. - $this->assertTrue(is_float( $a), "my message");
  32. - $this->assertTrue(is_nan($a));
  33. + $this->assertInternalType('float', $a, "my message");
  34. + $this->assertNan($a);
  35. }
  36. }
  37. Example #2
  38. ~~~~~~~~~~
  39. With configuration: ``['target' => '5.6']``.
  40. .. code-block:: diff
  41. --- Original
  42. +++ New
  43. <?php
  44. final class MyTest extends \PHPUnit_Framework_TestCase
  45. {
  46. public function testSomeTest()
  47. {
  48. - $this->assertTrue(is_dir($a));
  49. - $this->assertTrue(is_writable($a));
  50. - $this->assertTrue(is_readable($a));
  51. + $this->assertDirectoryExists($a);
  52. + $this->assertIsWritable($a);
  53. + $this->assertIsReadable($a);
  54. }
  55. }
  56. Rule sets
  57. ---------
  58. The rule is part of the following rule sets:
  59. - `@PHPUnit30Migration:risky <./../../ruleSets/PHPUnit30MigrationRisky.rst>`_ with config:
  60. ``['target' => '3.0']``
  61. - `@PHPUnit32Migration:risky <./../../ruleSets/PHPUnit32MigrationRisky.rst>`_ with config:
  62. ``['target' => '3.0']``
  63. - `@PHPUnit35Migration:risky <./../../ruleSets/PHPUnit35MigrationRisky.rst>`_ with config:
  64. ``['target' => '3.5']``
  65. - `@PHPUnit43Migration:risky <./../../ruleSets/PHPUnit43MigrationRisky.rst>`_ with config:
  66. ``['target' => '3.5']``
  67. - `@PHPUnit48Migration:risky <./../../ruleSets/PHPUnit48MigrationRisky.rst>`_ with config:
  68. ``['target' => '3.5']``
  69. - `@PHPUnit50Migration:risky <./../../ruleSets/PHPUnit50MigrationRisky.rst>`_ with config:
  70. ``['target' => '5.0']``
  71. - `@PHPUnit52Migration:risky <./../../ruleSets/PHPUnit52MigrationRisky.rst>`_ with config:
  72. ``['target' => '5.0']``
  73. - `@PHPUnit54Migration:risky <./../../ruleSets/PHPUnit54MigrationRisky.rst>`_ with config:
  74. ``['target' => '5.0']``
  75. - `@PHPUnit55Migration:risky <./../../ruleSets/PHPUnit55MigrationRisky.rst>`_ with config:
  76. ``['target' => '5.0']``
  77. - `@PHPUnit56Migration:risky <./../../ruleSets/PHPUnit56MigrationRisky.rst>`_ with config:
  78. ``['target' => '5.6']``
  79. - `@PHPUnit57Migration:risky <./../../ruleSets/PHPUnit57MigrationRisky.rst>`_ with config:
  80. ``['target' => '5.6']``
  81. - `@PHPUnit60Migration:risky <./../../ruleSets/PHPUnit60MigrationRisky.rst>`_ with config:
  82. ``['target' => '5.6']``
  83. - `@PHPUnit75Migration:risky <./../../ruleSets/PHPUnit75MigrationRisky.rst>`_ with config:
  84. ``['target' => '5.6']``
  85. - `@PHPUnit84Migration:risky <./../../ruleSets/PHPUnit84MigrationRisky.rst>`_ with config:
  86. ``['target' => '5.6']``
  87. - `@PHPUnit91Migration:risky <./../../ruleSets/PHPUnit91MigrationRisky.rst>`_ with config:
  88. ``['target' => '5.6']``
  89. - `@PHPUnit100Migration:risky <./../../ruleSets/PHPUnit100MigrationRisky.rst>`_ with config:
  90. ``['target' => '5.6']``
  91. References
  92. ----------
  93. - Fixer class: `PhpCsFixer\\Fixer\\PhpUnit\\PhpUnitDedicateAssertFixer <./../../../src/Fixer/PhpUnit/PhpUnitDedicateAssertFixer.php>`_
  94. - Test class: `PhpCsFixer\\Tests\\Fixer\\PhpUnit\\PhpUnitDedicateAssertFixerTest <./../../../tests/Fixer/PhpUnit/PhpUnitDedicateAssertFixerTest.php>`_
  95. The test class defines officially supported behaviour. Each test case is a part of our backward compatibility promise.