|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Mantle\Tests\Support; |
| 4 | + |
| 5 | +use Mantle\Support\Attributes\Action; |
| 6 | +use Mantle\Support\Attributes\Filter; |
| 7 | +use Mantle\Support\Traits\Hookable; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | + |
| 10 | +class HookableAttributeTest extends TestCase { |
| 11 | + public function setUp(): void { |
| 12 | + parent::setUp(); |
| 13 | + |
| 14 | + remove_all_actions( 'example_action' ); |
| 15 | + } |
| 16 | + |
| 17 | + public function test_action_from_method_name(): void { |
| 18 | + $_SERVER['__hook_fired'] = false; |
| 19 | + |
| 20 | + $class = new class { |
| 21 | + use Hookable; |
| 22 | + |
| 23 | + #[Action( 'example_action' )] |
| 24 | + public function example_action( mixed $args ): void { |
| 25 | + $_SERVER['__hook_fired'] = $args; |
| 26 | + } |
| 27 | + }; |
| 28 | + |
| 29 | + new $class; |
| 30 | + |
| 31 | + $this->assertFalse( $_SERVER['__hook_fired'] ); |
| 32 | + |
| 33 | + do_action( 'example_action', 'foo' ); |
| 34 | + |
| 35 | + $this->assertSame( 'foo', $_SERVER['__hook_fired'] ); |
| 36 | + } |
| 37 | + |
| 38 | + public function test_action_from_method_name_with_priority(): void { |
| 39 | + |
| 40 | + $_SERVER['__hook_fired'] = []; |
| 41 | + |
| 42 | + $class = new class { |
| 43 | + use Hookable; |
| 44 | + |
| 45 | + #[Action( 'example_action', 20 )] |
| 46 | + public function action_at_20( mixed $args ): void { |
| 47 | + $_SERVER['__hook_fired'][] = 20; |
| 48 | + } |
| 49 | + |
| 50 | + #[Action( 'example_action' )] |
| 51 | + public function action_at_10( mixed $args ): void { |
| 52 | + $_SERVER['__hook_fired'][] = 10; |
| 53 | + } |
| 54 | + |
| 55 | + }; |
| 56 | + |
| 57 | + // Remove the action that was added by creating the anonymous class. |
| 58 | + remove_all_actions( 'example_action' ); |
| 59 | + |
| 60 | + new $class; |
| 61 | + |
| 62 | + $this->assertEmpty( $_SERVER['__hook_fired'] ); |
| 63 | + |
| 64 | + do_action( 'example_action', 'foo' ); |
| 65 | + |
| 66 | + $this->assertSame( [ 10, 20 ], $_SERVER['__hook_fired'] ); |
| 67 | + } |
| 68 | + |
| 69 | + public function test_filter_from_method_name(): void { |
| 70 | + $_SERVER['__hook_fired'] = false; |
| 71 | + |
| 72 | + $class = new class { |
| 73 | + use Hookable; |
| 74 | + |
| 75 | + #[Filter( 'example_action' )] |
| 76 | + public function filter_the_value( mixed $value ): mixed { |
| 77 | + $_SERVER['__hook_fired'] = $value; |
| 78 | + |
| 79 | + return 'bar'; |
| 80 | + } |
| 81 | + }; |
| 82 | + |
| 83 | + remove_all_filters( 'example_action' ); |
| 84 | + |
| 85 | + new $class; |
| 86 | + |
| 87 | + $this->assertFalse( $_SERVER['__hook_fired'] ); |
| 88 | + |
| 89 | + $value = apply_filters( 'example_action', 'foo' ); |
| 90 | + |
| 91 | + $this->assertSame( 'foo', $_SERVER['__hook_fired'] ); |
| 92 | + $this->assertSame( 'bar', $value ); |
| 93 | + } |
| 94 | + |
| 95 | + public function test_filter_from_method_name_with_priority(): void { |
| 96 | + $_SERVER['__hook_fired'] = []; |
| 97 | + |
| 98 | + $class = new class { |
| 99 | + use Hookable; |
| 100 | + |
| 101 | + #[Filter( 'example_action', priority: 20 )] |
| 102 | + public function filter_at_20( int $value ): int { |
| 103 | + $_SERVER['__hook_fired'][] = $value; |
| 104 | + |
| 105 | + return $value + 20; |
| 106 | + } |
| 107 | + |
| 108 | + #[Filter( 'example_action' )] |
| 109 | + public function filter_at_10( int $value ): int { |
| 110 | + $_SERVER['__hook_fired'][] = $value; |
| 111 | + |
| 112 | + return $value + 10; |
| 113 | + } |
| 114 | + |
| 115 | + }; |
| 116 | + |
| 117 | + // Remove the action that was added by creating the anonymous class. |
| 118 | + remove_all_actions( 'example_action' ); |
| 119 | + |
| 120 | + new $class; |
| 121 | + |
| 122 | + $this->assertEmpty( $_SERVER['__hook_fired'] ); |
| 123 | + |
| 124 | + $value = apply_filters( 'example_action', 5 ); |
| 125 | + |
| 126 | + $this->assertSame( [ 5, 15 ], $_SERVER['__hook_fired'] ); |
| 127 | + $this->assertSame( 35, $value ); |
| 128 | + } |
| 129 | + |
| 130 | + public function test_multiple_filters_on_one_method(): void { |
| 131 | + $_SERVER['__hook_fired'] = []; |
| 132 | + |
| 133 | + $class = new class { |
| 134 | + use Hookable; |
| 135 | + |
| 136 | + #[Filter( 'another_filter' )] |
| 137 | + #[Filter( 'example_action' )] |
| 138 | + public function filter_to_call( int $value ): int { |
| 139 | + $_SERVER['__hook_fired'][] = $value; |
| 140 | + |
| 141 | + return $value + 20; |
| 142 | + } |
| 143 | + }; |
| 144 | + |
| 145 | + // Remove the action that was added by creating the anonymous class. |
| 146 | + remove_all_actions( 'another_filter' ); |
| 147 | + remove_all_actions( 'example_action' ); |
| 148 | + |
| 149 | + new $class; |
| 150 | + |
| 151 | + $this->assertTrue( has_filter( 'another_filter' ) ); |
| 152 | + $this->assertTrue( has_filter( 'example_action' ) ); |
| 153 | + |
| 154 | + $this->assertEmpty( $_SERVER['__hook_fired'] ); |
| 155 | + |
| 156 | + $value = apply_filters( 'example_action', 5 ); |
| 157 | + |
| 158 | + $this->assertSame( [ 5 ], $_SERVER['__hook_fired'] ); |
| 159 | + $this->assertSame( 25, $value ); |
| 160 | + |
| 161 | + $_SERVER['__hook_fired'] = []; |
| 162 | + |
| 163 | + $value = apply_filters( 'another_filter', 10 ); |
| 164 | + |
| 165 | + $this->assertSame( [ 10 ], $_SERVER['__hook_fired'] ); |
| 166 | + $this->assertSame( 30, $value ); |
| 167 | + } |
| 168 | +} |
0 commit comments