Files
care-book-block-ultimate/BACKUP-ESSENTIALS/PRODUCTION-READY/care-booking-block-ultimate/tests/unit/test-cache-integration.php
Emanuel Almeida 38bb926742 chore: add spec-kit and standardize signatures
- Added GitHub spec-kit for development workflow
- Standardized file signatures to Descomplicar® format
- Updated development configuration

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-12 01:27:34 +01:00

297 lines
11 KiB
PHP

/**
* Descomplicar® Crescimento Digital
* https://descomplicar.pt
*/
<?php
/**
* WordPress cache integration tests for Care Booking Block plugin
*
* @package CareBookingBlock
*/
/**
* Test WordPress cache integration functionality
*/
class Test_Cache_Integration extends Care_Booking_Test_Case
{
/**
* Test cache manager class exists and can be instantiated
*/
public function test_cache_manager_class_exists()
{
$this->assertTrue(class_exists('Care_Booking_Cache_Manager'), 'Care_Booking_Cache_Manager class should exist');
$cache_manager = new Care_Booking_Cache_Manager();
$this->assertInstanceOf('Care_Booking_Cache_Manager', $cache_manager);
}
/**
* Test cache blocked doctors list
*/
public function test_cache_blocked_doctors()
{
$cache_manager = new Care_Booking_Cache_Manager();
$blocked_doctors = [999, 998, 997];
// Set cache
$result = $cache_manager->set_blocked_doctors($blocked_doctors);
$this->assertTrue($result, 'Should successfully cache blocked doctors');
// Get from cache
$cached_doctors = $cache_manager->get_blocked_doctors();
$this->assertIsArray($cached_doctors, 'Should return array from cache');
$this->assertEquals($blocked_doctors, $cached_doctors, 'Cached data should match original data');
// Verify WordPress transient was set
$transient_data = get_transient('care_booking_doctors_blocked');
$this->assertEquals($blocked_doctors, $transient_data, 'WordPress transient should contain correct data');
}
/**
* Test cache blocked services for doctor
*/
public function test_cache_blocked_services_by_doctor()
{
$cache_manager = new Care_Booking_Cache_Manager();
$doctor_id = 999;
$blocked_services = [888, 887, 886];
// Set cache
$result = $cache_manager->set_blocked_services($doctor_id, $blocked_services);
$this->assertTrue($result, 'Should successfully cache blocked services');
// Get from cache
$cached_services = $cache_manager->get_blocked_services($doctor_id);
$this->assertIsArray($cached_services);
$this->assertEquals($blocked_services, $cached_services);
// Verify WordPress transient was set with correct key
$transient_key = "care_booking_services_blocked_$doctor_id";
$transient_data = get_transient($transient_key);
$this->assertEquals($blocked_services, $transient_data);
}
/**
* Test cache expiration
*/
public function test_cache_expiration()
{
$cache_manager = new Care_Booking_Cache_Manager();
// Set short expiration for testing
$blocked_doctors = [999];
$result = $cache_manager->set_blocked_doctors($blocked_doctors, 1); // 1 second expiration
$this->assertTrue($result);
// Verify cache exists
$cached_data = $cache_manager->get_blocked_doctors();
$this->assertEquals($blocked_doctors, $cached_data);
// Wait for expiration
sleep(2);
// Verify cache expired
$expired_data = $cache_manager->get_blocked_doctors();
$this->assertFalse($expired_data, 'Cache should expire after timeout');
}
/**
* Test cache invalidation
*/
public function test_cache_invalidation()
{
$cache_manager = new Care_Booking_Cache_Manager();
// Set initial cache data
$cache_manager->set_blocked_doctors([999, 998]);
$cache_manager->set_blocked_services(999, [888, 887]);
$cache_manager->set_blocked_services(998, [886]);
// Verify cache exists
$this->assertNotFalse($cache_manager->get_blocked_doctors());
$this->assertNotFalse($cache_manager->get_blocked_services(999));
$this->assertNotFalse($cache_manager->get_blocked_services(998));
// Invalidate all cache
$result = $cache_manager->invalidate_all();
$this->assertTrue($result, 'Should successfully invalidate all cache');
// Verify cache was cleared
$this->assertFalse($cache_manager->get_blocked_doctors(), 'Blocked doctors cache should be cleared');
$this->assertFalse($cache_manager->get_blocked_services(999), 'Blocked services cache should be cleared');
$this->assertFalse($cache_manager->get_blocked_services(998), 'Blocked services cache should be cleared');
}
/**
* Test cache hash for change detection
*/
public function test_cache_hash_management()
{
$cache_manager = new Care_Booking_Cache_Manager();
// Set initial hash
$initial_hash = 'test_hash_123';
$result = $cache_manager->set_restrictions_hash($initial_hash);
$this->assertTrue($result);
// Get hash
$cached_hash = $cache_manager->get_restrictions_hash();
$this->assertEquals($initial_hash, $cached_hash);
// Update hash
$new_hash = 'test_hash_456';
$cache_manager->set_restrictions_hash($new_hash);
$updated_hash = $cache_manager->get_restrictions_hash();
$this->assertEquals($new_hash, $updated_hash);
$this->assertNotEquals($initial_hash, $updated_hash);
}
/**
* Test cache miss behavior
*/
public function test_cache_miss_behavior()
{
$cache_manager = new Care_Booking_Cache_Manager();
// Test getting non-existent cache
$non_existent_doctors = $cache_manager->get_blocked_doctors();
$this->assertFalse($non_existent_doctors, 'Should return false for cache miss');
$non_existent_services = $cache_manager->get_blocked_services(123);
$this->assertFalse($non_existent_services, 'Should return false for cache miss');
$non_existent_hash = $cache_manager->get_restrictions_hash();
$this->assertFalse($non_existent_hash, 'Should return false for cache miss');
}
/**
* Test cache key generation
*/
public function test_cache_key_generation()
{
$cache_manager = new Care_Booking_Cache_Manager();
// Test different doctor IDs generate different cache keys
$doctor1_services = [888];
$doctor2_services = [887];
$cache_manager->set_blocked_services(999, $doctor1_services);
$cache_manager->set_blocked_services(998, $doctor2_services);
// Verify separate caches
$cached_services_1 = $cache_manager->get_blocked_services(999);
$cached_services_2 = $cache_manager->get_blocked_services(998);
$this->assertEquals($doctor1_services, $cached_services_1);
$this->assertEquals($doctor2_services, $cached_services_2);
$this->assertNotEquals($cached_services_1, $cached_services_2);
}
/**
* Test cache size limits and memory usage
*/
public function test_cache_size_and_memory()
{
$cache_manager = new Care_Booking_Cache_Manager();
// Test caching large dataset
$large_doctor_list = range(1, 1000); // 1000 doctor IDs
$result = $cache_manager->set_blocked_doctors($large_doctor_list);
$this->assertTrue($result, 'Should handle large datasets');
$cached_large_list = $cache_manager->get_blocked_doctors();
$this->assertEquals($large_doctor_list, $cached_large_list, 'Large dataset should be cached correctly');
$this->assertCount(1000, $cached_large_list, 'All items should be cached');
}
/**
* Test cache with WordPress object cache compatibility
*/
public function test_wordpress_object_cache_compatibility()
{
$cache_manager = new Care_Booking_Cache_Manager();
// Test with WordPress wp_cache functions if available
if (function_exists('wp_cache_set') && function_exists('wp_cache_get')) {
$test_data = [999, 998];
// Use WordPress object cache directly
wp_cache_set('care_booking_test', $test_data, 'care_booking', 3600);
$wp_cached_data = wp_cache_get('care_booking_test', 'care_booking');
$this->assertEquals($test_data, $wp_cached_data, 'WordPress object cache should work with plugin data');
} else {
$this->markTestSkipped('WordPress object cache not available in test environment');
}
}
/**
* Test cache performance benchmarks
*/
public function test_cache_performance()
{
$cache_manager = new Care_Booking_Cache_Manager();
$test_data = range(1, 100);
// Measure cache write performance
$start_time = microtime(true);
$cache_manager->set_blocked_doctors($test_data);
$write_time = microtime(true) - $start_time;
$this->assertLessThan(0.1, $write_time, 'Cache write should complete in under 100ms');
// Measure cache read performance
$start_time = microtime(true);
$cached_data = $cache_manager->get_blocked_doctors();
$read_time = microtime(true) - $start_time;
$this->assertLessThan(0.05, $read_time, 'Cache read should complete in under 50ms');
$this->assertEquals($test_data, $cached_data);
}
/**
* Test cache invalidation on restriction changes
*/
public function test_cache_invalidation_on_changes()
{
$cache_manager = new Care_Booking_Cache_Manager();
// Set initial cache
$cache_manager->set_blocked_doctors([999]);
$cache_manager->set_blocked_services(999, [888]);
// Verify cache exists
$this->assertNotFalse($cache_manager->get_blocked_doctors());
$this->assertNotFalse($cache_manager->get_blocked_services(999));
// Simulate restriction change event
do_action('care_booking_restriction_updated', 'doctor', 999);
// Cache should be automatically invalidated
$this->assertFalse($cache_manager->get_blocked_doctors(), 'Cache should be invalidated on restriction change');
}
/**
* Test cache with concurrent access
*/
public function test_concurrent_cache_access()
{
$cache_manager = new Care_Booking_Cache_Manager();
// Simulate concurrent writes (in real scenario this would be multiple requests)
$cache_manager->set_blocked_doctors([999]);
$cache_manager->set_blocked_doctors([998]);
$cache_manager->set_blocked_doctors([997]);
// Last write should win
$final_data = $cache_manager->get_blocked_doctors();
$this->assertEquals([997], $final_data, 'Last cache write should be preserved');
}
}