|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Test class for FixesManager. |
| 4 | + * |
| 5 | + * @package accessibility-checker |
| 6 | + */ |
| 7 | + |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | +use EqualizeDigital\AccessibilityChecker\Fixes\FixesManager; |
| 10 | +use EqualizeDigital\AccessibilityChecker\Fixes\FixInterface; |
| 11 | + |
| 12 | +/** |
| 13 | + * Unit tests for the FixesManager class. |
| 14 | + */ |
| 15 | +class FixesManagerTest extends WP_UnitTestCase { |
| 16 | + |
| 17 | + /** |
| 18 | + * Setup the test environment by resetting the instance before each test. |
| 19 | + * |
| 20 | + * @return void |
| 21 | + */ |
| 22 | + public function setUp(): void { |
| 23 | + parent::setUp(); |
| 24 | + // Reset the instance before each test. |
| 25 | + $reflection = new ReflectionClass( FixesManager::class ); |
| 26 | + $instance = $reflection->getProperty( 'instance' ); |
| 27 | + $instance->setAccessible( true ); |
| 28 | + $instance->setValue( null, null ); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Tear down the test environment by closing Mockery. |
| 33 | + * |
| 34 | + * @return void |
| 35 | + */ |
| 36 | + public function tearDown(): void { |
| 37 | + Mockery::close(); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Test that the instance retuns an empty array when no fixes are registered. |
| 42 | + * |
| 43 | + * @return void |
| 44 | + */ |
| 45 | + public function test_get_fixes_settings_returns_empty_array_when_no_fixes() { |
| 46 | + $fixes_manager = FixesManager::get_instance(); |
| 47 | + $this->assertEmpty( $fixes_manager->get_fixes_settings() ); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Test that the instance returns the correct structure when fixes are registered. |
| 52 | + * |
| 53 | + * @return void |
| 54 | + */ |
| 55 | + public function test_get_fixes_settings_returns_correct_structure() { |
| 56 | + $fix_mock = Mockery::mock( 'EqualizeDigital\AccessibilityChecker\Fixes\Fix\AddFileSizeAndTypeToLinkedFilesFix' ); |
| 57 | + $fix_mock->shouldReceive( 'get_slug' )->andReturn( 'mock_fix' ); |
| 58 | + $fix_mock->shouldReceive( 'get_fields_array' )->andReturn( |
| 59 | + [ |
| 60 | + 'field1' => [ 'default' => 'value1' ], |
| 61 | + 'field2' => [ 'default' => 'value2' ], |
| 62 | + ] |
| 63 | + ); |
| 64 | + $fix_mock->is_pro = true; |
| 65 | + |
| 66 | + $fixes_manager = FixesManager::get_instance(); |
| 67 | + $reflection = new ReflectionClass( $fixes_manager ); |
| 68 | + $fixes_property = $reflection->getProperty( 'fixes' ); |
| 69 | + $fixes_property->setAccessible( true ); |
| 70 | + $fixes_property->setValue( $fixes_manager, [ 'mock_fix' => $fix_mock ] ); |
| 71 | + |
| 72 | + $expected = [ |
| 73 | + 'mock_fix' => [ |
| 74 | + 'fields' => [ |
| 75 | + 'field1' => 'value1', |
| 76 | + 'field2' => 'value2', |
| 77 | + ], |
| 78 | + 'is_pro' => true, |
| 79 | + ], |
| 80 | + ]; |
| 81 | + |
| 82 | + $this->assertEquals( $expected, $fixes_manager->get_fixes_settings() ); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Test that the instance returns the default values when options aren't set. |
| 87 | + * |
| 88 | + * @return void |
| 89 | + */ |
| 90 | + public function test_get_fixes_settings_uses_default_values() { |
| 91 | + $fix_mock = Mockery::mock( 'EqualizeDigital\AccessibilityChecker\Fixes\Fix\AddFileSizeAndTypeToLinkedFilesFix' ); |
| 92 | + $fix_mock->shouldReceive( 'get_slug' )->andReturn( 'mock_fix' ); |
| 93 | + $fix_mock->shouldReceive( 'get_fields_array' )->andReturn( |
| 94 | + [ |
| 95 | + 'field1' => [ 'default' => 'default_value1' ], |
| 96 | + 'field2' => [ 'default' => 'default_value2' ], |
| 97 | + ] |
| 98 | + ); |
| 99 | + |
| 100 | + $fixes_manager = FixesManager::get_instance(); |
| 101 | + $reflection = new ReflectionClass( $fixes_manager ); |
| 102 | + $fixes_property = $reflection->getProperty( 'fixes' ); |
| 103 | + $fixes_property->setAccessible( true ); |
| 104 | + $fixes_property->setValue( $fixes_manager, [ 'mock_fix' => $fix_mock ] ); |
| 105 | + |
| 106 | + $expected = [ |
| 107 | + 'mock_fix' => [ |
| 108 | + 'fields' => [ |
| 109 | + 'field1' => 'default_value1', |
| 110 | + 'field2' => 'default_value2', |
| 111 | + ], |
| 112 | + 'is_pro' => false, |
| 113 | + ], |
| 114 | + ]; |
| 115 | + |
| 116 | + $this->assertEquals( $expected, $fixes_manager->get_fixes_settings() ); |
| 117 | + } |
| 118 | +} |
0 commit comments