<?php

/**

 * PHPUnit bootstrap file for SensFRX Plugin

 *

 * @package SensFRX

 */



// Define test environment

define( 'SENSFRX_PLUGIN_TESTS', true );



// Determine WordPress test library path

$_tests_dir = getenv( 'WP_TESTS_DIR' );



if ( ! $_tests_dir ) {

	// Try common WordPress test installation paths

	$possible_paths = array(

		'/tmp/wordpress-tests-lib',

		'C:/wamp64/www/wordpress-tests-lib',

		dirname( dirname( dirname( dirname( __FILE__ ) ) ) ) . '/wordpress-tests-lib',

	);

	

	foreach ( $possible_paths as $path ) {

		if ( file_exists( $path . '/includes/functions.php' ) ) {

			$_tests_dir = $path;

			break;

		}

	}

}



// If WordPress test suite is not available, use a minimal mock environment

if ( ! $_tests_dir || ! file_exists( $_tests_dir . '/includes/functions.php' ) ) {

	

	// Minimal WordPress mock for testing DB Helper independently

	if ( ! class_exists( 'wpdb' ) ) {

		class wpdb {

			public $prefix = 'wp_';

			public $last_error = '';

			public $insert_id = 0;

			public $num_rows = 0;

			

			public function prepare( $query, ...$args ) {

				// Simple prepare implementation

				array_walk( $args, function( &$arg ) {

					if ( is_null( $arg ) ) {

						$arg = 'NULL';

					} elseif ( is_int( $arg ) ) {

						$arg = (string) $arg;

					} elseif ( is_float( $arg ) ) {

						$arg = (string) $arg;

					} else {

						$arg = "'" . addslashes( (string) $arg ) . "'";

					}

				});

				

				$query = str_replace( array( '%s', '%d', '%f' ), '%s', $query );

				return vsprintf( $query, $args );

			}

			

			public function get_results( $query, $output = OBJECT ) {

				return array();

			}

			

			public function get_row( $query, $output = OBJECT, $y = 0 ) {

				return null;

			}

			

			public function get_var( $query, $x = 0, $y = 0 ) {

				return null;

			}

			

			public function insert( $table, $data, $format = null ) {

				return false;

			}

			

			public function update( $table, $data, $where, $format = null, $where_format = null ) {

				return false;

			}

			

			public function delete( $table, $where, $where_format = null ) {

				return false;

			}

		}

	}

	

	// Define WordPress constants if not already defined

	if ( ! defined( 'OBJECT' ) ) {

		define( 'OBJECT', 'OBJECT' );

	}

	if ( ! defined( 'ARRAY_A' ) ) {

		define( 'ARRAY_A', 'ARRAY_A' );

	}

	if ( ! defined( 'ARRAY_N' ) ) {

		define( 'ARRAY_N', 'ARRAY_N' );

	}

	if ( ! defined( 'WP_DEBUG' ) ) {

		define( 'WP_DEBUG', true );

	}

	

	// Mock WordPress functions needed for testing

	if ( ! function_exists( 'sanitize_sql_orderby' ) ) {

		function sanitize_sql_orderby( $orderby ) {

			$pattern = '/^\s*([a-z0-9_\.]+)(\s+(ASC|DESC))?\s*(,\s*([a-z0-9_\.]+)(\s+(ASC|DESC))?\s*)*$/i';

			if ( preg_match( $pattern, $orderby ) ) {

				return $orderby;

			}

			return false;

		}

	}

	

	if ( ! function_exists( 'esc_sql' ) ) {

		function esc_sql( $data ) {

			global $wpdb;

			if ( ! is_array( $data ) ) {

				return addslashes( $data );

			}

			foreach ( $data as &$value ) {

				$value = addslashes( $value );

			}

			return $data;

		}

	}

	

	if ( ! function_exists( 'sanitize_key' ) ) {

		function sanitize_key( $key ) {

			$key = strtolower( $key );

			$key = preg_replace( '/[^a-z0-9_\-]/', '', $key );

			return $key;

		}

	}

	

	if ( ! function_exists( 'wp_cache_get' ) ) {

		function wp_cache_get( $key, $group = '' ) {

			return false;

		}

	}

	

	if ( ! function_exists( 'wp_cache_set' ) ) {

		function wp_cache_set( $key, $data, $group = '', $expire = 0 ) {

			return true;

		}

	}

	

	if ( ! function_exists( 'plugin_dir_path' ) ) {

		function plugin_dir_path( $file ) {

			return dirname( $file ) . '/';

		}

	}

	

	if ( ! function_exists( 'plugin_basename' ) ) {

		function plugin_basename( $file ) {

			return basename( dirname( $file ) ) . '/' . basename( $file );

		}

	}

	

	// Minimal WP_UnitTestCase class for testing

	if ( ! class_exists( 'WP_UnitTestCase' ) ) {

		class WP_UnitTestCase {

			public function setUp() {}

			public function tearDown() {}

			

			public function assertTrue( $condition, $message = '' ) {

				if ( ! $condition ) {

					throw new Exception( $message ?: 'Assertion failed: expected true' );

				}

			}

			

			public function assertFalse( $condition, $message = '' ) {

				if ( $condition ) {

					throw new Exception( $message ?: 'Assertion failed: expected false' );

				}

			}

			

			public function assertEquals( $expected, $actual, $message = '' ) {

				if ( $expected != $actual ) {

					throw new Exception( 

						$message ?: sprintf( 

							'Assertion failed: expected %s, got %s',

							var_export( $expected, true ),

							var_export( $actual, true )

						)

					);

				}

			}

			

			public function assertNotEmpty( $value, $message = '' ) {

				if ( empty( $value ) ) {

					throw new Exception( $message ?: 'Assertion failed: value should not be empty' );

				}

			}

			

			public function assertEmpty( $value, $message = '' ) {

				if ( ! empty( $value ) ) {

					throw new Exception( $message ?: 'Assertion failed: value should be empty' );

				}

			}

			

			public function assertNull( $value, $message = '' ) {

				if ( ! is_null( $value ) ) {

					throw new Exception( $message ?: 'Assertion failed: value should be null' );

				}

			}

			

			public function assertNotNull( $value, $message = '' ) {

				if ( is_null( $value ) ) {

					throw new Exception( $message ?: 'Assertion failed: value should not be null' );

				}

			}

			

			public function assertContains( $needle, $haystack, $message = '' ) {

				if ( is_array( $haystack ) ) {

					if ( ! in_array( $needle, $haystack, true ) ) {

						throw new Exception( $message ?: 'Assertion failed: array should contain value' );

					}

				} elseif ( is_string( $haystack ) ) {

					if ( strpos( $haystack, $needle ) === false ) {

						throw new Exception( $message ?: 'Assertion failed: string should contain substring' );

					}

				}

			}

			

			public function assertInstanceOf( $expected, $actual, $message = '' ) {

				if ( ! ( $actual instanceof $expected ) ) {

					throw new Exception( $message ?: sprintf( 'Assertion failed: expected instance of %s', $expected ) );

				}

			}

			

			public function assertIsArray( $value, $message = '' ) {

				if ( ! is_array( $value ) ) {

					throw new Exception( $message ?: 'Assertion failed: value should be an array' );

				}

			}

			

			public function assertIsString( $value, $message = '' ) {

				if ( ! is_string( $value ) ) {

					throw new Exception( $message ?: 'Assertion failed: value should be a string' );

				}

			}

			

			public function assertGreaterThan( $expected, $actual, $message = '' ) {

				if ( ! ( $actual > $expected ) ) {

					throw new Exception( 

						$message ?: sprintf( 'Assertion failed: %s should be greater than %s', $actual, $expected ) 

					);

				}

			}

			

			public function assertGreaterThanOrEqual( $expected, $actual, $message = '' ) {

				if ( ! ( $actual >= $expected ) ) {

					throw new Exception( 

						$message ?: sprintf( 'Assertion failed: %s should be greater than or equal to %s', $actual, $expected ) 

					);

				}

			}

			

			public function assertNotFalse( $value, $message = '' ) {

				if ( $value === false ) {

					throw new Exception( $message ?: 'Assertion failed: value should not be false' );

				}

			}

		}

	}

	

	// Initialize global $wpdb for testing

	global $wpdb;

	if ( ! isset( $wpdb ) ) {

		$wpdb = new wpdb();

	}

	

	echo "Using minimal WordPress mock environment for testing.\n";

	echo "For full WordPress integration testing, install WordPress test suite.\n\n";

	

} else {

	// Load WordPress test suite

	require_once $_tests_dir . '/includes/functions.php';

	

	/**

	 * Manually load the plugin being tested.

	 */

	function _manually_load_plugin() {

		require dirname( dirname( __FILE__ ) ) . '/sensfrx-fraud-prevention-for-woocommerce.php';

	}

	tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );

	

	// Start up the WP testing environment.

	require $_tests_dir . '/includes/bootstrap.php';

}



// Load the DB Helper class

require_once dirname( dirname( __FILE__ ) ) . '/includes/class-sensfrx-db-helper.php';



echo "Bootstrap complete. Ready for testing.\n";



