class.jetpack-constants.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * Class Jetpack_Constants
  4. *
  5. * Testing constants is hard. Once you define a constant, it's defined. Jetpack_Constants is an
  6. * abstraction layer so that unit tests can set "constants" for tests.
  7. *
  8. * To test your code, you'll need to swap out `defined( 'CONSTANT' )` with `Jetpack_Constants::is_defined( 'CONSTANT' )`
  9. * and replace `CONSTANT` with `Jetpack_Constants::get_constant( 'CONSTANT' )`. Then in the unit test, you can set the
  10. * constant with `Jetpack::set_constant( 'CONSTANT', $value )` and then clean up after each test with something like
  11. * this:
  12. *
  13. * function tearDown() {
  14. * Jetpack_Constants::clear_constants();
  15. * }
  16. */
  17. class Jetpack_Constants {
  18. static $set_constants = array();
  19. /**
  20. * Checks if a "constant" has been set in Jetpack_Constants, and if not,
  21. * checks if the constant was defined with define( 'name', 'value ).
  22. *
  23. * @param $name string The name of the constant
  24. *
  25. * @return bool
  26. */
  27. public static function is_defined( $name ) {
  28. return array_key_exists( $name, self::$set_constants )
  29. ? true
  30. : defined( $name );
  31. }
  32. /**
  33. * Checks if a "constant" has been set in Jetpack_Constants
  34. * and has the value of true
  35. *
  36. * @param $name string The name of the constant
  37. *
  38. * @return bool
  39. */
  40. public static function is_true( $name ) {
  41. return self::is_defined( $name) && self::get_constant( $name );
  42. }
  43. /**
  44. * Attempts to retrieve the "constant" from Jetpack_Constants, and if it hasn't been set,
  45. * then attempts to get the constant with the constant() function.
  46. *
  47. * @param $name
  48. *
  49. * @return mixed null if the constant does not exist or the value of the constant.
  50. */
  51. public static function get_constant( $name ) {
  52. if ( array_key_exists( $name, self::$set_constants ) ) {
  53. return self::$set_constants[ $name ];
  54. }
  55. return defined( $name ) ? constant( $name ) : null;
  56. }
  57. /**
  58. * Sets the value of the "constant" within Jetpack_Constants.
  59. *
  60. * @param $name string The name of the "constant"
  61. * @param $value string The value of the "constant"
  62. */
  63. public static function set_constant( $name, $value ) {
  64. self::$set_constants[ $name ] = $value;
  65. }
  66. /**
  67. * Will unset a "constant" from Jetpack_Constants if the constant exists.
  68. *
  69. * @param $name string The name of the "constant"
  70. *
  71. * @return bool Whether the constant was removed.
  72. */
  73. public static function clear_single_constant( $name ) {
  74. if ( ! array_key_exists( $name, self::$set_constants ) ) {
  75. return false;
  76. }
  77. unset( self::$set_constants[ $name ] );
  78. return true;
  79. }
  80. /**
  81. * Resets all of the constants within Jetpack_Constants.
  82. */
  83. public static function clear_constants() {
  84. self::$set_constants = array();
  85. }
  86. }