2014-09-01 06:04:02 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2018-01-15 17:38:13 +00:00
|
|
|
* Base test case for all WooCommerce tests.
|
|
|
|
*
|
|
|
|
* @package WooCommerce\Tests
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* WC Unit Test Case.
|
2014-09-01 06:04:02 +00:00
|
|
|
*
|
|
|
|
* Provides WooCommerce-specific setup/tear down/assert methods, custom factories,
|
2015-11-03 13:31:20 +00:00
|
|
|
* and helper functions.
|
2014-09-01 06:04:02 +00:00
|
|
|
*
|
|
|
|
* @since 2.2
|
|
|
|
*/
|
|
|
|
class WC_Unit_Test_Case extends WP_UnitTestCase {
|
|
|
|
|
2018-01-15 17:38:13 +00:00
|
|
|
/**
|
|
|
|
* Holds the WC_Unit_Test_Factory instance.
|
|
|
|
*
|
|
|
|
* @var WC_Unit_Test_Factory
|
|
|
|
*/
|
2014-09-01 06:04:02 +00:00
|
|
|
protected $factory;
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Setup test case.
|
2014-09-01 06:04:02 +00:00
|
|
|
*
|
|
|
|
* @since 2.2
|
|
|
|
*/
|
|
|
|
public function setUp() {
|
|
|
|
|
|
|
|
parent::setUp();
|
|
|
|
|
2018-01-15 17:38:13 +00:00
|
|
|
// Add custom factories.
|
2014-09-01 06:04:02 +00:00
|
|
|
$this->factory = new WC_Unit_Test_Factory();
|
2014-09-05 06:35:53 +00:00
|
|
|
|
2018-01-15 17:38:13 +00:00
|
|
|
// Setup mock WC session handler.
|
2014-09-05 06:35:53 +00:00
|
|
|
add_filter( 'woocommerce_session_handler', array( $this, 'set_mock_session_handler' ) );
|
|
|
|
|
|
|
|
$this->setOutputCallback( array( $this, 'filter_output' ) );
|
2014-10-09 19:24:37 +00:00
|
|
|
|
2018-01-15 17:38:13 +00:00
|
|
|
// Register post types before each test.
|
2014-10-09 19:24:37 +00:00
|
|
|
WC_Post_types::register_post_types();
|
|
|
|
WC_Post_types::register_taxonomies();
|
2014-09-05 06:35:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Mock the WC session using the abstract class as cookies are not available.
|
|
|
|
* during tests.
|
2014-09-05 06:35:53 +00:00
|
|
|
*
|
2015-02-04 16:22:52 +00:00
|
|
|
* @since 2.2
|
2018-01-15 17:38:13 +00:00
|
|
|
* @return string The $output string, sans newlines and tabs.
|
2014-09-05 06:35:53 +00:00
|
|
|
*/
|
|
|
|
public function set_mock_session_handler() {
|
|
|
|
return 'WC_Mock_Session_Handler';
|
|
|
|
}
|
2014-09-05 06:36:46 +00:00
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Strip newlines and tabs when using expectedOutputString() as otherwise.
|
|
|
|
* the most template-related tests will fail due to indentation/alignment in.
|
|
|
|
* the template not matching the sample strings set in the tests.
|
2014-09-05 06:36:46 +00:00
|
|
|
*
|
|
|
|
* @since 2.2
|
2018-01-15 17:38:13 +00:00
|
|
|
*
|
|
|
|
* @param string $output The captured output.
|
|
|
|
* @return string The $output string, sans newlines and tabs.
|
2014-09-05 06:36:46 +00:00
|
|
|
*/
|
|
|
|
public function filter_output( $output ) {
|
|
|
|
|
|
|
|
$output = preg_replace( '/[\n]+/S', '', $output );
|
|
|
|
$output = preg_replace( '/[\t]+/S', '', $output );
|
|
|
|
|
|
|
|
return $output;
|
2014-09-01 06:04:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Asserts thing is not WP_Error.
|
2014-09-01 06:04:02 +00:00
|
|
|
*
|
|
|
|
* @since 2.2
|
2018-01-15 17:38:13 +00:00
|
|
|
* @param mixed $actual The object to assert is not an instance of WP_Error.
|
|
|
|
* @param string $message A message to display if the assertion fails.
|
2014-09-01 06:04:02 +00:00
|
|
|
*/
|
|
|
|
public function assertNotWPError( $actual, $message = '' ) {
|
|
|
|
$this->assertNotInstanceOf( 'WP_Error', $actual, $message );
|
|
|
|
}
|
|
|
|
|
2014-10-24 19:26:26 +00:00
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Asserts thing is WP_Error.
|
2014-10-24 19:26:26 +00:00
|
|
|
*
|
2018-01-15 17:38:13 +00:00
|
|
|
* @param mixed $actual The object to assert is an instance of WP_Error.
|
|
|
|
* @param string $message A message to display if the assertion fails.
|
2014-10-24 19:26:26 +00:00
|
|
|
*/
|
|
|
|
public function assertIsWPError( $actual, $message = '' ) {
|
|
|
|
$this->assertInstanceOf( 'WP_Error', $actual, $message );
|
|
|
|
}
|
|
|
|
|
2017-11-22 16:03:58 +00:00
|
|
|
/**
|
|
|
|
* Throws an exception with an optional message and code.
|
|
|
|
*
|
|
|
|
* Note: can't use `throwException` as that's reserved.
|
|
|
|
*
|
|
|
|
* @since 3.3-dev
|
2018-01-15 17:38:13 +00:00
|
|
|
* @param string $message Optional. The exception message. Default is empty.
|
|
|
|
* @param int $code Optional. The exception code. Default is empty.
|
|
|
|
* @throws Exception Containing the given message and code.
|
2017-11-22 16:03:58 +00:00
|
|
|
*/
|
|
|
|
public function throwAnException( $message = null, $code = null ) {
|
|
|
|
$message = $message ? $message : "We're all doomed!";
|
|
|
|
throw new Exception( $message, $code );
|
|
|
|
}
|
|
|
|
|
2014-09-05 23:21:31 +00:00
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Backport assertNotFalse to PHPUnit 3.6.12 which only runs in PHP 5.2.
|
2014-09-05 23:21:31 +00:00
|
|
|
*
|
2015-02-04 16:22:52 +00:00
|
|
|
* @since 2.2
|
2018-01-15 17:38:13 +00:00
|
|
|
* @param mixed $condition The statement to evaluate as not false.
|
|
|
|
* @param string $message A message to display if the assertion fails.
|
2014-09-05 23:21:31 +00:00
|
|
|
*/
|
|
|
|
public static function assertNotFalse( $condition, $message = '' ) {
|
|
|
|
|
2014-09-05 23:36:43 +00:00
|
|
|
if ( version_compare( phpversion(), '5.3', '<' ) ) {
|
2014-09-05 23:21:31 +00:00
|
|
|
|
|
|
|
self::assertThat( $condition, self::logicalNot( self::isFalse() ), $message );
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
parent::assertNotFalse( $condition, $message );
|
|
|
|
}
|
|
|
|
}
|
2014-09-01 06:04:02 +00:00
|
|
|
}
|