Constants

Class Automattic\Jetpack\Constants


Description Description

Testing constants is hard. Once you define a constant, it’s defined. Constants Manager is an abstraction layer so that unit tests can set "constants" for tests.

To test your code, you’ll need to swap out defined( 'CONSTANT' ) with Automattic\Jetpack\Constants::is_defined( 'CONSTANT' ) and replace CONSTANT with Automattic\Jetpack\Constants::get_constant( 'CONSTANT' ). Then in the unit test, you can set the constant with Automattic\Jetpack\Constants::set_constant( 'CONSTANT', $value ) and then clean up after each test with something like this:

function tearDown() { Automattic\Jetpack\Constants::clear_constants(); }


Source Source

File: vendor/automattic/jetpack-constants/src/class-constants.php


			

Top ↑

Methods Methods

  • clear_constants — Resets all of the constants within constants Manager.
  • clear_single_constant — Will unset a "constant" from constants Manager if the constant exists.
  • get_constant — Attempts to retrieve the "constant" from constants Manager, and if it hasn't been set, then attempts to get the constant with the constant() function. If that also hasn't been set, attempts to get a value from filters.
  • is_defined — Checks if a "constant" has been set in constants Manager, and if not, checks if the constant was defined with define( 'name', 'value ).
  • is_true — Checks if a "constant" has been set in constants Manager and has the value of true
  • set_constant — Sets the value of the "constant" within constants Manager.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.